【轉】使用SevenZipSharp壓縮、解壓文件


引用

下載之后引用“SevenZipSharp.dll”至項目中,然后將“7z.dll”放到bin目錄下,或者這樣引用:SevenZipCompressor.SetLibraryPath("c:\\7z.dll");//引用7Z.dll

因為SevenZipSharp本身無法獨立工作,需要使用7z.dll來配合工作。

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."。
 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM