在.NET中有時會遇到處理壓縮檔的情況, 內建的GZipStream並不是非常容易使用.看了一下網路的資料, 決定使用SharpZipLib和DotNetZip.
SharpZipLib
範例如下:
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();
}
DotNetZip Library
範例請參考:http://www.codeplex.com/DotNetZip/Wiki/View.aspx?title=Examples&referringTitle=Home
參考:
http://blog.miniasp.com/?tag=/sharpziplib
http://blog.blueshop.com.tw/mamba/archive/2007/04/03/50387.aspx
http://died.tw/2009/04/c-zip.html
訂閱:
張貼留言 (Atom)
沒有留言:
張貼留言