2017-04-18 213 views
1

我正在尋找一種快速的方式來創建一個包含大量小文件(例如25.4 MB,8目錄和4505文件,但可能更大)的目錄的.zip存檔。SevenZipSharp快速壓縮

當我使用標準7zip安裝(通過上下文菜單)壓縮需要1到2秒。

當我使用SevenZipSharp庫中的SevenZipCompressor在C#應用程序中執行相同操作時,它需要更長的時間(> 5秒)。現在我想知道7zip使用的默認參數是什麼,如何在代碼中設置它們以達到相同的速度?

對於我的應用程序,壓縮級別不像速度那麼重要。

這裏是我的代碼(我嘗試不同的壓縮級別和模式,但沒有顯著差異):

public Compressor() 
{ 
    var zipFile = @"pathTo7ZipDll\7z.dll"; 
    if (File.Exists(zipFile)) 
    { 
    SevenZipBase.SetLibraryPath(zipFile); 
    } 
    else 
    { 
    throw new ApplicationException("seven zip dll file not found!"); 
    } 

    Zipper = new SevenZipCompressor 
    { 
    ArchiveFormat = OutArchiveFormat.Zip, 
    DirectoryStructure = true, 
    PreserveDirectoryRoot = true, 
    CompressionLevel = CompressionLevel.Fast, 
    CompressionMethod = CompressionMethod.Deflate 
    }; 


    Zipper.FileCompressionStarted += (s, e) => 
    { 
    if (IsCancellationRequested) 
    { 
     e.Cancel = true; 
    } 
    }; 

    Zipper.Compressing += (s, e) => 
    { 
    if (IsCancellationRequested) 
    { 
     e.Cancel = true; 
     return; 
    } 

    if (e.PercentDone == 100) 
    { 
     OnFinished(); 
    } 
    else 
    { 
     Console.WriteLine($"Progress received: {e.PercentDone}."); 
    } 
    }; 

    Zipper.CompressionFinished += (s, e) => 
    { 
    OnFinished(); 
    }; 
} 

private void OnFinished() 
{ 
    IsProcessing = false; 
    IsCancellationRequested = false; 
} 

public void StartCompression() 
{ 
    IsProcessing = true; 
    Zipper.CompressDirectory(InputDir, OutputFilePath); 
} 

原始目錄的大小26.678.577字節。

用c#代碼創建的壓縮.zip是25.786.743字節。

使用7zip安裝創建的壓縮.zip爲25.771.350字節。

我也嘗試使用BeginCompressDirectory而不是CompressDirectory,但這根本不起作用。它立即返回,沒有事件被觸發,只創建一個空的歸檔。

+0

嘗試'Zipper.CustomParameters.Add(「mt」,「on」);'告訴它使用多個線程。 –

+0

不幸的是,這並沒有什麼區別。 – tabina

+0

我的C#代碼的壓縮需要6167毫秒。當我添加多線程選項時,它是6289毫秒。 – tabina

回答

0

將生成的存檔文件與您的代碼的大小相比較,通過上下文菜單生成的文件大小。代碼中的過程花費更長時間,會產生更小的文件。另外,請確認您得到的壓縮比率,因爲不清楚原始文件的可壓縮程度。

+0

我添加了文件大小。 – tabina

+0

文件大小相似,壓縮率低,所以不幸的是沒有更多的信息。您是否嘗試過其他CompressionMethod選項,例如'Zipper.CompressionMethod = CompressionMethod.Lzma' – Snympi

+0

是的,我嘗試了不同的方法。他們都需要相當長的時間。對於Lzma我甚至沒有得到任何壓縮事件。 – tabina