c#壓縮檔案

micDavid發表於2021-06-23

話說當今壓縮市場三足鼎立,能叫上名號的有zip、rar、7z。其中zip是壓縮界的鼻祖,在各大平臺上的流行度最廣,rar是商業軟體,壓縮率和效率都是很高的,對個人使用者沒有限制。7z是開源的,屬於後起之秀,也有著不凡的壓縮率,但在記憶體佔有率的問題上,稍遜風騷。今天,主要總結下,windows平臺下,zip的壓縮與解壓的方法,用ICSharpCode元件。

一、單檔案壓縮

      場景,檔案可能比較大,需要壓縮傳輸,比如上傳和下載

     

 1         /// <summary>
 2         /// 單檔案壓縮
 3         /// </summary>
 4         /// <param name="sourceFile">原始檔</param>
 5         /// <param name="zipedFile">zip壓縮檔案</param>
 6         /// <param name="blockSize">緩衝區大小</param>
 7         /// <param name="compressionLevel">壓縮級別</param>
 8         public static void ZipFile(string sourceFile, string zipedFile, int blockSize = 1024, int compressionLevel = 6)
 9         {
10             if (!File.Exists(sourceFile))
11             {
12                 throw new System.IO.FileNotFoundException("The specified file " + sourceFile + " could not be found.");
13             }
14             var fileName = System.IO.Path.GetFileNameWithoutExtension(sourceFile);
15 
16             FileStream streamToZip = new FileStream(sourceFile, FileMode.Open, FileAccess.Read);
17             FileStream zipFile = File.Create(zipedFile);
18             ZipOutputStream zipStream = new ZipOutputStream(zipFile);
19 
20             ZipEntry zipEntry = new ZipEntry(fileName);
21             zipStream.PutNextEntry(zipEntry);
22 
23             //儲存、最快、較快、標準、較好、最好  0-9
24             zipStream.SetLevel(compressionLevel);
25 
26             byte[] buffer = new byte[blockSize];
27 
28             int size = streamToZip.Read(buffer, 0, buffer.Length);
29             zipStream.Write(buffer, 0, size);
30             try
31             {
32                 while (size < streamToZip.Length)
33                 {
34                     int sizeRead = streamToZip.Read(buffer, 0, buffer.Length);
35                     zipStream.Write(buffer, 0, sizeRead);
36                     size += sizeRead;
37                 }
38             }
39             catch (Exception ex)
40             {
41                 throw ex;
42             }
43             zipStream.Finish();
44             zipStream.Close();
45             streamToZip.Close();
46         }

說明:26行,blocksize為快取區大小,不能設定太大,如果太大也會報異常。26-38行,把檔案通過FileStream流,讀取到緩衝區中,再寫入到ZipOutputStream流。你可以想象,兩個管道,一個讀,另一個寫,中間是緩衝區,它們的工作方式是同步的方式。想一下,能不能以非同步的方式工作,讀的管道只管讀,寫的管道只管寫?如果是這樣一個場景,讀的特別快,寫的比較慢,比如,不是本地寫,而是要經過網路傳輸,就可以考慮非同步的方式。怎麼做,讀者可以自行改造。關鍵一點,流是有順序的,所以要保證順序的正確性即可。

二、多檔案壓縮

      這種場景也是比較多見,和單檔案壓縮類似,無非就是多迴圈幾次。

     

 1         /// <summary>
 2         /// 多檔案壓縮
 3         /// </summary>
 4         /// <param name="zipfile">zip壓縮檔案</param>
 5         /// <param name="filenames">原始檔集合</param>
 6         /// <param name="password">壓縮加密</param>
 7         public void ZipFiles(string zipfile, string[] filenames, string password = "")
 8         {
 9             ZipOutputStream s = new ZipOutputStream(System.IO.File.Create(zipfile));
10 
11             s.SetLevel(6);
12 
13             if (password != "")
14                 s.Password = Md5Help.Encrypt(password);
15 
16             foreach (string file in filenames)
17             {
18                 //開啟壓縮檔案 
19                 FileStream fs = File.OpenRead(file);
20 
21                 byte[] buffer = new byte[fs.Length];
22                 fs.Read(buffer, 0, buffer.Length);
23 
24                 var name = Path.GetFileName(file);
25 
26                 ZipEntry entry = new ZipEntry(name);
27                 entry.DateTime = DateTime.Now;
28                 entry.Size = fs.Length;
29                 fs.Close();
30                 s.PutNextEntry(entry);
31                 s.Write(buffer, 0, buffer.Length);
32             }
33             s.Finish();
34             s.Close();
35         }

說明:21行,緩衝區大小直接為檔案大小,所以一次讀完,沒有迴圈讀寫。這種情況下,單個檔案不能太大,比如超過1G。14行,可以為壓縮包設定密碼,MD5的生成方法如下:

     public class Md5Help
    {
        /// <summary>
        ///32位 MD5加密
        /// </summary>
        /// <param name="str">加密字元</param>
        /// <returns></returns>
        public static string Encrypt(string str)
        {
            MD5 md5 = new MD5CryptoServiceProvider();

            byte[] encryptdata = md5.ComputeHash(Encoding.UTF8.GetBytes(str));

            return Convert.ToBase64String(encryptdata);
        }
    }

 

