首页 > 其他分享 >.NET 压缩/解压文件

.NET 压缩/解压文件

时间:2024-09-06 21:39:18浏览次数:2  
标签:解压 文件 string zip 压缩 zipFile new NET

本文为大家介绍下.NET解压/压缩zip文件。虽然解压缩不是啥核心技术,但压缩性能以及进度处理还是需要关注下,针对使用较多的zip开源组件验证,给大家提供技术选型

之前在《.NET WebSocket高并发通信阻塞问题 - 唐宋元明清2188 - 博客园 (cnblogs.com)》讲过,团队遇到Zip文件解压进度频率过高问题,也在这里顺带讲下解决方法

目前了解到的常用技术方案有System.IO.Compression、SharpZipLib以及DotNetZip,下面我们分别介绍下使用以及性能

System.IO.Compression

如果你需要处理简单的ZIP压缩和解压任务,且不需要高级特性,建议使用System.IO.Compression。作为.NET标准库的一部分,不需要额外安装第三方库,而且会随着.NET平台的更新而更新

看下代码实现:

 1     /// <summary>
 2     /// 解压Zip文件
 3     /// </summary>
 4     /// <param name="filePath">zip文件路径</param>
 5     /// <param name="outputFolder">解压目录</param>
 6     /// <returns></returns>
 7     public static void Decompress(string filePath, string outputFolder)
 8     {
 9         ZipFile.ExtractToDirectory(filePath, outputFolder);
10     }
11 
12     /// <summary>
13     /// 压缩成Zip文件
14     /// </summary>
15     /// <param name="sourceFolder">文件目录</param>
16     /// <param name="zipFile">zip文件路径</param>
17     /// <param name="includeFolder">是否包含文件父目录(即sourceFolder本身)</param>
18     /// <returns></returns>
19     public static void Compress(string sourceFolder, string zipFile, bool includeFolder = true)
20     {
21         ZipFile.CreateFromDirectory(sourceFolder, zipFile, CompressionLevel.Fastest, includeFolder);
22     }

优点很明显,API简洁易懂,适用于简单的文件压缩和解压操作。当然提供的功能比较基础,缺乏一些高级特性,比如分卷压缩和加密,也提供不了操作详细进度

我们来测试下解压缩性能,找个zip文件,“智微工厂生产需要的固件及安装包.zip”文件大小847M,里面是如下结构有文件以及文件夹:

解压耗时:8484ms。再将解压后的文件夹压缩,耗时:28672ms。性能整体上还是不错的,特别是解压很优秀
所以呢,比较简单的业务场景可以直接用这个方案。大家可以将这个方案放在公司通用基础技术组件里

SharpZipLib

支持多种压缩格式(如ZIP、TAR、GZIP、BZIP2等),并提供了高级功能如加密、分卷压缩等。icsharpcode/SharpZipLib: #ziplib is a Zip, GZip, Tar and BZip2 library written entirely in C# for the .NET platform. (github.com)

API设计可用性高,满足更多复杂定制化需求。社区里好多小伙伴在使用,开发历史久远、组件稳定性较高

引用下Nuget包SharpZipLib后,解压zip文件

获取压缩包压缩后的文件的大小,这里Size是压缩前大小,还有一个属性CompressedSize压缩后大小:

 1         public static long GetZipFileTotalSize(string zipPath)
 2         {
 3             long totalSize = 0;
 4             using FileStream fileStream = File.OpenRead(zipPath);
 5             using ZipInputStream zipStream = new ZipInputStream(fileStream);
 6             while (zipStream.GetNextEntry() is { } zipEntry)
 7             {
 8                 totalSize += zipEntry.Size;
 9             }
10 
11             return totalSize;
12         }

