zoukankan      html  css  js  c++  java
  • SharpZipLib 压缩ZIP导出

     1          var uploadSectionDir = Path.Combine("Upload", "QQ", DateTime.Now.ToString("yyyyMMdd"));
     2             string uploadDir = Path.Combine(HttpRuntime.AppDomainAppPath, uploadSectionDir);
     3             if (!Directory.Exists(uploadDir))
     4             {
     5                 Directory.CreateDirectory(uploadDir);
     6             }
     7             string fileName ="test.zip";
     8             string filePath = Path.Combine(uploadDir, fileName);
     9 
    10             //生成的压缩文件为test.zip
    11             using (FileStream fsOut = System.IO.File.Create(filePath))
    12             {
    13                 //ZipOutputStream类的构造函数需要一个流,文件流、内存流都可以,压缩后的内容会写入到这个流中。
    14                 using (ZipOutputStream zipStream = new ZipOutputStream(fsOut))
    15                 {
    16                     MemoryStream ws = new AirBillBLL().ExportToExcel(ladingNoList);
    17                     string entryName = string.Concat(string.Format("{0:yyyyMMddHHmmssffff}", DateTime.Now), ".xls");
    18                     ZipEntry newEntry = new ZipEntry(entryName);
    19                     newEntry.DateTime = DateTime.Now;
    20                     newEntry.Size = ws.Length;
    21 
    22                     //把压缩项的信息添加到ZipOutputStream中。
    23                     zipStream.PutNextEntry(newEntry);
    24                     byte[] buffer = new byte[4096];
    25                     //把需要压缩文件以文件流的方式复制到ZipOutputStream中。
    26 
    27                     StreamUtils.Copy(ws, zipStream, buffer);
    28 
    29                     zipStream.CloseEntry();
    30                     zipStream.IsStreamOwner = false;
    31                     zipStream.Finish();
    32                     zipStream.Close();
    33                 }
    34             }
    35 return File(filePath, "application/x-zip-compressed", string.Concat(string.Format("{0:yyyyMMddHHmmssffff}", DateTime.Now), ".zip"));
  • 相关阅读:
    文档API
    vi编辑器常用方法
    storm实战入门一
    redis教程
    为redis分配一个新的端口
    Lucene分页查询
    Lucene搜索方式大合集
    HBase Scan类用法
    java.util.Queue用法
    Makefile中预定义变量
  • 原文地址:https://www.cnblogs.com/LiuFengH/p/9862309.html
Copyright © 2011-2022 走看看