zoukankan      html  css  js  c++  java
  • 使用 ICharpCode.SharpZipLib 压缩指定目录结构

    今天做项目中遇见一个压缩问题,我的目录结构是树形菜单,文件在服务器存储是平面存储,没有目录结构,所以在下载指定目录的时候要构建目录结构,如下:

    当我右键点击下载b目录文件夹的时候要Download  b 目录下的所有文件,并且有目录结构,实现如下:

     1         public static void CreateZip(List<string> directories, List<string> filenames, string zipFileName, string dir)
     2         {
     3             using (ZipOutputStream ZipStream = new ZipOutputStream(System.IO.File.Create(dir + zipFileName)))
     4             {
     5                 ZipStream.SetLevel(9);
     6                 ZipEntryFactory factory = new ZipEntryFactory();
     7                 foreach (var directory in directories)
     8                 {
     9                     string virtualDirectory = directory;
    10                     ZipEntry zipEntry = factory.MakeDirectoryEntry(virtualDirectory);
    11                     zipEntry.DateTime = DateTime.Now;
    12                     ZipStream.PutNextEntry(zipEntry);
    13                 }
    14 
    15                 byte[] buffer = new byte[4096];
    16                 for (int i = 0; i < filenames.Count; i++)
    17                 {
    18                     string file = filenames[i];
    19                     string newfileName = file.Replace(dir, string.Empty);
    20                     ZipEntry entry = factory.MakeFileEntry(directories[i] + "//" + newfileName);
    21 
    22                     entry.DateTime = DateTime.Now;
    23                     ZipStream.PutNextEntry(entry);
    24 
    25                     using (FileStream fs = System.IO.File.OpenRead(file))
    26                     {
    27                         int sourceBytes;
    28                         do
    29                         {
    30                             sourceBytes = fs.Read(buffer, 0, buffer.Length);
    31                             ZipStream.Write(buffer, 0, sourceBytes);
    32                         } while (sourceBytes > 0);
    33                     }
    34                 }
    35                 ZipStream.Finish();
    36                 ZipStream.Close();
    37             }
    38 
    39             System.Web.HttpContext.Current.Response.ContentType = "application/x-compress zip";
    40             System.Web.HttpContext.Current.Response.AddHeader("Content-Disposition", "attachment;filename=" + zipFileName);
    41             System.Web.HttpContext.Current.Response.TransmitFile(dir + zipFileName);
    42         }
    以下载b目录为例,参数如下:
    directories:

    filenames:

    zipFileName: 

     dir:

    调用上面方法传入正确参数即可生成正确的目录

    
    
  • 相关阅读:
    NOIP2011
    Android获取百度音乐下载音乐和歌词下载链接
    oracle备份和升级数据库
    谁刚开始学习----数据
    [Angular2] Build reuseable template with ngTemplateOutlet
    [Angular 2] Set Values on Generated Angular 2 Templates with Template Context
    [Angular2 Router] Resolving route data in Angular 2
    [MobX] MobX fundamentals: deriving computed values and managing side effects with reactions
    [Docker] Build a Simple Node.js Web Server with Docker
    [AngularJS NG-redux] Integrate Redux Devtools
  • 原文地址:https://www.cnblogs.com/mlgblog/p/ICharpCode.html
Copyright © 2011-2022 走看看