解压Zip文件:

 1       /// <summary>
 2       /// 解压Zip文件
 3       /// </summary>
 4       /// <param name="zipFile">zip文件路径</param>
 5       /// <param name="outputFolder">解压目录</param>
 6       /// <param name="cancellationToken">取消操作</param>
 7       /// <param name="progressChanged">解压进度回调</param>
 8       /// <returns></returns>
 9       public static async Task UnZipAsync(string zipFile, string outputFolder,
10           CancellationToken cancellationToken = default, Action<ZipProgress> progressChanged = null)
11       {
12           if (!File.Exists(zipFile))
13           {
14               throw new InvalidOperationException($"file not exist,{zipFile}");
15           }
16           var decompressLength = GetZipFileTotalSize(zipFile);
17           using FileStream fileStream = File.OpenRead(zipFile);
18           await Task.Run(() =>
19           {
20               using ZipInputStream zipStream = new ZipInputStream(fileStream);
21               long completedSize = 0;
22               while (zipStream.GetNextEntry() is { } zipEntry)
23               {
24                   if (cancellationToken != default && cancellationToken.IsCancellationRequested)
25                   {
26                       cancellationToken.ThrowIfCancellationRequested();
27                   }
28 
29                   if (zipEntry.IsDirectory)
30                   {
31                       string folder = Path.Combine(outputFolder, zipEntry.Name);
32                       EnsureFolder(folder);
33                   }
34                   else if (zipEntry.IsFile)
35                   {
36                       var operatingSize = completedSize;
37                       var zipEntryName = zipEntry.Name;
38                       string fullEntryPath = Path.Combine(outputFolder, zipEntryName);
39                       string dirPath = Path.GetDirectoryName(fullEntryPath);
40                       EnsureFolder(dirPath);
41                       //解压后的数据
42                       long singleFileSize = WriteUnzipDataToFile(zipStream, fullEntryPath, partialFileSize =>
43                       {
44                           if (progressChanged == null)
45                           {
46                               return;
47                           }
48                           long currentSize = operatingSize + partialFileSize;
49                           progressChanged.Invoke(new ZipProgress(currentSize, decompressLength, zipEntryName));
50                       });
51                       completedSize += singleFileSize;
52                   }
53               }
54           }, cancellationToken);
55       }

解压进度能反馈详细的文件写入进度值。另外,这里有个文件夹判断处理,也是支持空文件夹的

Zip压缩,获取所有的文件夹/子文件夹、所有的文件,添加到ZipFile里保存:

 1       /// <summary>
 2       /// 压缩文件
 3       /// </summary>
 4       /// <param name="toZipDirectory">待压缩的文件夹</param>
 5       /// <param name="destZipPath">Zip文件的保存路径</param>
 6       /// <returns></returns>
 7       public static bool Zip(string toZipDirectory, string destZipPath)
 8       {
 9           if (string.IsNullOrEmpty(destZipPath))
10           {
11               throw new ArgumentNullException(nameof(destZipPath));
12           }
13           if (!destZipPath.ToUpper().EndsWith(".ZIP"))
14           {
15               throw new ArgumentException("保存路径不是ZIP后缀", nameof(destZipPath));
16           }
17           if (!Directory.Exists(toZipDirectory))
18           {
19               throw new ArgumentException("待压缩的文件夹不存在", nameof(toZipDirectory));
20           }
21 
22           var dirs = Directory.GetDirectories(toZipDirectory, "*", SearchOption.AllDirectories)
23               .Select(dir => PathUtils.GetRelativePath(toZipDirectory, dir));
24           var files = Directory.GetFiles(toZipDirectory, "*", SearchOption.AllDirectories).ToArray();
25           var destFiles = files.Select(file => PathUtils.GetRelativePath(toZipDirectory, file)).ToArray();
26           if (File.Exists(destZipPath))
27           {
28               File.Delete(destZipPath);
29           }
30           using (ZipFile zipFile = ZipFile.Create(destZipPath))
31           {
32               zipFile.BeginUpdate();
33               foreach (var dir in dirs)
34               {
35                   zipFile.AddDirectory(dir);
36               }
37               for (int i = 0; i < files.Length; i++)
38               {
39                   zipFile.Add(files[i], destFiles[i]);
40               }
41               zipFile.CommitUpdate();
42           }
43           return true;
44       }

