I have a .zip
file file.zip
. There are a lot of files inside this zip file and only one text file with extension .txt
. I want to read the name of this text file. Is there any way to read the name without extracting the zip file in Powershell or in C#?
Asked
Active
Viewed 2,640 times
0
-
Have you done any code for this? – Poomrokc The 3years Apr 09 '14 at 06:08
-
You can find an answer at this similar question: http://stackoverflow.com/questions/307774/how-to-list-the-contents-of-a-zip-folder-in-c – HABJAN Apr 09 '14 at 06:09
-
Yes i was able to read the file name by extracting zip.But i wan to read the name without extracting – mystack Apr 09 '14 at 06:10
-
HABJAN i want to read it without using any third party library.Is there any way? – mystack Apr 09 '14 at 06:16
2 Answers
1
You can run something like this if you have .Net 4.5
[Reflection.Assembly]::LoadWithPartialName( "System.IO.Compression.FileSystem" );
$zipContens = [System.IO.Compression.ZipFile]::OpenRead("C:\path\file.zip");
$zipContens.Entries | % {
if ($_.Name -match "TheFileYouAreLookingFor.txt"){
# found, your code block
}
}
This question will help: Powershell to search for files based on words / phrase but also Zip files and within zip files
1
You can use the Shell.Application
object for enumerating file names from a zip file:
$zip = 'C:\path\to\file.zip'
$app = New-Object -COM 'Shell.Application'
$app.NameSpace($zip).Items() | ? { $_.Name -like '*.txt' } | select -Expand Name

Ansgar Wiechers
- 193,178
- 25
- 254
- 328