下载文件夹里面的所有文件,并压缩成.zip压缩包的形式

http://www.aspsnippets.com/Articles/Download-multiple-files-as-Zip-Archive-File-in-ASPNet-using-C-and-VBNet.aspx

1.HTML tag

 <div>

        <asp:Button ID="Button1" runat="server" Text="DownLoadFile" OnClick="Button1_Click" />

    </div>

2.添加命名空间

using System.IO;

using Ionic.Zip;

using System.Collections.Generic;


3.下载文件事件

     protected void Button1_Click(object sender, EventArgs e)

        {

            string strPath = @"D:\ASP.NET_Products\2014.10Demos\Demos\FormsAuthentication\img";

            string[] files = System.IO.Directory.GetFiles(strPath);

            using (Ionic.Zip.ZipFile zip = new Ionic.Zip.ZipFile())

            {

                zip.AlternateEncodingUsage = ZipOption.AsNecessary;

                zip.AddDirectoryByName("Files");

                foreach (string fiel in files)

                {

                    strPath = System.IO.Path.Combine(strPath, fiel);

                    zip.AddFile(strPath, "Files");

                }

                Response.Clear();

                Response.BufferOutput = false;

                string zipName = String.Format("Zip_{0}.zip", DateTime.Now.ToString("yyyy-MMM-dd-HHmmss"));

                Response.ContentType = "application/zip";

                Response.AddHeader("content-disposition", "attachment; filename=" + zipName);

                zip.Save(Response.OutputStream);

                Response.End();

            }





            //下载一个文件的方法



            //Response.ContentType = ContentType;

            //Response.AppendHeader("Content-Disposition", "attachment; filename=" + Path.GetFileName(strPath));

            //Response.WriteFile(strPath);

            //Response.End();

        }

 -------------------------------------------------------

---------------------------------------------------------

使用 ZipFile.CreateFromDirectory方法快速创建ZIP文件

1. 引入命名空间

 

using System.IO.Compression;

using System.IO.Compression.ZipFile;

 

2. 调用方法

 ZipFile.CreateFromDirectory(@"C:\Users\v-xsong\Desktop\11", @"C:\Users\v-xsong\Desktop\11.zip",CompressionLevel.Fastest,true);

            Response.Write("OK");

 

你可能感兴趣的:(下载文件)