值得一提的是,如有需要指定Zip压缩文件内的文件名以及文件路径,可以在文件时输入对应的压缩后路径定义,注意是指压缩包内的相对路径:

 1       /// <summary>指定的文件压缩到对应的压缩文件中</summary>
 2       /// <param name="files">待压缩的文件路径列表(绝对路径)</param>
 3       /// <param name="destFiles">文件路径对应的压缩后路径列表,即压缩后压缩包内的文件路径</param>
 4       /// <param name="destZipPath">Zip文件的保存路径</param>
 5       public static bool Zip(List<string> files, List<string> destFiles, string destZipPath)
 6       {
 7           if (files.Count != destFiles.Count)
 8           {
 9               throw new ArgumentException($"{nameof(files)}与{nameof(destFiles)}文件列表数量不一致");
10           }
11           if (string.IsNullOrEmpty(destZipPath))
12               throw new ArgumentNullException(nameof(destZipPath));
13           using (ZipFile zipFile = ZipFile.Create(destZipPath))
14           {
15               zipFile.BeginUpdate();
16               for (int i = 0; i < files.Count; i++)
17               {
18                   zipFile.Add(files[i], destFiles[i]);
19               }
20               zipFile.CommitUpdate();
21           }
22           return true;
23       }

SharpZipLib虽然功能丰富,但大家看上面的demo代码,接口搞的有点复杂、学习曲线较高
同样我们按上面测试操作,解压缩同一zip文件,解压耗时20719ms,压缩耗时102109ms。。。

DotNetZip

再看看DotNetZip,这个相对SharpZipLib,API设计的更友好、容易上手。官网是haf/DotNetZip.Semverd(github.com),它停止维护了。。。作者推荐大家去使用System.IO.Compression!好吧先忽略这个,尽管已不再积极维护,但稳定性、性能真的好,下面给大家列下使用demo和性能测试

 Zip文件解压:

 1     /// <summary>
 2     /// 解压Zip文件
 3     /// </summary>
 4     /// <param name="zipFile">zip文件路径</param>
 5     /// <param name="outputFolder">解压目录</param>
 6     /// <param name="password">密码</param>
 7     /// <param name="progressChanged">解压进度回调</param>
 8     /// <returns></returns>
 9     public static void UnZip(string zipFile, string outputFolder, string password, Action<ZipProgress> progressChanged)
10     {
11         if (!File.Exists(zipFile)) throw new InvalidOperationException($"file not exist,{zipFile}");
12         //获取文件解压后的大小
13         var totalZipSize = GetZipFileSize(zipFile);
14         long completedSize = 0L;
15         using (var zip = ZipFile.Read(zipFile))
16         {
17             zip.Password = password;
18             zip.ExtractProgress += (s, e) =>
19             {
20                 if (e.EventType == ZipProgressEventType.Extracting_EntryBytesWritten)
21                 {
22                     var fileName = e.CurrentEntry.FileName;
23                     if (e.BytesTransferred < e.TotalBytesToTransfer)
24                     {
25                         //单个文件解压中的进度
26                         var operatingSize = completedSize + e.BytesTransferred;
27                         progressChanged?.Invoke(new ZipProgress(operatingSize, totalZipSize, fileName));
28                     }
29                     else
30                     {
31                         //单个文件解压完全的进度
32                         completedSize += e.TotalBytesToTransfer;
33                         progressChanged?.Invoke(new ZipProgress(completedSize, totalZipSize, fileName));
34                     }
35                 }
36             };
37             zip.ExtractAll(outputFolder);
38         }
39     }

这里获取压缩后文件大小,与上面SharpZipLib的zipEntry.Size对应,取的是zipEntry.UncompressedSize

非常人性的提供了ExtractProgress事件进度,我们取的是Extracting_EntryBytesWritten类型,可以拿到细节进度。具体进度的处理看上方代码

因为反馈的是详细字节写入进度,所以间隔很短。。。1ms都能给你爆几次进度,尤其是大文件:

