標籤:style blog http color java 使用 os io strong
SharpZipLib 檔案/檔案夾壓縮
一、ZipFile
ZipFile類用於選擇檔案或檔案夾進行壓縮產生壓縮包。
常用屬性:
| 屬性 |
說明 |
| Count |
檔案數目(注意是在ComitUpdat之後才有) |
| Password |
壓縮包密碼 |
| Size |
壓縮包佔用空間大小 |
| Name |
壓縮包名稱,預設輸出是檔案路徑 |
| ZipEntry |
壓縮包裡的檔案,通過索引[]訪問 |
其常用方法如下:
| 方法 |
說明 |
| Add |
添加要進行壓縮的檔案 |
| AddDirectory |
添加檔案夾(不會壓縮檔夾裡的檔案) |
| Delete |
刪除檔案或檔案夾 |
| BeginUpdate |
開始修改壓縮包 |
| CommitUpdate |
提交修改 |
| SetComment |
添加註釋 |
樣本1(建立壓縮檔):
using (ZipFile zip = ZipFile.Create(@"D:\test.zip")) { zip.BeginUpdate(); zip.SetComment("這是我的壓縮包"); zip.Add(@"D:\1.txt"); //添加一個檔案 zip.AddDirectory(@"D:\2"); //添加一個檔案夾(這個方法不會壓縮檔夾裡的檔案) zip.Add(@"D:\2\2.txt"); //添加檔案夾裡的檔案 zip.CommitUpdate(); }
這樣產生的壓縮包是包含子檔案夾,子檔案夾也是包含子檔案的。
其中,注釋如下:
樣本2:修改壓縮包
using (ZipFile zip = new ZipFile(@"D:\test.zip")) { zip.BeginUpdate(); zip.Add(@"D:\2.txt"); zip.CommitUpdate(); }
留意這個樣本和上面的有什麼不同,上面的是Create方法建立的ZipFile對象,而這裡是直接讀。因此,如果壓縮包裡面有檔案,則不會改動原來的壓縮檔,而是往會裡面添加一個。這樣就相當於壓縮包的修改,而上面是壓縮包的建立。
樣本3:讀取壓縮包裡的檔案:
using (ZipFile zip = new ZipFile(@"D:\test.zip")) { foreach (ZipEntry z in zip) { Console.WriteLine(z); } ZipEntry z1 = zip[0]; Console.WriteLine(z1.Name); }二、FastZip
這個類就兩個方法:
| 方法 |
說明 |
| CreateZip |
壓縮目錄 |
| ExtractZip |
解壓縮目錄 |
1、FastZip用於快速壓縮目錄,樣本如下:
//快速壓縮目錄,包括目錄下的所有檔案(new FastZip()).CreateZip(@"D:\test.zip", @"D:\test\", true, "");
這個是遞迴壓縮的。但是局限性就是只能壓縮檔夾。
否則報如下錯誤:
2、快速解壓縮目錄
//快速解壓(new FastZip()).ExtractZip(@"D:\test.zip", @"D:\解壓目錄\", "");
三、ZipOutputStream與ZipEntry
- ZipOutputStream:相當於一個壓縮包;
- ZipEntry:相當於壓縮包裡的一個檔案;
以上兩個類是SharpZipLib的主類,最耐玩的就是這兩個類。
ZipOutputStream常用屬性:
| 屬性 |
說明 |
| IsFinished |
ZipOutputStream是否已結束 |
ZipOutputStream常用方法:
| 方法 |
說明 |
| CloseEntry |
關閉入口,關閉之後不允許再對ZipOutputStream進行操作 |
| Finish |
結束寫入 |
| GetLevel |
讀取壓縮等級 |
| PutNextEntry |
往ZipOutputStream裡寫入一個ZipEntry |
| SetComment |
壓縮包的注釋 |
| SetLevel |
設定壓縮等級,等級越高檔案越小 |
| Write |
寫入檔案內容 |
使用ZipOutputStream建立一個壓縮包並往裡面寫入一個檔案的樣本:
static void Main(string[] args) { using (ZipOutputStream s = new ZipOutputStream(File.Create(@"D:\123.zip"))) { s.SetLevel(6); //設定壓縮等級,等級越高壓縮效果越明顯,但佔用CPU也會更多using (FileStream fs = File.OpenRead(@"D:\1.txt")) { byte[] buffer = new byte[4 * 1024]; //緩衝區,每次操作大小 ZipEntry entry = new ZipEntry(Path.GetFileName(@"改名.txt")); //建立壓縮包內的檔案 entry.DateTime = DateTime.Now; //檔案建立時間 s.PutNextEntry(entry); //將檔案寫入壓縮包 int sourceBytes; do { sourceBytes = fs.Read(buffer, 0, buffer.Length); //讀取檔案內容(1次讀4M,寫4M) s.Write(buffer, 0, sourceBytes); //將檔案內容寫入壓縮相應的檔案 } while (sourceBytes > 0); } s.CloseEntry(); } Console.ReadKey(); }
以上樣本僅僅能夠壓縮檔,要壓縮檔夾就要使用遞迴的方式,迴圈子目錄並壓縮子目錄裡的檔案。
樣本2:檔案夾壓縮,保持原檔案夾架構:
class Program { static void Main(string[] args) { string Source = @"D:\test"; string TartgetFile = @"D:\test.zip"; Directory.CreateDirectory(Path.GetDirectoryName(TartgetFile)); using (ZipOutputStream s = new ZipOutputStream(File.Create(TartgetFile))) { s.SetLevel(6); Compress(Source, s); s.Finish(); s.Close(); } Console.ReadKey(); } /// <summary> /// 壓縮 /// </summary> /// <param name="source">來源目錄</param> /// <param name="s">ZipOutputStream對象</param> public static void Compress(string source, ZipOutputStream s) { string[] filenames = Directory.GetFileSystemEntries(source); foreach (string file in filenames) { if (Directory.Exists(file)) { Compress(file, s); //遞迴壓縮子檔案夾 } else { using (FileStream fs = File.OpenRead(file)) { byte[] buffer = new byte[4 * 1024]; ZipEntry entry = new ZipEntry(file.Replace(Path.GetPathRoot(file),"")); //此處去掉盤符,如D:\123\1.txt 去掉D: entry.DateTime = DateTime.Now; s.PutNextEntry(entry); int sourceBytes; do { sourceBytes = fs.Read(buffer, 0, buffer.Length); s.Write(buffer, 0, sourceBytes); } while (sourceBytes > 0); } } } } }
附上解壓縮方法:
/// <summary> /// 解壓縮 /// </summary> /// <param name="sourceFile">源檔案</param> /// <param name="targetPath">目標路經</param> public bool Decompress(string sourceFile, string targetPath) { if (!File.Exists(sourceFile)) { throw new FileNotFoundException(string.Format("未能找到檔案 ‘{0}‘ ", sourceFile)); } if (!Directory.Exists(targetPath)) { Directory.CreateDirectory(targetPath); } using (ZipInputStream s = new ZipInputStream(File.OpenRead(sourceFile))) { ZipEntry theEntry; while ((theEntry = s.GetNextEntry()) != null) { string directorName = Path.Combine(targetPath, Path.GetDirectoryName(theEntry.Name)); string fileName = Path.Combine(directorName, Path.GetFileName(theEntry.Name)); // 建立目錄 if (directorName.Length > 0) { Directory.CreateDirectory(directorName); } if (fileName != string.Empty) { using (FileStream streamWriter = File.Create(fileName)) { int size = 4096; byte[] data = new byte[ 4 * 1024]; while (true) { size = s.Read(data, 0, data.Length); if (size > 0) { streamWriter.Write(data, 0, size); } else break; } } } } } return true; }
ZipEntry就沒什麼好說的了,都是一些屬性,指示一下,實際用到的很少。
轉自:http://www.cnblogs.com/kissdodog/p/3525295.html
c#操作Zip壓縮檔