三、多檔案非同步壓縮

      上面同步的壓縮的前提是,假設檔案不大,而且檔案數不多,但是現實是,不光檔案大,而且檔案數比較多。這種情況,就要考慮非同步方法了。否則會阻塞主執行緒,就是我們平常說的卡死。

   

        /// <summary>
        /// 非同步壓縮檔案為zip壓縮包
        /// </summary>
        /// <param name="zipfile">壓縮包儲存路徑</param>
        /// <param name="filenames">檔案集合</param>
        public static async void ZipFilesAsync(string zipfile, string[] filenames)
        {
            await Task.Run(() =>
            {
                ZipOutputStream s = null;
                try
                {
                    s = new ZipOutputStream(System.IO.File.Create(zipfile));

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

                    foreach (string file in filenames)
                    {
                        //開啟壓縮檔案 
                        FileStream fs = System.IO.File.OpenRead(file);

                        var name = Path.GetFileName(file);
                        ZipEntry entry = new ZipEntry(name);
                        entry.DateTime = DateTime.Now;
                        entry.Size = fs.Length;
                        s.PutNextEntry(entry);

                        //如果檔案大於1G
                        long blockSize = 51200;

                        var size = (int)fs.Length;

                        var oneG = 1024 * 1024 * 1024;

                        if (size > oneG)
                        {
                            blockSize = oneG;
                        }
                        byte[] buffer = new byte[blockSize];

                        size = fs.Read(buffer, 0, buffer.Length);

                        s.Write(buffer, 0, size);

                        while (size < fs.Length)
                        {
                            int sizeRead = fs.Read(buffer, 0, buffer.Length);
                            s.Write(buffer, 0, sizeRead);
                            size += sizeRead;
                        }
                        s.Flush();
                        fs.Close();
                    }

                }
                catch (Exception ex)
                {
                    Console.WriteLine("非同步壓縮檔案出錯:" + ex.Message);
                }
                finally
                {
                    s?.Finish();
                    s?.Close();
                }
            });
        }

 

四、壓縮資料夾

    實際的應用當中,是檔案和資料夾一起壓縮,所以這種情況,就乾脆把要壓縮的東西全部放到一個資料夾,然後進行壓縮。

 主方法如下:

c#壓縮檔案
/// <summary>
        /// 非同步壓縮資料夾為zip壓縮包
        /// </summary>
        /// <param name="zipfile">壓縮包儲存路徑</param>
        /// <param name="sourceFolder">壓縮包儲存路徑</param>
        /// <param name="filenames">檔案集合</param>
        public static async void ZipFolderAsync(string zipfile, string sourceFolder, string[] filenames)
        {
            await Task.Run(() =>
            {
                ZipOutputStream s = null;
                try
                {
                    s = new ZipOutputStream(System.IO.File.Create(zipfile));

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

                    CompressFolder(sourceFolder, s, sourceFolder);
                }
                catch (Exception ex)
                {
                    Console.WriteLine("非同步壓縮檔案出錯:" + ex.Message);
                }
                finally
                {
                    s?.Finish();
                    s?.Close();
                }
            });
        }
View Code

壓縮的核心方法:

c#壓縮檔案
 1         /// <summary>
 2         /// 壓縮資料夾
 3         /// </summary>
 4         /// <param name="source">源目錄</param>
 5         /// <param name="s">ZipOutputStream物件</param>
 6         /// <param name="parentPath">和source相同</param>
 7         public static void CompressFolder(string source, ZipOutputStream s, string parentPath)
 8         {
 9             string[] filenames = Directory.GetFileSystemEntries(source);
10             foreach (string file in filenames)
11             {
12                 if (Directory.Exists(file))
13                 {
14                     CompressFolder(file, s, parentPath);  //遞迴壓縮子資料夾
15                 }
16                 else
17                 {
18                     using (FileStream fs = System.IO.File.OpenRead(file))
19                     {
20                         var writeFilePath = file.Replace(parentPath, "");
21                         ZipEntry entry = new ZipEntry(writeFilePath);
22                         entry.DateTime = DateTime.Now;
23                         entry.Size = fs.Length;
24 
25                         s.PutNextEntry(entry);
26 
27                         //如果檔案大於1G
28                         long blockSize = 51200;
29 
30                         var size = (int)fs.Length;
31 
32                         var oneG = 1024 * 1024 * 1024;
33 
34                         if (size > oneG)
35                         {
36                             blockSize = oneG;
37                         }
38                         byte[] buffer = new byte[blockSize];
39 
40                         size = fs.Read(buffer, 0, buffer.Length);
41 
42                         s.Write(buffer, 0, size);
43 
44 
45                         while (size < fs.Length)
46                         {
47                             int sizeRead = fs.Read(buffer, 0, buffer.Length);
48                             s.Write(buffer, 0, sizeRead);
49                             size += sizeRead;
50                         }
51 
52                         s.Flush();   //清除流的緩衝區,使得所有緩衝資料都寫入到檔案中
53                         fs.Close();
54                     }
55                 }
56             }
57         }
View Code

唯一需要注意的地方,可能解壓出來的目錄結構和壓縮前的檔案目錄不同,這時候檢查parentPath引數,它在ZipEntry實體new的時候用,替換絕對路徑為當前的相對路徑,也就是相對壓縮資料夾的路徑。

上面的方法比較複雜,還有一種相對簡單的方式,直接呼叫api:

      public static string ZipFolder(string sourceFolder, string zipFile)
        {
            string result = "";
            try
            {
                //建立壓縮包
                if (!Directory.Exists(sourceFolder)) return result = "壓縮資料夾不存在";

                DirectoryInfo d = new DirectoryInfo(sourceFolder);
                var files = d.GetFiles();
                if (files.Length == 0)
                {
                    //找子目錄
                    var ds = d.GetDirectories();
                    if (ds.Length > 0)
                    {
                        files = ds[0].GetFiles();
                    }
                }
                if (files.Length == 0) return result = "待壓縮檔案為空";
                System.IO.Compression.ZipFile.CreateFromDirectory(sourceFolder, zipFile);
            }
            catch (Exception ex)
            {
                result += "壓縮出錯:" + ex.Message;
            }
            return result;
        }

 

相關文章