-
Jan 5th, 2022, 05:58 AM
#1
Thread Starter
Addicted Member
[RESOLVED] Unzip Archive from Resources
As i read in net 4.5 has included:
Imports System.IO.Compression
but i face problems to extract from my resources .zip archive and unzip it in project directory
In my resources i have Test.zip archive added , but when i write zipFilePath it got problems with Byte() to string so one.. so how i can correctly get from resource and unzip it?
Code:
Const zipFilePath As String = My.Resources.Test
Const dirToExtract As String = "C:\apps\Sample Pictures\"
Using zipFileToOpen As New FileStream(zipFilePath, FileMode.Open)
Using archive As New ZipArchive(zipFileToOpen, ZipArchiveMode.Update)
archive.ExtractToDirectory(dirToExtract)
End Using
End Using
-
Jan 5th, 2022, 06:40 AM
#2
Re: Unzip Archive from Resources
Originally Posted by luckydead
As i read in net 4.5 has included:
Imports System.IO.Compression
but i face problems to extract from my resources .zip archive and unzip it in project directory
In my resources i have Test.zip archive added , but when i write zipFilePath it got problems with Byte() to string so one.. so how i can correctly get from resource and unzip it?
Code:
Const zipFilePath As String = My.Resources.Test
Const dirToExtract As String = "C:\apps\Sample Pictures\"
Using zipFileToOpen As New FileStream(zipFilePath, FileMode.Open)
Using archive As New ZipArchive(zipFileToOpen, ZipArchiveMode.Update)
archive.ExtractToDirectory(dirToExtract)
End Using
End Using
Is the zip file embedded as a resource in your application? If so the resource My.Resources.Test is the zip file, not a path to the zip file.
Not tested the code below, so it could very well be wrong but try something like
Code:
dim zipBytes as byte() = My.Resources.Test
Using zipFileToOpen As New MemoryStream(zipBytes)
Using archive As New ZipArchive(zipFileToOpen, ZipArchiveMode.Update)
archive.ExtractToDirectory(dirToExtract)
End Using
End Using
-
Jan 5th, 2022, 06:52 AM
#3
Thread Starter
Addicted Member
Re: Unzip Archive from Resources
Originally Posted by PlausiblyDamp
Is the zip file embedded as a resource in your application? If so the resource My.Resources.Test is the zip file, not a path to the zip file.
Not tested the code below, so it could very well be wrong but try something like
Code:
dim zipBytes as byte() = My.Resources.Test
Using zipFileToOpen As New MemoryStream(zipBytes)
Using archive As New ZipArchive(zipFileToOpen, ZipArchiveMode.Update)
archive.ExtractToDirectory(dirToExtract)
End Using
End Using
yes it works byte() = my.resource seems to do the trick with the .zip
Thanks
Code:
Private Sub ExportZip(zipBytes As Byte(), dirPath As String)
Using zipFileToOpen As New MemoryStream(zipBytes)
Using archive As New ZipArchive(zipFileToOpen, ZipArchiveMode.Update)
archive.ExtractToDirectory(dirPath)
MsgBox("Extract Completed")
End Using
End Using
End Sub
Last edited by luckydead; Jan 5th, 2022 at 06:57 AM.
-
Jan 5th, 2022, 07:21 AM
#4
Thread Starter
Addicted Member
Re: [RESOLVED] Unzip Archive from Resources
sorry i have one problem here, i receive this error:
System.NotSupportedException: 'Memory stream is not expandable.'
I think this maybe solve the issue, but i how can i make if files already been extracted, to overwrite them
Code:
Private Sub ExportZip(zipBytes As Byte(), dirPath As String)
Using zipFileToOpen As MemoryStream = New MemoryStream(zipBytes)
Using archive As New ZipArchive(zipFileToOpen, ZipArchiveMode.Update)
archive.ExtractToDirectory(dirPath)
'MsgBox("Extract Completed")
End Using
End Using
End Sub
Last edited by luckydead; Jan 5th, 2022 at 07:41 AM.
-
Jan 5th, 2022, 07:48 AM
#5
Re: [RESOLVED] Unzip Archive from Resources
Originally Posted by luckydead
sorry i have one problem here, i receive this error:
System.NotSupportedException: 'Memory stream is not expandable.'
I think this maybe solve the issue, but i how can i make if files already been extracted, to overwrite them
Code:
Private Sub ExportZip(zipBytes As Byte(), dirPath As String)
Using zipFileToOpen As MemoryStream = New MemoryStream(zipBytes)
Using archive As New ZipArchive(zipFileToOpen, ZipArchiveMode.Update, True)
archive.ExtractToDirectory(dirPath)
'MsgBox("Extract Completed")
End Using
End Using
End Sub
Perhaps make the ZipArchiveMode.Read rather than update if you are only ever going to extract, according to the docs https://docs.microsoft.com/en-us/dot...acttodirectory you can specify to overwrite existing files when extracting. Other than that the Sub looks like it should work as long as you are passing in a byte array that contains a valid Zip archive.
When you get the exception how are you calling that method?
-
Jan 5th, 2022, 07:50 AM
#6
Thread Starter
Addicted Member
Re: [RESOLVED] Unzip Archive from Resources
so i try something like this:
Code:
Private Sub ExportZip(zipBytes As Byte(), dirPath As String)
Using zipFileToOpen As New MemoryStream(zipBytes)
Using archive As New ZipArchive(zipFileToOpen, ZipArchiveMode.Update)
For Each entry As ZipArchiveEntry In archive.Entries
entry.ExtractToFile(dirPath + entry.Name, True)
Next
End Using
End Using
End Sub
but i got problem again with the Momory "Memory stream is not expandable.'"
if i try it like you say to replace Update with Read same sting happends cannot overwrite the file if its already there
Code:
Private Sub ExportZip(zipBytes As Byte(), dirPath As String)
Using zipFileToOpen As MemoryStream = New MemoryStream(zipBytes)
Using archive As New ZipArchive(zipFileToOpen, ZipArchiveMode.Read)
archive.ExtractToDirectory(dirPath)
'MsgBox("Extract Completed")
End Using
End Using
End Sub
-
Jan 5th, 2022, 08:48 AM
#7
Re: [RESOLVED] Unzip Archive from Resources
In the second example you aren't using the same overload of the ExtractToDirectory method, try using the one that takes a second Boolean parameter.
Which line in the first example gives the exception?
-
Jan 5th, 2022, 09:02 AM
#8
Thread Starter
Addicted Member
Re: [RESOLVED] Unzip Archive from Resources
error shows for both variants here:
Using archive As New ZipArchive(zipFileToOpen, ZipArchiveMode.Update)
and gives error "Memory stream is not expandable.'"
-
Jan 5th, 2022, 09:37 AM
#9
Thread Starter
Addicted Member
Re: [RESOLVED] Unzip Archive from Resources
i think maybe the problem comes from here
zipBytes As Byte() = my.resources.Test
when i try do like this:
Dim sax As Byte() = My.Resources.Test
MsgBox(System.Text.Encoding.UTF8.GetString(sax))
It gives some stuff like PK[][][][]
Encoding try all UTF ANSII all give some stuff
-
Jan 5th, 2022, 09:44 AM
#10
Re: [RESOLVED] Unzip Archive from Resources
Originally Posted by luckydead
error shows for both variants here:
Using archive As New ZipArchive(zipFileToOpen, ZipArchiveMode.Update)
and gives error "Memory stream is not expandable.'"
If you were updating the MemoryStream then I could understand that exception, however if you are only reading from it that seems strange...
Try the following...
Code:
dim zipBytes as byte() = My.Resources.Test
Using zipFileToOpen As New MemoryStream()
zipFileToOpen.Write(zipBytes)
Using archive As New ZipArchive(zipFileToOpen, ZipArchiveMode.Read)
archive.ExtractToDirectory(dirToExtract, True)
End Using
End Using
and see if that works, that will prevent a fixed sized MemoryStream (even though that shouldn't matter) and will make sure the files are overwritten on extract.
Last edited by PlausiblyDamp; Jan 5th, 2022 at 09:54 AM.
-
Jan 5th, 2022, 09:45 AM
#11
Re: [RESOLVED] Unzip Archive from Resources
Originally Posted by luckydead
i think maybe the problem comes from here
zipBytes As Byte() = my.resources.Test
when i try do like this:
Dim sax As Byte() = My.Resources.Test
MsgBox(System.Text.Encoding.UTF8.GetString(sax))
It gives some stuff like PK[][][][]
Encoding try all UTF ANSII all give some stuff
A zip file is a binary file, trying to display it as UTF8 text makes no sense.
-
Jan 5th, 2022, 09:53 AM
#12
Thread Starter
Addicted Member
Re: [RESOLVED] Unzip Archive from Resources
Originally Posted by PlausiblyDamp
If you were updating the MemoryStream then I could understand that exception, however if you are only reading from it that seems strange...
Try the following...
Code:
dim zipBytes as byte() = My.Resources.Test
Using zipFileToOpen As New MemoryStream()
zipFileToOpen.Write(zipBytes)
Using archive As New ZipArchive(zipFileToOpen, ZipArchiveMode.Update)
archive.ExtractToDirectory(dirToExtract, True)
End Using
End Using
and see if that works, that will prevent a fixed sized MemoryStream (even though that shouldn't matter) and will make sure the files are overwritten on extract.
this cannot work:
Public Overrides Sub Write(buffer() As Byte, offset As Integer, count As Integer)
Public Shared Sub ExtractToDirectory(source As ZipArchive, destinationDirectoryName As String)
errors:
zipFileToOpen.Write(zipBytes)
archive.ExtractToDirectory(dirToExtract, True)
you can make an test, add some Test.zip in your project Resources
add in form 1 button that has:
ExportZip(My.Resources.Test, ".\Test")
then add one function
Code:
Private Sub ExportZip(zipBytes As Byte(), dirPath As String)
Using zipFileToOpen As MemoryStream = New MemoryStream(zipBytes)
Using archive As New ZipArchive(zipFileToOpen, ZipArchiveMode.Update)
archive.ExtractToDirectory(dirPath)
'MsgBox("Extract Completed")
End Using
End Using
End Sub
Last edited by luckydead; Jan 5th, 2022 at 09:56 AM.
-
Jan 5th, 2022, 10:10 AM
#13
Re: [RESOLVED] Unzip Archive from Resources
Originally Posted by luckydead
this cannot work:
Public Overrides Sub Write(buffer() As Byte, offset As Integer, count As Integer)
Public Shared Sub ExtractToDirectory(source As ZipArchive, destinationDirectoryName As String)
errors:
zipFileToOpen.Write(zipBytes)
archive.ExtractToDirectory(dirToExtract, True)
What errors do you get? The more information you provide the less we have to guess at what is going wrong. What version of .Net are you using? The code I posted will work with the latest version but if you are using an older version then it might not be compatible.
You could always change the first error to be zipFileToOpen.Write(zipBytes, 0, zipBytes.Length) and specify the extra parameters - it would have only taken a couple of minutes to read the docs to figure that out.
It looks like the Overload of the ExtractToDirectory doesn't exist in Framework 4.8 and earlier so you could loop over the entries. Try
Code:
Using zipFileToOpen As New MemoryStream(zipBytes)
Using archive As New ZipArchive(zipFileToOpen, ZipArchiveMode.Read)
For Each entry As ZipArchiveEntry In archive.Entries
entry.ExtractToFile(Path.Combine(dirToExtract, entry.Name), True)
Next
End Using
End Using
Which works without any exceptions when used with a fixed sized MemoryStream, or if that is still throwing an error try
Code:
Using zipFileToOpen As New MemoryStream()
zipFileToOpen.Write(zipBytes, 0, zipBytes.Length)
Using archive As New ZipArchive(zipFileToOpen, ZipArchiveMode.Read)
For Each entry As ZipArchiveEntry In archive.Entries
entry.ExtractToFile(Path.Combine(dirToExtract, entry.Name), True)
Next
End Using
End Using
-
Jan 5th, 2022, 10:14 AM
#14
Thread Starter
Addicted Member
Re: [RESOLVED] Unzip Archive from Resources
both codes return error " System.IO.DirectoryNotFoundException: 'Could not find a part of the path " because there isnt created folder.
If i create manually the folder .\Test it will work and will overwrite the files, but if folder not exist directly crash
-
Jan 5th, 2022, 10:19 AM
#15
Re: [RESOLVED] Unzip Archive from Resources
Originally Posted by luckydead
both codes return error " System.IO.DirectoryNotFoundException: 'Could not find a part of the path " because there isnt created folder.
If i create manually the folder .\Test it will work and will overwrite the files, but if folder not exist directly crash
Then check to see if the folder is there and create it if it isn't. Or handle the exception by creating the folder and trying it again.
-
Jan 5th, 2022, 10:42 AM
#16
Thread Starter
Addicted Member
Re: [RESOLVED] Unzip Archive from Resources
so i manage it this way:
Code:
Using zipFileToOpen As New MemoryStream()
zipFileToOpen.Write(zipBytes, 0, zipBytes.Length)
Using archive As New ZipArchive(zipFileToOpen, ZipArchiveMode.Read)
For Each entry As ZipArchiveEntry In archive.Entries
Dim entryFullname = Path.Combine(dirPath, entry.FullName)
Dim entryPath = Path.GetDirectoryName(entryFullname)
If (Not (Directory.Exists(entryPath))) Then
Directory.CreateDirectory(entryPath)
End If
Dim entryFn = Path.GetFileName(entryFullname)
If (Not String.IsNullOrEmpty(entryFn)) Then
entry.ExtractToFile(entryFullname, True)
End If
Next
End Using
End Using
Seems to work
-
Jan 5th, 2022, 10:48 AM
#17
Re: [RESOLVED] Unzip Archive from Resources
Originally Posted by luckydead
so i manage it this way:
Code:
Using zipFileToOpen As New MemoryStream()
zipFileToOpen.Write(zipBytes, 0, zipBytes.Length)
Using archive As New ZipArchive(zipFileToOpen, ZipArchiveMode.Read)
For Each entry As ZipArchiveEntry In archive.Entries
Dim entryFullname = Path.Combine(dirPath, entry.FullName)
Dim entryPath = Path.GetDirectoryName(entryFullname)
If (Not (Directory.Exists(entryPath))) Then
Directory.CreateDirectory(entryPath)
End If
Dim entryFn = Path.GetFileName(entryFullname)
If (Not String.IsNullOrEmpty(entryFn)) Then
entry.ExtractToFile(entryFullname, True)
End If
Next
End Using
End Using
Seems to work
Code:
Dim entryFullname = Path.Combine(dirPath, entry.FullName)
Dim entryPath = Path.GetDirectoryName(entryFullname)
Seems a bit redundant as you are combining the dirPath with the entry name, and then splitting the Directory name out again - that should just be the same as dirPath anyway.
Also with
Code:
If (Not (Directory.Exists(entryPath))) Then
Directory.CreateDirectory(entryPath)
End If
you are checking if the directory exists for every single file you extract, unless you are expecting the directory to be deleted mid operation you could just check before you start extracting any files.
Code:
Dim entryFn = Path.GetFileName(entryFullname)
Just appears to be splitting the full name to get back to the filename, which you already had in entry.FullName - no need to keep splitting up the full path.
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|