在vb.net中测试zip密码的正确性
我想测试一个zip在vb.net中是否有特定的密码。我如何创建一个函数,比如在vb.net中测试zip密码的正确性,vb.net,zip,Vb.net,Zip,我想测试一个zip在vb.net中是否有特定的密码。我如何创建一个函数,比如check\u if\u zip\u pass(file,pass)作为布尔值 在.net框架中,我似乎找不到任何已经做到这一点的东西,除非我遗漏了一些非常明显的东西 此方法不应提取文件,仅在尝试传递有效时返回True,否则返回False。使用第三方库,如。请记住,zipfiles中的密码应用于条目,而不是整个zip文件。所以你的测试没什么意义 WinZip可能拒绝解压缩zipfile的一个原因是第一个条目受密码保护。可
check\u if\u zip\u pass(file,pass)作为布尔值
在.net框架中,我似乎找不到任何已经做到这一点的东西,除非我遗漏了一些非常明显的东西
此方法不应提取文件,仅在尝试传递有效时返回True
,否则返回False
。使用第三方库,如。请记住,zipfiles中的密码应用于条目,而不是整个zip文件。所以你的测试没什么意义
WinZip可能拒绝解压缩zipfile的一个原因是第一个条目受密码保护。可能只有一些条目受密码保护,而有些条目不受密码保护。可能是在不同的条目上使用了不同的密码。你必须决定你想对这些可能性做些什么
一种选择是假设在zipfile中加密的任何条目上只使用一个密码。(这不是zip规范所要求的)在这种情况下,下面是一些检查密码的示例代码。没有解密就无法检查密码。因此,该代码解密并提取到Stream.Null中
public bool CheckZipPassword(string filename, string password)
{
bool success = false;
try
{
using (ZipFile zip1 = ZipFile.Read(filename))
{
var bitBucket = System.IO.Stream.Null;
foreach (var e in zip1)
{
if (!e.IsDirectory && e.UsesEncryption)
{
e.ExtractWithPassword(bitBucket, password);
}
}
}
success = true;
}
catch(Ionic.Zip.BadPasswordException) { }
return success;
}
哎呀!我认为是C。在VB.NET中,这将是:
Public Function CheckZipPassword(filename As String, password As String) As System.Boolean
Dim success As System.Boolean = False
Try
Using zip1 As ZipFile = ZipFile.Read(filename)
Dim bitBucket As System.IO.Stream = System.IO.Stream.Null
Dim e As ZipEntry
For Each e in zip1
If (Not e.IsDirectory) And e.UsesEncryption Then
e.ExtractWithPassword(bitBucket, password)
End If
Next
End Using
success = True
Catch ex As Ionic.Zip.BadPasswordException
End Try
Return success
End Function
我使用.NET中的SharpZipLib来实现这一点,下面是一个到他们的wiki的示例,其中包含一个帮助函数,用于解压缩受密码保护的zip文件。下面是VB.NET的助手函数的副本
Imports ICSharpCode.SharpZipLib.Core
Imports ICSharpCode.SharpZipLib.Zip
Public Sub ExtractZipFile(archiveFilenameIn As String, password As String, outFolder As String)
Dim zf As ZipFile = Nothing
Try
Dim fs As FileStream = File.OpenRead(archiveFilenameIn)
zf = New ZipFile(fs)
If Not [String].IsNullOrEmpty(password) Then ' AES encrypted entries are handled automatically
zf.Password = password
End If
For Each zipEntry As ZipEntry In zf
If Not zipEntry.IsFile Then ' Ignore directories
Continue For
End If
Dim entryFileName As [String] = zipEntry.Name
' to remove the folder from the entry:- entryFileName = Path.GetFileName(entryFileName);
' Optionally match entrynames against a selection list here to skip as desired.
' The unpacked length is available in the zipEntry.Size property.
Dim buffer As Byte() = New Byte(4095) {} ' 4K is optimum
Dim zipStream As Stream = zf.GetInputStream(zipEntry)
' Manipulate the output filename here as desired.
Dim fullZipToPath As [String] = Path.Combine(outFolder, entryFileName)
Dim directoryName As String = Path.GetDirectoryName(fullZipToPath)
If directoryName.Length > 0 Then
Directory.CreateDirectory(directoryName)
End If
' Unzip file in buffered chunks. This is just as fast as unpacking to a buffer the full size
' of the file, but does not waste memory.
' The "Using" will close the stream even if an exception occurs.
Using streamWriter As FileStream = File.Create(fullZipToPath)
StreamUtils.Copy(zipStream, streamWriter, buffer)
End Using
Next
Finally
If zf IsNot Nothing Then
zf.IsStreamOwner = True ' Makes close also shut the underlying stream
' Ensure we release resources
zf.Close()
End If
End Try
End Sub
为了进行测试,您可以创建一个文件比较,在压缩文件之前查看该文件,在解压缩文件之后再次查看该文件(大小、日期等)。如果您想使用一个简单的测试文件,比如里面有文本“test”的文件,您甚至可以比较内容。有很多选择,这取决于您想要测试的数量和距离。框架中没有太多内置的选项。您可以尝试使用SharpZipLib库,这里有一个很大的混乱:
public static bool CheckIfCorrectZipPassword(string fileName, string tempDirectory, string password)
{
byte[] buffer= new byte[2048];
int n;
bool isValid = true;
using (var raw = File.Open(fileName, FileMode.Open, FileAccess.Read))
{
using (var input = new ZipInputStream(raw))
{
ZipEntry e;
while ((e = input.GetNextEntry()) != null)
{
input.Password = password;
if (e.IsDirectory) continue;
string outputPath = Path.Combine(tempDirectory, e.FileName);
try
{
using (var output = File.Open(outputPath, FileMode.Create, FileAccess.ReadWrite))
{
while ((n = input.Read(buffer, 0, buffer.Length)) > 0)
{
output.Write(buffer, 0, n);
}
}
}
catch (ZipException ze)
{
if (ze.Message == "Invalid Password")
{
isValid = false;
}
}
finally
{
if (File.Exists(outputPath))
{
// careful, this can throw exceptions
File.Delete(outputPath);
}
}
if (!isValid)
{
break;
}
}
}
}
return isValid;
}
为C#道歉;转换为VB.NET应该相当简单。对不起,我假设它是zip本身,因为没有密码winzip不会解包。你能提供一些代码吗?我在上面运行了一个转换器,但它不工作。你能提供实际的vb代码吗?