使用SevenZipSharp压缩、解压文件
来源:互联网 发布:软件可否申请专利 编辑:程序博客网 时间:2024/06/06 05:34
安装
执行"Install-Package SevenZipSharp"即可安装。它本身无法独立工作,需要使用7z.dll来配合工作。作者只提供了net20的lib文件。
压缩文件
class Program { static void Main(string[] args) { // 指定7z动态库文件路径,默认是"7z.dll" SevenZipBase.SetLibraryPath("7za.dll"); var compressor = new SevenZipCompressor(); // 可以在构造时指定临时文件夹 //var compressor = new SevenZipCompressor("Temp"); // 打印临时文件夹路径 Console.WriteLine(compressor.TempFolderPath); // 设置压缩等级 compressor.CompressionLevel = CompressionLevel.Normal; // 指定压缩包格式,默认为7z。 // 如果使用的7za.dll则只能使用7z格式。 compressor.ArchiveFormat = OutArchiveFormat.SevenZip; // 是否保持目录结构,默认为true。 compressor.DirectoryStructure = true; // 是否包含空目录,默认true。 compressor.IncludeEmptyDirectories = true; // 压缩目录时是否使用顶层目录,默认false compressor.PreserveDirectoryRoot = false; // 加密7z头,默认false compressor.EncryptHeaders = false; // 文件加密算法 compressor.ZipEncryptionMethod = ZipEncryptionMethod.ZipCrypto; // 尽快压缩(不会触发*Started事件,仅触发*Finished事件) compressor.FastCompression = false; // 单个文件开始压缩 compressor.FileCompressionStarted += (sender, eventArgs) => { Console.WriteLine($"正在压缩:{eventArgs.FileName}"); Console.WriteLine($"进度:{eventArgs.PercentDone}%"); }; // 单个文件压缩完成时 compressor.FileCompressionFinished += (sender, eventArgs) => { Console.WriteLine("FileCompressionFinished"); }; compressor.Compressing += (sender, eventArgs) => { Console.WriteLine(eventArgs.PercentDelta); Console.WriteLine(eventArgs.PercentDone); }; // 压缩完成 compressor.CompressionFinished += (sender, eventArgs) => { Console.WriteLine("CompressionFinished"); }; // 添加文件 var baseDir = AppDomain.CurrentDomain.BaseDirectory; var files = new string[] {$"{baseDir}123.txt", $"{baseDir}456.txt"}; compressor.CompressFiles("Files.7z", files); // 添加目录下的所有文件 compressor.CompressDirectory($"{baseDir}Dir", "Dir.7z"); Console.ReadKey(); } }
解压缩
class Program { static void Main(string[] args) { // 指定7z动态库文件路径,默认是"7z.dll" SevenZipBase.SetLibraryPath("7za.dll"); var extractor = new SevenZipExtractor("Dir.7z"); extractor.Extracting += (sender, eventArgs) => { Console.WriteLine("OnExtracting"); }; // 文件数量(文件夹也算) Console.WriteLine(extractor.FilesCount); // 压缩包字节数 Console.WriteLine(extractor.PackedSize); // 压缩文件属性 foreach (var fileProp in extractor.ArchiveProperties) { Console.WriteLine(fileProp.ToString()); } // 遍历文件名 foreach (var fileName in extractor.ArchiveFileNames) { Console.WriteLine(fileName); } // 遍历文件数据 foreach (var fileData in extractor.ArchiveFileData) { Console.WriteLine(fileData.ToString()); } // 是否使用的固实压缩 Console.WriteLine(extractor.IsSolid); // 解压后的大小 Console.WriteLine(extractor.UnpackedSize); extractor.ExtractArchive("Unpack"); Console.ReadKey(); } }
注意事项
C#工程的默认目标平台是Any CPU,也就是根据系统决定程序是x86还是x64,所以在设置7z.dll的时候要注意使用架构相同的dll文件,否则你会收到一个错误提示"Can not load 7-zip library or internal COM error! Message: DLL file does not exist."。
0 0
- 使用SevenZipSharp压缩、解压文件
- C# SevenZipSharp压缩和解压文件数量大的时候会很慢
- 使用zlib压缩/解压文件
- 使用C#压缩解压文件
- 使用C#进行文件压缩和解压
- 使用C#进行文件压缩和解压
- 使用 ZLib 压缩/解压 ZIP 文件
- 使用InfoZip压缩解压zip文件
- 使用 ZLib 压缩/解压 ZIP 文件
- 使用java.util.zip压缩、解压文件
- 使用开源库zlib压缩和解压文件
- 使用 ZLib 压缩/解压 ZIP 文件
- 使用 ZLib 压缩/解压 ZIP 文件
- 使用java解压或者压缩zip文件
- 使用开源库zlib压缩和解压文件
- 使用 ZLib 压缩/解压 ZIP 文件
- 使用开源库zlib压缩和解压文件
- IOS使用ZipArchive压缩解压文件
- 题目:判断101-200之间有多少个素数,并输出所有素数。
- Kendo UI开发教程:Kendo UI模板概述
- SQL视图
- 2016.11.11 js对象(RegExp 事件)
- C# ListView用法详解 很完整
- 使用SevenZipSharp压缩、解压文件
- jquery 两种方法设置disabled属性
- Exsi5.0在虚拟机中安装时提示Intel VT不支持解决办法
- Laravel与Repository Pattern(仓库模式)——概念篇
- ndk编译出现的问题
- CentOS6.5下安装mysql(glibc包安装)
- Mybatis源码分析(二)- SqlSessionFactory和SqlSession详解
- ImageLoader类
- JDBC学习(6)工厂模式与单例模式