利用DotNetZip服務端壓縮檔案並下載

SunRain117發表於2014-07-11

public void DownFile() {
              string filePath = Server.MapPath("/Files/txt/bb.txt");//檔案所在位置
              var fs = Response.OutputStream;
            using(Ionic.Zip.ZipFile zf = new Ionic.Zip.ZipFile())

    {
              zf.AddFile(filePath, "txtfile");
              //zf.Save(Server.MapPath("/Files/")+"dd.zip");//檔案儲存的地址
              zf.Save(fs);//儲存到輸出流

    }

            string fileName = "dd.zip"; //下載顯示的檔名字
            //string filePath = Server.MapPath("/Files/txt/bb.txt");
            //FileStream fs = new FileStream(filePath, FileMode.Open);
            //byte[] bytes = new byte[fs.Length];
            //fs.Read(bytes, 0, bytes.Length);
            //fs.Close();

            Response.ContentType = "application/octet-stream";
            Response.AddHeader("Content-Disposition", "attachment;filename=" + HttpUtility.UrlEncode(fileName, System.Text.Encoding.UTF8));
            //Response.BinaryWrite(bytes);
            Response.Flush();
            Response.End();
        }

 

類庫下載地址:http://dotnetzip.codeplex.com/

相關文章