Reputation: 2221
Does anyone know how to unzip a folder that has been zipped so that just the files within the folder are returned instead of the folder itself.
Currently I am using this:
ZipFile.ExtractToDirectory(<Zipped File Source>, <Unzipped File Destination>)
This works fine for files that have been zipped into one zip file, but for files within a folder that has been zipped, I just want the files.
Upvotes: 0
Views: 3591
Reputation: 2221
Thanks to Stewart_R, for pointing me in the right direction on this one.
Just to reiterate, the "First Method" below works but not in the way I needed it to.
First Method (VB):
ZipFile.ExtractToDirectory(<Zipped File Source>, <Unzipped File Destination>)
As I previously mentioned in my question, using the above code unzips files that have been zipped together or a folder that contains files, but doing the latter will result in the folder-name with the files inside. In order to get just the files try this Second Method (VB):
Using archive As ZipArchive = ZipFile.OpenRead(<zip_file_path>) // "<zip_file_path>" is the location of your zip file
For Each entry As ZipArchiveEntry In archive.Entries
If entry.Name.EndsWith(".txt", StringComparison.OrdinalIgnoreCase) Then // ".txt" is the extention that you're looking for
entry.ExtractToFile(Path.Combine(<unzip_to_path>, entry.Name)) // "<unzip_to_path>" is the destination folder
End If
Next
End Using
Note:
"entry.Name" returns the actual filename with its extention i.e.;
"somefile.txt"
This is important due to the fact that unzipping using the "first method" returns;
"folder_name/somefile.txt"
This behavior can be duplicated using the "second method" if you replace;
"entry.Name"
with;
"entry.FullName"
Final Note:
If you have more than one extension that you are trying to match you may add an "or" (VB), or a || (C#) within the conditional:
VB:
entry.Name.EndsWith(".txt", StringComparison.OrdinalIgnoreCase) or
entry.Name.EndsWith(".jpg", StringComparison.OrdinalIgnoreCase) Then
C#:
entry.Name.EndsWith(".txt", StringComparison.OrdinalIgnoreCase) ||
entry.Name.EndsWith(".jpg", StringComparison.OrdinalIgnoreCase) Then
Upvotes: 1
Reputation: 14485
The same code unzips all files.
From here:
http://msdn.microsoft.com/en-us/library/hh485723%28v=vs.110%29.aspx
we have:
Extracts all the files in the specified zip archive to a directory on the file system.
Upvotes: 1