所以需要限制下回调Action触发,可以加个计时器限制单个文件的进度回调,如100ms内最多触发一次,下面是优化后的代码:

 1     /// <summary>
 2     /// 解压Zip文件
 3     /// </summary>
 4     /// <param name="zipFile">zip文件路径</param>
 5     /// <param name="outputFolder">解压目录</param>
 6     /// <param name="password">密码</param>
 7     /// <param name="progressChanged">解压进度回调</param>
 8     /// <returns></returns>
 9     public static void UnZip(string zipFile, string outputFolder, string password,
10         Action<ZipProgress> progressChanged)
11     {
12         if (!File.Exists(zipFile)) throw new InvalidOperationException($"file not exist,{zipFile}");
13         //获取文件解压后的大小
14         var totalZipSize = GetZipFileSize(zipFile);
15         long completedSize = 0L;
16         using (var zip = ZipFile.Read(zipFile))
17         {
18             zip.Password = password;
19             var lastProgressTick = Environment.TickCount;
20             zip.ExtractProgress += (s, e) =>
21             {
22                 if (e.EventType == ZipProgressEventType.Extracting_EntryBytesWritten)
23                 {
24                     var fileName = e.CurrentEntry.FileName;
25                     if (e.BytesTransferred < e.TotalBytesToTransfer)
26                     {
27                         // 单个文件解压变化,限制间隔时间触发解压事件
28                         if (Environment.TickCount - lastProgressTick < ProgressEventTick)
29                         {
30                             return;
31                         }
32                         lastProgressTick = Environment.TickCount;
33                         //单个文件解压中的进度
34                         var operatingSize = completedSize + e.BytesTransferred;
35                         progressChanged?.Invoke(new ZipProgress(operatingSize, totalZipSize, fileName));
36                     }
37                     else
38                     {
39                         //重置计时器
40                         lastProgressTick = Environment.TickCount;
41                         //单个文件解压完全的进度
42                         completedSize += e.TotalBytesToTransfer;
43                         progressChanged?.Invoke(new ZipProgress(completedSize, totalZipSize, fileName));
44                     }
45                 }
46             };
47             zip.ExtractAll(outputFolder);
48         }
49     }

再看看Zip压缩:

 1     public static void Zip(string sourceFolder, string destZipFile, string password,
 2         Action<ZipProgress> zipProgressAction)
 3     {
 4         if (string.IsNullOrEmpty(destZipFile)) throw new ArgumentNullException(nameof(destZipFile));
 5         if (!destZipFile.ToUpper().EndsWith(".ZIP")) throw new ArgumentException("保存路径不是Zip文件", destZipFile);
 6         if (File.Exists(destZipFile)) File.Delete(destZipFile);
 7 
 8         using (var zipFile = new ZipFile())
 9         {
10             // 设置压缩进度事件处理程序
11             zipFile.SaveProgress += (sender, e) =>
12             {
13                 if (e.EventType == ZipProgressEventType.Saving_AfterWriteEntry)
14                     zipProgressAction?.Invoke(new ZipProgress(e.EntriesSaved, e.EntriesTotal, e.CurrentEntry.FileName));
15             };
16             zipFile.AddDirectory(sourceFolder);
17             zipFile.Password = password;
18             zipFile.Save(destZipFile);
19         }
20     }

如果不考虑加密、压缩进度,DotNetZip压缩zip文件只需要几行代码,所以是相当的易学易用、入手快

还是同一个847M的zip文件,测试下解压缩性能,解压11907ms,压缩耗时16282ms,用数据说话性能强不强

用表格把这三个方案的对比列下:

所以如果你需要处理简单的ZIP压缩和解压任务,且不需要高级特性,建议使用System.IO.Compression

需要考虑解压缩性能比如公司的大文件OTA功能,需要减少业务的处理时间,推荐使用DotNetZip。DotNetZip也能提供高级特性,进度显示等。至于停止维护的状况可以忽然,有BUG大家可以在公司内或者github维护下这个组件代码

 

