我希望创建一个zip文件提取器,用于我为asp.net网站创建的插件。我希望客户能够运行提取器,并将文件放置到我指定的正确文件夹中。有没有相关工具可以实现这个功能?我不确定如何操作,谢谢。
DotNetZip是一个允许托管代码应用程序读取或写入zip文件的库。它可以实现生成自解压缩存档(SFX)。
在C#中,使用DotNetZip生成SFX存档的代码如下:
using (ZipFile zip1 = new ZipFile())
{
// zip up a directory
zip1.AddDirectory("C:\\project1\\datafiles", "data");
zip1.Comment = "This will be embedded into a self-extracting exe";
zip1.AddEntry("Readme.txt", "This is content for a 'Readme' file that will appear in the zip.");
zip1.SaveSelfExtractor("archive.exe", SelfExtractorFlavor.WinFormsApplication);
}
在生成SFX时有很多选项,例如:
通过DotNetZip类库接口可以访问SFX的所有这些选项。
Public Shared Function Main(ByVal args As String()) As Integer
Dim sfxStub As String = "my-sfx-stub.exe"
Dim outputFile As String = "my-sfx-archive.exe"
Dim directoryToZip As String = "c:\directory\to\ZIP"
Dim buffer As Byte() = New Byte(4000){}
Dim n As Integer = 1
Using output As System.IO.Stream = File.Open(outputFile, FileMode.Create)
'' copy the contents of the sfx stub to the output stream
Using input As System.IO.Stream = File.Open(sfxStub, FileMode.Open)
While n <> 0
n = input.Read(buffer, 0, buffer.Length)
If n <> 0 Then
output.Write(buffer, 0, n)
End If
End While
End Using
'' now save the zip file to the same stream
Using zp As New ZipFile
zp.AddFiles(Directory.GetFiles(directoryToZip), False, "")
zp.Save(output)
End Using
End Using
End Function
Dim a As Assembly = Assembly.GetExecutingAssembly
Try
'' read myself as a zip file
Using zip As ZipFile = ZipFile.Read(a.Location)
Dim entry As ZipEntry
For Each entry in zip
'' extract here, or add to a listBox, etc.
'' listBox1.Items.Add(entry.FileName)
Next
End Using
Catch
MessageBox.Show("-No embedded zip file.-")
End Try