压缩解压缩下载多个文件

 需要引用的空间
using ICSharpCode.SharpZipLib.Zip;
using System.Collections;
using System.IO;
using System.Net; 


//ICSharpCode.SharpZipLib.Zip下载网址:http://www.icsharpcode.net/OpenSource/SharpZipLib/  


 /// <summary>
        /// 压缩多个文件 并下载
        /// <param name="strpath">待压缩的文件路径(物理路径如d://123.txt)</param>
        /// <param name="string strFileName">最终的压缩包文件名</param>
        /// </summary>              
        public void DownMultiFileOfZip(List<string> strPath, string strFileName)
        {
            MemoryStream ms = new MemoryStream();//创建可存储的内存流,用来放置压缩流;          
            ZipOutputStream zos = new ZipOutputStream(ms);//压缩流
            long ChunkSize = 524288;//512K 每次读取文件,只读取512K,这样可以缓解服务器的压力
                      
            //向压缩流中添加文件  
            for (int i = 0; i < strPath.Count; i++)
            {
                System.IO.FileInfo fileInfo = new System.IO.FileInfo(strPath[i].ToString());
                if (fileInfo.Exists == true)
                {
                    byte[] buffer = new byte[ChunkSize];
                    System.Web.HttpContext.Current.Response.Clear();
                    string strEntryName = System.IO.Path.GetFileName(strPath[i].ToString()); //根据路径取文件名
                    ZipEntry entry = new ZipEntry(strEntryName);
                    zos.PutNextEntry(entry);

                    FileStream fs = File.OpenRead(strPath[i].ToString());
                    long dataLengthToRead = fs.Length;//获取下载的文件总大小
                    while (dataLengthToRead > 0 && System.Web.HttpContext.Current.Response.IsClientConnected)
                    {
                        int lengthRead = fs.Read(buffer, 0, Convert.ToInt32(ChunkSize));//读取的大小
                        zos.Write(buffer, 0, lengthRead);
                        dataLengthToRead = dataLengthToRead - lengthRead;
                    }
                    fs.Close();
                }
            }

            //关闭压缩流
            zos.Finish();
            zos.Close();

            //打开下载压缩流的保存框          
            System.Web.HttpContext.Current.Response.ContentType = "application/octet-stream";
            strFileName = HttpUtility.UrlEncode(strFileName).Replace('+', ' ');
            System.Web.HttpContext.Current.Response.AddHeader("Content-Disposition", "attachment; filename=" + strFileName + ".zip");                      
            System.Web.HttpContext.Current.Response.Clear();
            System.Web.HttpContext.Current.Response.BinaryWrite(ms.ToArray());            
            System.Web.HttpContext.Current.Response.End();
        }
        /// <summary>
        /// 压缩多个文件
        /// </summary>
        public void WriteZip()
        {
            List<string> strPath = new List<string>();//多个文件路径
            strPath.Add("d:\\1.txt");
            strPath.Add("d:\\2.txt");

            FileInfo zipFile = new FileInfo("d:\\mldn.zip"); //定义压缩文件名称
            ZipOutputStream zipOut = null; // 定义压缩输出流
            zipOut = new ZipOutputStream(zipFile.Create()); // 实例化压缩输出流对象,并指定压缩文件的输出路径    

            for (int i = 0; i < strPath.Count; i++)
            {
                FileInfo file = new FileInfo(strPath[i]); //定义要压缩的文件
                if (file.Exists == false)
                {
                    Response.Write("要压缩的文件(" + strPath[i] + ")不存在<br/>");
                    continue;
                }                              
                zipOut.PutNextEntry(new ZipEntry(file.Name));   // 创建ZipEntry  每一个被压缩的文件都用ZipEntry表示,需要为每一个压缩后的文件设置名称  
                zipOut.SetComment("这里是注释");  // 设置注释   

                FileStream fs = file.OpenRead();//定义输出流
                long dataLengthToRead = fs.Length; ////获取输出流总大小                           

                while (dataLengthToRead > 0)
                {   // 读取内容
                    byte[] buffer = new byte[524288];
                    int lengthRead = fs.Read(buffer, 0, buffer.Length);//读取的大小
                    zipOut.Write(buffer, 0, lengthRead); // 压缩输出内容                
                    dataLengthToRead = dataLengthToRead - lengthRead;
                }
                fs.Close();  // 关闭输入流   
            }
            
            zipOut.Finish();  //完成压缩流          
            zipOut.Close(); // 关闭压缩输出流                         

        }
        /// <summary>
        /// 解压缩
        /// </summary>
        public void ReadZip()
        {
            string zipFilePath = "d:\\mldn.zip";//压缩文件位置
            string unZipPath = "d:\\";//放置解压缩文件的目录位置

            if (!unZipPath.EndsWith("\\"))
                unZipPath += "\\";

            using (ZipInputStream inzip = new ZipInputStream(File.OpenRead(zipFilePath)))
            {
                ZipEntry theEntry=null ;
                while ((theEntry = inzip.GetNextEntry()) != null)
                {
                    string theEntryDirectoryName = Path.GetDirectoryName(theEntry.Name);//获取目录名称
                    string theEntryFileName = Path.GetFileName(theEntry.Name);//获取文件名称
                    if (theEntryDirectoryName.Length > 0)
                    {
                        Directory.CreateDirectory(unZipPath + theEntryDirectoryName);//创建放置解压缩文件的子目录
                    }
                    if (!theEntryDirectoryName.EndsWith("\\"))
                        theEntryDirectoryName += "\\";

                    if (theEntryFileName != String.Empty)
                    {
                        using (FileStream ts = File.Create(unZipPath + theEntry.Name))
                        {
                            int inSize = 524288;
                            byte[] date = new byte[524288];
                            while (true)
                            {
                                inSize = inzip.Read(date, 0, date.Length);
                                if (inSize > 0)
                                {
                                    ts.Write(date, 0, inSize);
                                }
                                else
                                {
                                    break;
                                }
                            }
                        }
                    }
                }
            }

        }

原文地址:https://www.cnblogs.com/xiaonanmu/p/2455089.html