使用SevenZipSharp压缩、解压文件
2016-11-11 09:51
1561 查看
安装
执行"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."。相关文章推荐
- 【转】使用SevenZipSharp压缩、解压文件
- C#:SevenZipSharp使用7z.dll来进行压缩与解压缩
- 文件压缩java使用Apache的ant压缩和解压文件(zip)
- Java文件压缩与解压之java.util.zip的使用
- 使用 ZLib 压缩/解压 ZIP 文件
- Qt使用QuaZip对文件进行压缩解压操作
- 在mvc3中使用Telerik的upload控件上传Zip压缩文件,服务器端解压逐个上传到数据库,使用Ionic.Zip
- 【VC++技术杂谈008】使用zlib解压zip压缩文件
- python脚本编程:使用zip或者tar压缩和解压文件
- .NET中zip的压缩和解压——SharpCompress插件的使用
- [置顶] 使用Python3解压gz、tar、tgz、zip、rar五种格式的压缩文件例子
- 使用ZLIB生成DLL文件,并进行有中文的ZIP文件压缩与解压操作!支持目录压与解!
- Qt使用QuaZIP 压缩/解压文件
- Qt使用QuaZip对文件进行压缩解压操作
- 使用apache的工具类进行zip(rar等压缩文件)的解压
- 使用 ZLib 压缩/解压 ZIP 文件
- 使用PHP对文件进行压缩解压(zip)
- 使用 ZLib 压缩/解压 ZIP 文件
- 在Linux中使用zip、tar压缩、解压文件
- Win10如何使用自带压缩、解压zip压缩文件功能