首页 > 代码库 > SharpZipLib 压缩文档下载

SharpZipLib 压缩文档下载

using ICSharpCode.SharpZipLib.Zip;

  Response.Clear();
  Response.ClearContent();
  Response.ClearHeaders();
//创建压缩输出流
  ZipOutputStream zos = null;
//创建内存流
  MemoryStream ms = null;
  Response.ContentType = "application/octet-stream";   Response.AddHeader("Content-Disposition", "attachment; filename=" + fileName + ".zip");
  ms = new MemoryStream();
//关联压缩流和内存流
  zos = new ZipOutputStream(ms);
    string strEntryName = fileName + ".pdf";
//创建压缩的对象实体放入压缩流,可循环创建
 foreach (string file in filenames)  
     {  
        ZipEntry entry = new ZipEntry(Path.GetFileName(file));  
       entry.DateTime = DateTime.Now;  
        zos.PutNextEntry(entry);  
       using (FileStream fs = File.OpenRead(file))  
       {  
          int sourceBytes;  
           do  
              {  
                sourceBytes = fs.Read(buffer, 0, buffer.Length);  
                zos.Write(buffer, 0, sourceBytes);  
              } while (sourceBytes > 0);  
         }  
     }  
   zos.Finish();  
   zos.Close();
   Response.BinaryWrite(ms.ToArray());
   Response.End();       

  

SharpZipLib 压缩文档下载