从您的资源中提取VB.net

时间:2013-09-06 01:36:27

标签: vb.net

我在.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.

1 个答案:

答案 0 :(得分: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