zoukankan      html  css  js  c++  java
  • 使用微软自带 SharpZipLib 进行多文件 压缩

     1         /// <summary>
     2         /// 指定路径打包下载
     3         /// </summary>
     4         /// <param name="fileName"></param>
     5         /// <param name="filePath"></param>
     6         public void ZIPfiles(DataTable dt)
     7         {
     8              
     9             //string filename = "开店日期(正常).xlsx";
    10             //string filepath = "C:/ExportFolder/CADD87B4EDBE28A47669673F53DEC8458A998D5B84EEAA88EE6647CA6DF1D1E8.xlsx";
    11             string ZipName = string.Empty;
    12             MemoryStream ms = new MemoryStream();
    13             Encoding gb2312 = Encoding.GetEncoding("gb2312"); //对方英文服务器   进行转码
    14             ICSharpCode.SharpZipLib.Zip.ZipConstants.DefaultCodePage = gb2312.CodePage;//上面和这一句是为了防止真是坏境是英文服务器出现中文乱码的问题。
    15             ZipOutputStream zos = new ZipOutputStream(ms);
    16             FileStream fs = null; 
    17             //  byte[] buffer = null;
    18             System.IO.BinaryReader br = null;
    19             
    20             try
    21             {
    22                 if (dt.Rows.Count > 0)
    23                 {
    24                     //防止名称重复  这一段肯定是有点消耗性能的  暂时没有找出更好的方法改进。 请各位大神有好的意见 说一下。
    25                     for (int i = dt.Rows.Count - 2; i > 0; i--)
    26                     {
    27                         int a = 0;
    28                         string title = dt.Rows[0]["fileName"].ToString();
    29                         Logger.Log.Debug(title);
    30                         for (int j = i + 1; j > 0; j--)
    31                         {
    32                             if (dt.Rows[j]["fileName"].ToString() == title)
    33                             {
    34                                 a++;
    35                                 dt.Rows[j]["fileName"] = title + "("+a+")";
    36 
    37                             }
    38                             Logger.Log.Debug(dt.Rows[j]["fileName"]);
    39                         }
    40 
    41                     }
    42                     
    43                         foreach (DataRow dr in dt.Rows)
    44                         {
    45                             Logger.Log.Debug(dr["filePath"].ToString());
    46                             fs = new FileStream(dr["filePath"].ToString(), System.IO.FileMode.Open);//文件地址、
    47                             // fs = new FileStream(filepath, System.IO.FileMode.Open);//文件地址
    48                             br = new BinaryReader((Stream)fs);
    49                             byte[] buffer = br.ReadBytes((Int32)fs.Length); 
    50                             ZipEntry entry = new ZipEntry(dr["fileName"].ToString());//文件名
    51                             //ZipEntry entry = new ZipEntry(filename);//文件名
    52                             zos.PutNextEntry(entry);//UTF-8 
    53                             zos.Write(buffer, 0, buffer.Length); 
    54                             fs.Close();   //每一个文件打开之后 然后执行完就必须在循环中关闭,  否则就会出现进程冲突
    55                         }
    56                     ZipName = DateTime.Now.ToString("yyyyMMdd");
    57                     Logger.Log.Debug(ZipName);
    58                 }
    59             }
    60             catch (Exception ex)
    61             {
    62                 Logger.Log.Error("ZIP打包错误" + ex);
    63                 throw (ex);
    64             }
    65 
    66             zos.Close();
    67             
    68             HttpContext.Current.Response.ContentType = "application/octet-stream";
    69             HttpContext.Current.Response.AddHeader("Content-Disposition", "attachment;  filename=" + HttpUtility.UrlEncode(ZipName + ".zip", System.Text.Encoding.UTF8));
    70             HttpContext.Current.Response.AddHeader("Content-Length", ms.ToArray().Length.ToString());
    71             HttpContext.Current.Response.BinaryWrite(ms.ToArray());
    72             HttpContext.Current.Response.Flush();
    73             HttpContext.Current.Response.End();
    74         }
  • 相关阅读:
    .NET 分布式自增Id组件(解决自动分配机器Id、时间回拨问题)
    简洁实用Socket框架DotNettySocket
    Colder框架硬核更新(Sharding+IOC)
    .NET Core开源快速开发框架Colder发布 (NET Core2.1+AdminLTE版)
    .NET开源快速开发框架Colder发布 (NET452+AdminLTE版)
    .NET 跨平台RPC框架DotNettyRPC
    Web后台快速开发框架(.NET Core)
    Web后台快速开发框架
    EasyWcf------无需配置,无需引用,动态绑定,轻松使用
    C# .NET 0配置使用Wcf(半成品)
  • 原文地址:https://www.cnblogs.com/Cjb8973/p/9035029.html
Copyright © 2011-2022 走看看