Spartin503
Spartin503

Reputation: 47

Extracting from your resources VB.net

I have a .zip folder in the .exe resources and I have to move it out and then extract it to a folder. Currently I am moving the .zip out with System.IO.File.WriteAllByte and unziping it. Is there anyway to unzip straight from the resources to a folder?

    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.

Upvotes: 2

Views: 3634

Answers (2)

Ivan Ferrer Villa
Ivan Ferrer Villa

Reputation: 2158

Taking pieces from here and there, this works with 3.5 Framework on Windows 7:

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

Sources: answers here and https://www.codeproject.com/Tips/257193/Easily-Zip-Unzip-Files-using-Windows-Shell
Since we are using the shell to copy the files, it will ask the user to overwrite them if already exist.

Upvotes: 0

Adrian
Adrian

Reputation: 2875

So if you're using the Ionic library already, you could pull out your zip file resource as a stream, and plug that stream into Ionic to decompress it. Given a resource of My.Resources.Unleashed, you have two options for getting your zip file into a stream. You can load up a new MemoryStream from the bytes of the resource:

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

Or you can use the string representation of the name of the resource to pull a stream directly from the assembly:

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

Assuming you want to extract all the files to the current working directory once you have your stream then you'd do something like this:

Using zip As ZipFile = ZipFile.Read(zipFileStream)
    ForEach entry As ZipEntry In zip
        entry.Extract();
    Next
End Using

Upvotes: 1

Related Questions