DotNetZip文件压缩解压组建

 官方源码下载:http://files.cnblogs.com/zhengjuzhuan/DotNetZipLib-DevKit-v1[1].8.zip

代码
   
     
DotNetZip 是一个短小易用的用来操作 zip 文件的 .NET 类库,支持.NET的任何语言,可很方便的创建,读取,和更新zip文件。而且还可以使用在.NETCompact Framework中。

下面是一些简单的例子:

1 .加密压缩:

using (ZipFile zip = new ZipFile())
{
zip.Password
= sPassword; // set pwd
zip.AddDirectory(sZipDir);
zip.Save(sSavePath
+ @" \ " + sSaveName);
}

2 .向压缩文件中添加:
using (ZipFile zip = new ZipFile( " Backup.zip " ))
{
zip.Password
= " 123456! " ;
zip.AddFile(
" ReadMe.txt " );
zip.AddFile(
" 7440-N49th.png " );
zip.AddFile(
" 2005_Annual_Report.pdf " );
zip.Save();
}

3 .解压缩到制定目录:
using (ZipFile zip = ZipFile.Read( " D:\\test\\2007.zip " ))
{
foreach (ZipEntry e in zip)
{
Console.WriteLine(
" file name:{0} " , e.FileName);
Console.WriteLine(e.Comment);
e.Extract(
" D:\\test\\pwdata " , true ); // overwrite == true
}
}

4 .中文乱码问题 System.Text.Encoding.Default
using (ZipFile zip = new ZipFile(System.Text.Encoding.Default))
{
foreach (var f in filesToInclude)
{
zip.AddFile(f,
" files " );
}
zip.AddEntry(
" Readme.txt " , "" , ReadmeText);
zip.Save(Response.OutputStream);
}

5 .Asp.net文件打包下载
Ionic.Zip.ZipFile ZipFilePack
= new Ionic.Zip.ZipFile(System.Text.Encoding.Default);
string soureDirectory = this .MapPath( " FilePack/测试1号/temp " );
string sourePath = this .MapPath( " FilePack/研发中心bbbb.doc " );
ZipFilePack.AddDirectory(soureDirectory,
" temp " ); // 文件夹下的文件和文件夹压缩到temp目录下
ZipFilePack.AddFile(sourePath, " abc " ); // 文件压缩到abc目录下
ZipFilePack.AddFile(sourePath, "" ); // 文件压缩到根目录下
Response.Clear();
Response.BufferOutput
= false ;
Response.ContentEncoding
= System.Text.Encoding.UTF8;
Response.ContentType
= " application/zip " ;
Response.AddHeader(
" content-disposition " , " filename= " + Guid.NewGuid().ToString() + " .zip " );
ZipFilePack.Save(Response.OutputStream);
Response.Close();

开源首页:http://dotnetzip.codeplex.com/

你可能感兴趣的:(zip)