C# 壓縮和解壓檔案(SharpZipLib)

小菜來報導發表於2014-09-17

先從網上下載ICSharpCode.SharpZipLib.dll類庫

將檔案或資料夾壓縮為zip,函式如下

 1         /// <summary>
 2         /// 壓縮檔案
 3         /// </summary>
 4         /// <param name="fileName">壓縮檔案路徑</param>
 5         /// <param name="zipName">壓縮的檔名稱</param>
 6         /// <param name="error">返回的錯誤資訊</param>
 7         /// <returns></returns>
 8         public bool FileToZip(string fileName, string zipName, out string error)
 9         {
10             error = string.Empty;
11             try
12             {
13                 ZipOutputStream s = new ZipOutputStream(File.Create(zipName));
14                 s.SetLevel(6); // 0 - store only to 9 - means best compression
15                 zip(fileName, s);
16                 s.Finish();
17                 s.Close();
18                 return true;
19             }
20             catch (Exception ex)
21             {
22                 error = ex.Message;
23                 return false;
24             }
25         }
26 
27 
28         private void zip(string fileName, ZipOutputStream s)
29         {
30             if (fileName[fileName.Length - 1] != Path.DirectorySeparatorChar)
31                 fileName += Path.DirectorySeparatorChar;
32             Crc32 crc = new Crc32();
33             string[] filenames = Directory.GetFileSystemEntries(fileName);
34             foreach (string file in filenames)
35             {
36                 if (Directory.Exists(file))
37                 {
38                     zip(file, s);
39                 }
40                 else // 否則直接壓縮檔案
41                 {
42                     //開啟壓縮檔案
43                     FileStream fs = File.OpenRead(file);
44 
45                     byte[] buffer = new byte[fs.Length];
46                     fs.Read(buffer, 0, buffer.Length);
47                     string tempfile = Path.GetFileName(file);
48                     ZipEntry entry = new ZipEntry(tempfile);
49 
50                     entry.DateTime = DateTime.Now;
51                     entry.Size = fs.Length;
52                     fs.Close();
53                     crc.Reset();
54                     crc.Update(buffer);
55                     entry.Crc = crc.Value;
56                     s.PutNextEntry(entry);
57 
58                     s.Write(buffer, 0, buffer.Length);
59                 }
60             }
61         }

 

將zip解壓為檔案或資料夾,函式程式碼如下

 1         /// <summary>
 2         /// 解壓檔案
 3         /// </summary>
 4         /// <param name="zipName">解壓檔案路徑</param>
 5         /// <param name="fileDirName">解壓到資料夾的名稱</param>
 6         /// <param name="error">返回的錯誤資訊</param>
 7         /// <returns></returns>
 8         public bool ZipToFile(string zipName, string fileDirName, out string error)
 9         {
10             try
11             {
12                 error = string.Empty;
13                 //讀取壓縮檔案(zip檔案),準備解壓縮
14                 ZipInputStream s = new ZipInputStream(File.Open(zipName.Trim(), FileMode.Open, FileAccess.Read));
15                 ZipEntry theEntry;
16 
17                 string rootDir = " ";
18                 while ((theEntry = s.GetNextEntry()) != null)
19                 {
20                     string path = fileDirName;
21                     //獲取該檔案在zip中目錄
22                     rootDir = Path.GetDirectoryName(theEntry.Name);
23                     //獲取檔名稱
24                     string fileName = Path.GetFileName(theEntry.Name);
25                     if (string.IsNullOrEmpty(fileName))
26                         continue;
27                     //判斷是否為頂層檔案,是,將檔案直接放在fileDirName下,否,建立目錄
28                     if (string.IsNullOrEmpty(rootDir))
29                     {
30                         if (!Directory.Exists(path))
31                             Directory.CreateDirectory(path);
32                     }
33                     else
34                     {
35                         path += "\\" + rootDir;
36                         if (!Directory.Exists(path))
37                             Directory.CreateDirectory(path);
38                     }
39 
40                     //將檔案流寫入對應目錄下的檔案中
41                     if (fileName != String.Empty)
42                     {
43                         FileStream streamWriter = File.Create(path + "\\" + fileName);
44 
45                         int size = 2048;
46                         byte[] data = new byte[2048];
47                         while (true)
48                         {
49                             if (theEntry.Size == 0)
50                                 break;
51 
52                             size = s.Read(data, 0, data.Length);
53                             if (size > 0)
54                             {
55                                 streamWriter.Write(data, 0, size);
56                             }
57                             else
58                             {
59                                 break;
60                             }
61                         }
62                         streamWriter.Close();
63                     }
64                 }
65                 s.Close();
66                 return true;
67             }
68             catch (Exception ex)
69             {
70                 error = ex.Message;
71                 return false;
72             }
73         }

呼叫示例

1 string error;
2             if (FileToZip(@"E:\文件", "文件.zip", out error))
3                 MessageBox.Show("Succee");
4             else
5                 MessageBox.Show(error);
壓縮示例
1 string error;
2             if (ZipToFile(@"E:\文件.zip", "文件", out error))
3                 MessageBox.Show("Succee");
4             else
5                 MessageBox.Show(error);
解壓示例

 

相關文章