0

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

marc_s
  • 732,580
  • 175
  • 1,330
  • 1,459
mystack
  • 4,910
  • 10
  • 44
  • 75

2 Answers2

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

Community
  • 1
  • 1
Raf
  • 9,681
  • 1
  • 29
  • 41
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