使用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