利用ICSharpCode.SharpZipLib.Zip进行文件压缩

官网http://www.icsharpcode.net/

支持文件和字符压缩。

创建全新的压缩包

第一步,创建压缩包

using ICSharpCode.SharpZipLib.Zip;
ZipOutputStream zipOutputStream = new ZipOutputStream(File.Create(strZipPath));
参数:strZipPath,提供压缩后的文件包全路径,即地址和文件名。如,D:\tmp\01.rar
第二步,向压缩包中添加压缩包的文件名
ICSharpCode.SharpZipLib.Zip.ZipEntry entry = new ICSharpCode.SharpZipLib.Zip.ZipEntry(name);
entry.DateTime = System.DateTime.Now; zipOutputStream.PutNextEntry(entry);
如01.rar压缩包中包含A.txt,B.txt,C.txt三个文件,那么参数name则对应为A.txt,B.txt,C.txt。
第三步,向压缩包中的entry写入文件
System.IO.FileStream fs = System.IO.File.OpenRead(strFile);
byte[] buffer = new byte[fs.Length];
fs.Read(buffer, 0, buffer.Length);
zipOutputStream.Write(buffer, 0, buffer.Length);
fs.Close();
fs.Dispose();

参数:strFile,待压缩的文件全路径。

第二步、第三步,可以循环,向压缩包中添加更多的文件。

向已有的压缩包中添加新文件

ICSharpCode.SharpZipLib.Zip.ZipFile z = new ICSharpCode.SharpZipLib.Zip.ZipFile();

唯一区别是使用ZipFile代替ZipOutputStream,打开已有的压缩文件。

每天学习,进步一点点。

 

posted on 2014-08-01 09:52  lucika.zh  阅读(1171)  评论(0编辑  收藏  举报

导航