2

我在 .exe 资源中有一个 .zip 文件夹,我必须将其移出,然后将其解压缩到一个文件夹中。目前我正在使用 System.IO.File.WriteAllByte 将 .zip 移出并解压缩。有没有办法直接从资源解压到文件夹?

    Me.Cursor = Cursors.WaitCursor
    'Makes the program look like it's loading.

    Dim FileName As FileInfo
    Dim Dir_ExtractPath As String = Me.tb_Location.Text
    'This is where the FTB folders are located on the drive.

    If Not System.IO.Directory.Exists("C:\Temp") Then
        System.IO.Directory.CreateDirectory("C:\Temp")
    End If
    'Make sure there is a temp folder.

    Dim Dir_Temp As String = "C:\Temp\Unleashed.zip"
    'This is where the .zip file is moved to.

    Dim Dir_FTBTemp As String = Dir_ExtractPath & "\updatetemp"
    'This is where the .zip is extracted to.

    System.IO.File.WriteAllBytes(Dir_Temp, My.Resources.Unleashed)
    'This moves the .zip file from the resorces to the Temp file.

    Dim UnleashedZip As ZipEntry
    Using Zip As ZipFile = ZipFile.Read(Dir_Temp)
        For Each UnleashedZip In Zip
            UnleashedZip.Extract(Dir_FTBTemp, ExtractExistingFileAction.DoNotOverwrite)
        Next
    End Using
    'Extracts the .zip to the temp folder.
4

2 回答 2

1

因此,如果您已经在使用 Ionic 库,则可以将您的 zip 文件资源作为流提取出来,然后将该流插入 Ionic 以解压缩它。给定 My.Resources.Unleashed 资源,您有两个选项可将您的 zip 文件放入流中。您可以从资源的字节中加载一个新的MemoryStream :

Using zipFileStream As MemoryStream = New MemoryStream(My.Resources.Unleashed)
    ...
End Using

或者,您可以使用资源名称的字符串表示直接从程序集中提取流:

Dim a As Assembly = Assembly.GetExecutingAssembly()
Using zipFileStream As Stream = a.GetManifestResourceStream("My.Resources.Unleashed")
    ...
End Using

假设您想在拥有流后将所有文件提取到当前工作目录,那么您将执行以下操作:

Using zip As ZipFile = ZipFile.Read(zipFileStream)
    ForEach entry As ZipEntry In zip
        entry.Extract();
    Next
End Using
于 2013-09-06T08:11:00.137 回答
0

从这里和那里开始,这适用于 Windows 7 上的 3.5 框架:

Dim shObj As Object = Activator.CreateInstance(Type.GetTypeFromProgID("Shell.Application"))
Dim tmpZip As String = My.Application.Info.DirectoryPath & "\tmpzip.zip"
Using zip As Stream = Assembly.GetExecutingAssembly().GetManifestResourceStream("myProject.myfile.zip")
  Dim by(zip.Length) As Byte
  zip.Read(by, 0, zip.Length)
  My.Computer.FileSystem.WriteAllBytes(tmpZip, by, False)
End Using
'Declare the output folder
Dim output As Object = shObj.NameSpace(("C:\destination"))
'Declare the input zip file saved above
Dim input As Object = shObj.NameSpace((tmpZip)) 'I don't know why it needs to have double parentheses, but it fails without them
output.CopyHere((input.Items), 4)
IO.File.Delete(tmpZip)
shObj = Nothing

来源:这里的答案和https://www.codeproject.com/Tips/257193/Easily-Zip-Unzip-Files-using-Windows-Shell
由于我们使用 shell 复制文件,它会要求用户覆盖如果已经存在的话。

于 2022-02-18T14:31:16.520 回答