c#压缩文件

话说当今压缩市场三足鼎立,能叫上名号的有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();
                }
            });
        }

四、压缩文件夹

    实际的应用当中,是文件和文件夹一起压缩,所以这种情况,就干脆把要压缩的东西全部放到一个文件夹,然后进行压缩。

 主方法如下:

/// <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

压缩的核心方法:

 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;
        }
原文地址:https://www.cnblogs.com/wangqiang3311/p/14924111.html