mystack
mystack

Reputation: 5492

Read a filename inside a zip file without extracting it in powershell

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#?

Upvotes: 0

Views: 3011

Answers (2)

Ansgar Wiechers
Ansgar Wiechers

Reputation: 200313

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

Upvotes: 1

Raf
Raf

Reputation: 10107

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

Upvotes: 1

Related Questions