zoukankan      html  css  js  c++  java
  • 下载文件夹里面的所有文件,并压缩成.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_Products2014.10DemosDemosFormsAuthenticationimg";
                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:Usersv-xsongDesktop11", @"C:Usersv-xsongDesktop11.zip",CompressionLevel.Fastest,true);
                Response.Write("OK");
  • 相关阅读:
    Quartz_理解2
    Quartz_理解1
    Java监控常用工具 .
    DB2函数大全
    cvs上传复制项目
    PLSQL DEVELOPER 使用的一些技巧【转】
    webservice_模拟报文测试
    Myeclipse插件将wsdl生成java客户端代码
    利用 UltraEdit 重新排版 XML 结构数据
    uoj164. 【清华集训2015】V 统计
  • 原文地址:https://www.cnblogs.com/songxia/p/4040939.html
Copyright © 2011-2022 走看看