2016-07-22 60 views
0

我正在開發一個C#中的小工具,我在其中插入許多壓縮文件。這些文件有不同的擴展名(例如.zip,.rar,.tar,.uue)現在我不想提取這些文件,但我只想檢查這些文件是否受密碼保護。如何檢查壓縮文件(所有格式 - zip/rar/tar/uue)是否受密碼保護,而不是在c#中解壓縮?

我已經使用DotNetZip.dll的擴展名爲.zip的文件,它工作正常。我發現奇爾卡特DLL爲.rar文件。

任何人都可以爲我提供其他擴展的DLL或更好的解決方案爲所有壓縮文件?先謝謝你。

+0

你也可以使用SharpZipLib,它是寫在C#for .NET平臺,並可與大多數壓縮格式一起使用。 https://www.nuget.org/packages/SharpZipLib/ – Maverick

回答

1

使用SharpZipLib,以下代碼工作。而通過作品,我的意思是entry.IsCrypted根據是否存在壓縮文件中第一個條目的密碼來返回true或false。

var file = @"c:\testfile.zip"; 
FileStream fileStreamIn = new FileStream(file, FileMode.Open, FileAccess.Read); 
ZipInputStream zipInStream = new ZipInputStream(fileStreamIn); 
ZipEntry entry = zipInStream.GetNextEntry(); 
Console.WriteLine("IsCrypted: " + entry.IsCrypted); 

有一個關於在CodeProject上使用SharpZipLib的簡單教程。

這樣一個簡單的實現看起來是這樣的:

public static bool IsPasswordProtectedZipFile(string path) 
{ 
    using (FileStream fileStreamIn = new FileStream(path, FileMode.Open, FileAccess.Read)) 
    using (ZipInputStream zipInStream = new ZipInputStream(fileStreamIn)) 
    { 
     ZipEntry entry = zipInStream.GetNextEntry(); 
     return entry.IsCrypted; 
    } 
} 

注意有沒有真正的錯誤處理或什麼...

裁判:How to validate that a file is a password protected ZIP file, using C#

+0

這適用於.zip擴展名。我想要.rar,.tar,.uue擴展名的解決方案。 – Divya

+1

只是將所有類型或格式更改爲zip,然後再次檢查更改爲舊的擴展程序祝你好運 – sms247

+0

這些文件是非常重要的,我不能玩他們的擴展。 :) – Divya

相關問題