现在的位置: 首页 > 综合 > 正文

ASP.NET下Zip,GZip,BZip2,Tar的实时压缩与解压缩

2013年02月12日 ⁄ 综合 ⁄ 共 1711字 ⁄ 字号 评论关闭

ASP.Net实现实时压缩的方法多种多样了。比较多都是使用开源的SharpZipLib

下载地址是:http://prdownloads.sourceforge.net/sharpdevelop/084SharpZipLib.zip?use_mirror=jaist

结构很明晰

ICSharpCode.SharpZipLib.Tar.*
       Tar implementation
ICSharpCode.SharpZipLib.GZip.*
       Gzip implementation
ICSharpCode.SharpZipLib.BZip2.*
       Bzip2 implementation
ICSharpCode.SharpZipLib.Zip
       Zip implementation
ICSharpCode.SharpZipLib.Zip.Compression.Streams
       Inflater/Deflater streams

网络上有不少代码范例:

〈%@ Import namespace=“ICSharpCode.SharpZipLib.Zip“ %〉
〈%@ Import Namespace=“System.IO“ %〉
〈script language=“c#“ runat=“server“〉
ZipOutputStream zos=null;
String strBaseDir=““;
void dlZipDir(string strPath,string strFileName){
  MemoryStream ms =null;
  Response.ContentType = “application/octet-stream“;
  strFileName=HttpUtility.UrlEncode(strFileName).Replace(’+’,’ ’);
  Response.AddHeader(“Content-Disposition“, “attachment; filename=“ + strFileName+“.zip“);
  ms = new MemoryStream();
  zos = new ZipOutputStream(ms);
  strBaseDir=strPath+“//“;
  addZipEntry(strBaseDir);
  zos.Finish();
  zos.Close();
  Response.Clear();
  Response.BinaryWrite(ms.ToArray());
  Response.End();
}

void addZipEntry(string PathStr){
  DirectoryInfo di= new DirectoryInfo(PathStr);
  foreach(DirectoryInfo item in di.GetDirectories()){
   addZipEntry(item.FullName);
  }
  foreach(FileInfo item in di.GetFiles()){
   FileStream fs = File.OpenRead(item.FullName);
   byte[] buffer = new byte[fs.Length];
   fs.Read(buffer, 0, buffer.Length);
   string strEntryName=item.FullName.Replace(strBaseDir,““);
   ZipEntry entry = new ZipEntry(strEntryName);
   zos.PutNextEntry(entry);
   zos.Write(buffer, 0, buffer.Length);
   fs.Close();
  }
}
void Page_Load(){
  dlZipDir(Server.MapPath(“.“),“test“);
}
〈/script〉

 

当然如果觉得这个比较复杂还有更简单的NZipLib.dll 可以用

http://www.aspheute.com/english/20011115.asp

这个是比较老牌的

【上篇】
【下篇】

抱歉!评论已关闭.