标签:解压,文件,string,zip,压缩,zipFile,new,NET
From: https://www.cnblogs.com/kybs0/p/18398891

相关文章

  • What is Convolutional Neural Network(CNN)?
    笔记核心部分摘抄以及自己的理解[附有样例,可以轻松理解]:卷积网络的卷积层中使用了卷积操作,这个操作可以捕捉到图像中的局部特征而不受其位置的影响。在外围边缘补充若干圈0,方便从初始位置以步长为单位可以刚好滑倒末尾位置,通俗地讲就是为了总长能被步长整除。池化层通过减......
  • D20 kubernetes 工作负载资源对象-Deployment
    1、Deployment简介 Deployment是kubernetes中最常用的工作负载资源,具有以下特点和功能-副本管理:确保指定数量的pod副本在集群中运行。如果pod副本数小于期望值,则会自动创建pod;如果pod的副本数多余期望值,则删除多余的pod-滚动更新:采用滚动更新策略,逐步进行新旧版本pod的替换......
  • 驱动下载后第一步解压缩
    驱动解压教程在官网下载好驱动后我们第一步也是必要的一步,全部解压缩比如文件名是zip,rar,7z等常见的压缩包类型的,都需要先解压缩如果不解压缩直接打开里面的文件大概率都是用不了了的一般常见的压缩包windows都可以解压缩,没有显示可以解压缩的需要下载第三方软件进行解压缩......
  • asp.net中配置文件如何注册引用
    配置文件如何注册引用注册配置文件的形式注册方式//第一种注册方式:注册配置文件builder.Services.Configure<AppSettings>(builder.Configuration.GetSection("AppSettings"));AppSettingsappSettings=builder.Configuration.GetSection(AppSettings.Key).Get<AppSettin......
  • 解压zip文件或者安装软件出现 "无法成功完成操作" 解决方案
    近期,有一些朋友反馈在解压zip压缩包,或者在安装软件的过程中出现了下面的错误提示:"无法成功完成操作, 因为文件包含病毒或者潜在垃圾文件""Operation didnotcompletesuccessfullybecausethefilecontainsavirusorpotentiallyunwantedsoftware"    ......
  • 如何将源码压缩后发布到 GitHub 或其他平台
    在软件开发过程中,源码的管理和发布是非常关键的环节。特别是在需要对源码进行共享、分发或归档时,压缩和上传源码到平台如GitHub、GitLab、或其他云存储服务上是常见的做法。为什么需要压缩源码在一些场景下,开发者可能需要将源码进行压缩后发布,例如:1.文件体积较大:项目文件......
  • JAVA网络编程之InetAddress 和 SocketAddress 的区别
    InetAddress和SocketAddress是Java网络编程中常用的类,用于处理网络连接中的地址信息。1.InetAddressInetAddress用于表示IP地址,既可以是IPV4也可以是IPV6。它可以用来获取主机的IP地址,或根据IP地址查找主机名。InetAddress是抽象类,常用的有两个子类:Inet4Address和I......
  • NetCore MVC Controller和View之间传递数据
    方式一:使用@Model1.在Controller中定义变量:publicclassMyController:Controller{publicstringMyModelVariable{get;set;}publicIActionResultIndex(){MyModelVariable="Hello,World!";returnView(this);}} ......
  • Kubernetes学习指南:保姆级实操手册07——calico安装、metric-server安装
    七、Kubernetes学习指南:保姆级实操手册07——calico安装、metric-server安装一、calicoCalico是一个开源的虚拟化网络方案,支持基础的Pod网络通信和网络策略功能。官方文档:https://projectcalico.docs.tigera.io/getting-started/kubernetes/quickstart1、calico安装###在Master控......
  • D19 kubernetes 工作负载资源对象
    在kubernetes中,通常不会直接创建pod。都是使用工作负载资源deployment、statefulset来创建和管理pod。这种方式简化了pod的管理,提供了诸多如多副本、滚动和更新、回滚和自动扩展等告警功能,使得部署和管理应用程序变得方便和搞笑工作负载资源概述工作负载资源是kubernetes......