详解C#压缩、解压文件夹/文件(带密码)

这篇文章主要给大家介绍了关于C#压缩、解压文件夹/文件(带密码)的相关资料,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们下面随着小编来一起学习学习吧

前言

今天梳理一下项目中用到的压缩、解压文件夹或文件的方法,发现因为需求不同,已经用了好几个不同组件。今天就好好整理记录下,别下次遇到需求又重头开始了。

DotNetZip

DotNetZip是一个开源的免费类库,主要提供了快速操作zip文件的工具集,VB、C#任何.Net语言都可以通过它创建、解压缩zip文件。我使用该类库最主要的目的还是因为它可以创建带密码保护的压缩文件。

只有设置了zip.Password = "password"之后,被压缩的文件才会有密码保护

 ///  /// 压缩文件/文件夹 ///  /// 需要压缩的文件/文件夹路径 /// 压缩文件路径(zip后缀) /// 密码 /// 需要过滤的文件后缀名 public static void CompressionFile(string filePath, string zipPath, string password = "", List filterExtenList = null) { try { using (ZipFile zip = new ZipFile(Encoding.UTF8)) { if (!string.IsNullOrWhiteSpace(password)) { zip.Password = password; } if (Directory.Exists(filePath)) { if (filterExtenList == null) zip.AddDirectory(filePath); else AddDirectory(zip, filePath, filePath, filterExtenList); } else if (File.Exists(filePath)) { zip.AddFile(filePath,""); } zip.Save(zipPath); } } catch (Exception ex) { throw ex; } } ///  /// 添加文件夹 ///  /// ZipFile对象 /// 需要压缩的文件夹路径 /// 根目录路径 /// 需要过滤的文件后缀名 public static void AddDirectory(ZipFile zip, string dirPath, string rootPath, List filterExtenList) { var files = Directory.GetFiles(dirPath); for (int i = 0; i  Path.GetExtension(files[i]).Contains(d, StringComparison.OrdinalIgnoreCase)))) { //获取相对路径作为zip文件中目录路径 zip.AddFile(files[i], Path.GetRelativePath(rootPath, dirPath)); //如果没有Path.GetRelativePath方法,可以用下面代码替换 //string relativePath = Path.GetFullPath(dirPath).Replace(Path.GetFullPath(rootPath), ""); //zip.AddFile(files[i], relativePath); } } var dirs = Directory.GetDirectories(dirPath); for (int i = 0; i 

SharpCompress

SharpCompress是用到现在,感觉功能最强大的压缩、解压开源插件。它支持处理zip、rar、7z等多种格式的压缩文件,使用方式也很简单。当然,最让我难受的是创建压缩文件的时候没法设置密码~所以才有了上面DotnetZip的代码。

SharpCompress版本不同,设置ArchiveEncoding的方式也不同,默认设置了UTF8防止解压乱码。
通过设置ArchiveType切换生成不同格式压缩文件

 ///  /// 压缩文件/文件夹 ///  /// 需要压缩的文件/文件夹路径 /// 压缩文件路径(zip后缀) /// 需要过滤的文件后缀名 public static void CompressionFile(string filePath, string zipPath, List filterExtenList = null) { try { using (var zip = File.Create(zipPath)) { var option = new WriterOptions(CompressionType.Deflate) { ArchiveEncoding = new SharpCompress.Common.ArchiveEncoding() { Default = Encoding.UTF8 } }; using (var zipWriter = WriterFactory.Open(zip, ArchiveType.Zip, option)) { if (Directory.Exists(filePath)) { //添加文件夹 zipWriter.WriteAll(filePath, "*", (path) => filterExtenList == null ? true : !filterExtenList.Any(d => Path.GetExtension(path).Contains(d, StringComparison.OrdinalIgnoreCase)), SearchOption.AllDirectories); } else if (File.Exists(filePath)) { zipWriter.Write(Path.GetFileName(filePath), filePath); } } } } catch (Exception ex) { throw ex; } } 
 ///  /// 解压文件 ///  /// 压缩文件路径 /// 解压到文件夹路径 /// 密码 public static void DeCompressionFile(string zipPath, string dirPath, string password = "") { if (!File.Exists(zipPath)) { throw new ArgumentNullException("zipPath压缩文件不存在"); } Directory.CreateDirectory(dirPath); try { using (Stream stream = File.OpenRead(zipPath)) { var option = new ReaderOptions() { ArchiveEncoding = new SharpCompress.Common.ArchiveEncoding() { Default = Encoding.UTF8 } }; if (!string.IsNullOrWhiteSpace(password)) { option.Password = password; } var reader = ReaderFactory.Open(stream, option); while (reader.MoveToNextEntry()) { if (reader.Entry.IsDirectory) { Directory.CreateDirectory(Path.Combine(dirPath, reader.Entry.Key)); } else { //创建父级目录,防止Entry文件,解压时由于目录不存在报异常 var file = Path.Combine(dirPath, reader.Entry.Key); Directory.CreateDirectory(Path.GetDirectoryName(file)); reader.WriteEntryToFile(file); } } } } catch (Exception ex) { throw ex; } } 

总结

相似的插件还有SharpZipLib(支持更多的压缩格式)、SevenZipSharp(专注处理7z格式压缩文件)等,它们也都有各自的优缺点。但总的来说,上面的两个组件已经满足日常工作中的大部分需求,遇到相同问题的朋友可以参考下~

以上就是详解C#压缩、解压文件夹/文件(带密码)的详细内容,更多请关注0133技术站其它相关文章!

赞(0) 打赏
未经允许不得转载:0133技术站首页 » 其他教程