您的位置:首页 > 编程语言 > ASP

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

2008-10-14 15:16 405 查看
ASP.Net实现实时压缩的方法多种多样了。比较多都是使用开源的SharpZipLib

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

[align=left]ICSharpCode.SharpZipLib.Tar.*[/align]
[align=left]       Tar implementation[/align]
[align=left]ICSharpCode.SharpZipLib.GZip.*[/align]
[align=left]       Gzip implementation[/align]
[align=left]ICSharpCode.SharpZipLib.BZip2.*[/align]
[align=left]       Bzip2 implementation[/align]
[align=left]ICSharpCode.SharpZipLib.Zip[/align]
[align=left]       Zip implementation[/align]
[align=left]ICSharpCode.SharpZipLib.Zip.Compression.Streams[/align]
[align=left]       Inflater/Deflater streams[/align]
网络上有不少代码范例:

〈%@ 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

这个是比较老牌的
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
相关文章推荐