Reputation: 3275
I'm trying to unzip a .zip file. That works all fine, but it always creates a folder with the name of the .zip file.
How can I get the files without creating a new folder?
Imports System.IO.Compression
Private Sub XYform_Load(sender As Object, e As EventArgs) Handles Me.Load
Try
If (Not System.IO.Directory.Exists(System.IO.Path.GetTempPath & "\XML")) Then
System.IO.Directory.CreateDirectory(System.IO.Path.GetTempPath & "\XML")
End If
Catch
End Try
Try
ZipFile.ExtractToDirectory("D:\Test\Test data.zip", System.IO.Path.GetTempPath & "\XML")
Catch
'Allready Exists
End Try
End Sub
If I do it this way it will always create a "Test Data" folder in the "XML" folder.
Upvotes: 1
Views: 2700
Reputation: 1802
You need to work with the ZipArchiveEntries individually if you want to manipulate the files.
Here is an example:
Dim zipPath As String = "c:\example\start.zip"
Dim extractPath As String = "c:\example\extract"
Using archive As ZipArchive = ZipFile.OpenRead(zipPath)
For Each entry As ZipArchiveEntry In archive.Entries
entry.ExtractToFile(Path.Combine(extractPath, entry.FullName))
Next
End Using
If you simply want extract to a different directory though, you could use:
Dim zipPath As String = "c:\users\exampleuser\end.zip"
Dim extractPath As String = "c:\users\exampleuser\extract"
Using archive As ZipArchive = ZipFile.Open(zipPath, ZipArchiveMode.Update)
archive.ExtractToDirectory(extractPath)
End Using
Edit:
If you aren't wanting to keep the structure, try this:
Dim zipPath As String = "c:\example\start.zip"
Dim extractPath As String = "c:\example\extract"
Dim zipPath As String = "c:\example\start.zip"
Dim extractPath As String = "c:\example\extract"
Using archive As ZipArchive = ZipFile.OpenRead(zipPath)
For Each entry As ZipArchiveEntry In archive.Entries.Where(Function(a) Not String.IsNullOrEmpty(a.Name))
entry.ExtractToFile(entry.Name)
Next
End Using
Upvotes: 2