在线时间:8:00-16:00
迪恩网络APP
随时随地掌握行业动态
扫描二维码
关注迪恩网络微信公众号
System.IO.Compression是.Net 2.0里与压缩有关的命名空间,但是使用起来并不是很方便。使用第3方库ziplib可以很方便地进行压缩类的操作。 从[1] 下载动态库,然后在工程里Add Reference,把ICSharpCode.SharpZipLib.dll加进去。 在代码来创建一个zip包的例子如下(摘自ziplib sample code) using ICSharpCode.SharpZipLib.Checksums; using ICSharpCode.SharpZipLib.Zip; using ICSharpCode.SharpZipLib.GZip; 把指定目录下的所有文件压缩到一个zip包里 private
void ZipTheReports() string[]
filenames = Directory.GetFiles(curDir); string
zipFileName = "Rplan Report " +
year + month + days + ".zip"; zipRelativePath = zipFileName; Crc32
crc = new Crc32(); s.SetLevel(6); // 0 - store only to 9 - means best compression foreach
(string file in
filenames) // about the size and crc should be stored in the header // if it is not set it is automatically written in the footer. // (in this case size == crc == -1 in the header) // Some ZIP programs have problems with zip files that don't store // the size and crc in the header. entry.Size = fs.Length; crc.Reset(); crc.Update(buffer); entry.Crc = crc.Value; s.PutNextEntry(entry); } s.Finish(); } private string extractFileName(string filePath) int
index1 = filePath.LastIndexOf("\\"); string fileName = filePath.Substring(index1+1); 1.ZipLib 2. System.IO.Compression Namespace DeflateStream Class 3.Compression
Application Sample This sample demonstrates compression capabilities available in the .NET Framework. It builds a Windows Forms application that employs the GZipStream and DeflateStream types to compress and decompress files. The sample also introduces several types that are new in the .NET Framework version 2.0. 4. Using the Zip Classes in the J# Class Libraries to Compress Files and Data with C# http://msdn.microsoft.com/msdnmag/issues/03/06/ZipCompression/default.aspx |
请发表评论