日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 编程语言 > C# >内容正文

C#

Using ZipLib to create a Zip File in C#

發布時間:2024/4/11 C# 36 豆豆
生活随笔 收集整理的這篇文章主要介紹了 Using ZipLib to create a Zip File in C# 小編覺得挺不錯的,現在分享給大家,幫大家做個參考.

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 year = System.DateTime.Today.Year.ToString();
??????????? string month = System.DateTime.Today.Month.ToString();
??????????? string days = System.DateTime.Today.Day.ToString();????


??????????? string[] filenames = Directory.GetFiles(curDir);


??????????? string zipFileName = "Rplan Report " + year + month + days + ".zip";
??? ????????string zipAbsPath = this.curDir + zipFileName;


??????????? zipRelativePath = zipFileName;


??????????? Crc32 crc = new Crc32();
??????????? ZipOutputStream s = new ZipOutputStream(File.Create(zipAbsPath)); //
指定zip文件的絕對路徑,包括文件名

??????????? s.SetLevel(6); // 0 - store only to 9 - means best compression


??????????? foreach (string file in filenames)
??????????? {
??????????????? FileStream fs = File.OpenRead(file); //
文件的絕對路徑,包括文件名


byte[] buffer = new byte[fs.Length];
fs.Read(buffer, 0, buffer.Length);
ZipEntry entry = new ZipEntry(extractFileName(file)); //這里ZipEntry的參數必須是相對路徑名,表示文件在zip文檔里的相對路徑


entry.DateTime = DateTime.Now;

?

// set Size and the crc, because the information

??????????????? // 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;
??????????????? fs.Close();


??????????????? crc.Reset();

??? ????????????crc.Update(buffer);


??????????????? entry.Crc = crc.Value;


??????????????? s.PutNextEntry(entry);

?

s.Write(buffer, 0, buffer.Length);


??????????? }


??????????? s.Finish();
??????????? s.Close();

??????? }

?

// e.g. convert ?c:\\temp\test.xls to test.xls

?????? private string extractFileName(string filePath)
??????? {

??

??????????? int index1 = filePath.LastIndexOf("\\");


??????????? string fileName =

??????????????? filePath.Substring(index1+1);

?

return fileName;
}


?

Reference

1ZipLib
http://www.icsharpcode.net/OpenSource/SharpZipLib/Default.aspx
A port of zlib library to C#. Its license allows developers to include this library in commercial, closed-source applications.


2. System.IO.Compression Namespace
http://msdn2.microsoft.com/en-us/library/system.io.compression.aspx


DeflateStream Class
http://msdn2.microsoft.com/en-us/library/system.io.compression.deflatestream.aspx


3Compression Application Sample
http://msdn2.microsoft.com/en-us/library/ywf6dxhx.aspx

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

轉載于:https://www.cnblogs.com/yuquanlaobo/archive/2007/01/17/622851.html

總結

以上是生活随笔為你收集整理的Using ZipLib to create a Zip File in C#的全部內容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。