How to list the files in a zip in powershell?
Asked Answered
T

5

25

I am new to powershell and looking to list all the files, contained in zip files in a directory. I don't want to use any third-party tool.

Structure of the directory is
mydir > dir
a.zip
b.zip
c.zip

with each file containing files named 1.txt or 2.txt or 3.txt

I am trying to get an output in the form

a.zip:1.txt
a.zip:2.txt
b.zip:files\3.txt
b.zip:4.txt
c.zip:1.txt
d.zip:10.txt 

and so on.

Unfortunately my environment is not 4.5 but 4.0. I was able to write up this code but it still needs a lot of parsing for clean up as unzip gives a lot of extra information.

$packagedir="C:\Packages"
$unzipcmd = "c:\bins\unzip.exe -l"
$unmatchstr = "*Archive*"
pushd .
cd $packagedir

$filelist= Get-ChildItem -Recurse | Select-Object -ExpandProperty FullName

 foreach ($item in $filelist) 
 {$ziplist = Invoke-Expression "$unzipcmd $item"; 
 foreach ($item2 in $ziplist) 
  {
   if ($item2.Contains("Archive") )
   {

   }
   else
   {
     echo $item "::" $item2}} 
   }
popd

Is there any easier way to parse this. There is a lot of extra information in the unzip -l output, like Column headers, separators and dates and other date before every file name.

Tableau answered 7/1, 2013 at 21:21 Comment(2)
minasi.com/forum/topic.asp?TOPIC_ID=26804Mend
learningpcs.blogspot.com/2010/07/…Mend
I
40

In .NET Framework 4.5 there is a ZipFile class that is quite handy.
To list the entries in an archive file, you can use it like this in Powershell:

[Reflection.Assembly]::LoadWithPartialName('System.IO.Compression.FileSystem')
[IO.Compression.ZipFile]::OpenRead($sourceFile).Entries

Update: This seems to do the trick :]

[Reflection.Assembly]::LoadWithPartialName('System.IO.Compression.FileSystem')

foreach($sourceFile in (Get-ChildItem -filter '*.zip'))
{
    [IO.Compression.ZipFile]::OpenRead($sourceFile.FullName).Entries.FullName |
        %{ "$sourcefile`:$_" }
}
Intransigent answered 7/1, 2013 at 21:45 Comment(1)
To keep the .zip file from being locked you can dispose of it after you've read the entries: $zip = [IO.Compression.ZipFile]::OpenRead($sourceFile); $entries = $zip.Entries; $zip.Dispose()Bursa
G
6

If you have the PowerShell Community Extensions, you can use its Read-Archive cmdlet.

Grosgrain answered 8/1, 2013 at 16:10 Comment(1)
In the interests of keeping this answer up-to-date, the PowerShell Community Extensions project has been hosted on Github since 2015. Link: github.com/Pscx/Pscx and Read-Archive is now Read-PscxArchive.Onida
W
5

I had this question too! I crafted up this PS script based on this AWESOME script : https://gallery.technet.microsoft.com/scriptcenter/PowerShell-Get-Specific-9b35352f

What it does is simply list all the 'txt' files along with the zip files found!

The original script copies the files out into a specified dir -- which is also very useful.

Some notes:

You have to define the static variables Make sure none of the directory variables contain the final backslash i.e "directory" and NOT "directory/"

Here it is:

#----------------------------------------------------
# STATIC VARIABLES 
#----------------------------------------------------

$search  = "txt"
$zips   = "C:\zipfiles" 

#----------------------------------------------------
Function GetZipFileItems 
{ 
    Param([string]$zip) 

    $split = $split.Split(".")

    $shell   = New-Object -Com Shell.Application 
    $zipItem = $shell.NameSpace($zip) 
    $items   = $zipItem.Items() 

    GetZipFileItemsRecursive $items
}

Function GetZipFileItemsRecursive 
{     
    Param([object]$items) 
    ForEach($item In $items) 
    {
        $strItem = [string]$item.Name 
        If ($strItem -Like "*$search*")
        { 
            Write-Host "The txt files in the zips are : $strItem"   
        }
    }
}

Function GetZipFiles 
{ 
    $zipFiles = Get-ChildItem -Path $zips -Recurse -Filter "*.zip" | % { $_.DirectoryName + "\$_" } 

    ForEach ($zipFile In $zipFiles) 
    { 
        $split = $zipFile.Split("\")[-1] 
        Write-Host "Found zip-file : $split" 
        GetZipFileItems $zipFile 
    } 
}
GetZipFiles
Whicker answered 17/9, 2016 at 8:50 Comment(1)
I am working on a similar problem and this script gets me very close, but I was wondering how I can get the LastWriteTime of the txt file?Roup
S
4

I don't know how third-party it is, but there is C:\Windows\System32\tar.exe which you can use. Older versions of Windows don't have it. In Linux distros this utility works different. Maybe it's not a best choice for scripts, but you can use it in shell conveniently.

PS C:\> where.exe tar.exe
C:\Windows\System32\tar.exe
tar -tf C:\Stuff\pwshFormatDataGci.zip
fformatdata.ps1xml
formatDataFile.ps1xml
formatDataFile2.ps1xml
formatGci.ps1xml
formatGci2.ps1xml
formatGci3.ps1xml
formatOrigFile.ps1xml

In -tf t is for "list", f is for "file". Execute tar.exe --help to learn more.

Shingle answered 13/10, 2023 at 11:10 Comment(0)
M
0

Using the PSCompression Module you can list zip entries with Get-ZipEntry. The module also comes with a set of cmdlets to manage Zip and Gzip archives.

PS ..\pwsh> Get-ZipEntry .\test.zip -Include docs/en-us*

   Directory: docs/en-US/

EntryType               LastWriteTime  CompressedSize            Size EntryName
---------               -------------  --------------            ---- ---------
Directory          5/30/2023 12:34 AM         0.00  B         0.00  B
Archive            5/30/2023 12:32 AM         1.92 KB         7.58 KB Compress-GzipArchive.md
Archive            5/30/2023 12:33 AM         1.99 KB         7.26 KB Compress-ZipArchive.md
Archive            5/30/2023 12:32 AM         1.02 KB         2.52 KB ConvertFrom-GzipString.md
Archive            5/30/2023 12:32 AM         1.53 KB         4.36 KB ConvertTo-GzipString.md
Archive            5/30/2023 12:32 AM         1.47 KB         4.77 KB Expand-GzipArchive.md
Archive            5/30/2023 12:13 PM         1.20 KB         3.89 KB Expand-ZipEntry.md
Archive            5/31/2023 10:54 AM         1.02 KB         3.29 KB Get-ZipEntry.md
Archive            5/29/2023  3:49 PM       800.00  B         2.69 KB Get-ZipEntryContent.md
Archive            5/29/2023  3:49 PM       736.00  B         1.83 KB New-ZipEntry.md
Archive            5/29/2023  3:11 PM       411.00  B         1.30 KB PSCompression.md
Archive            5/29/2023  3:49 PM       643.00  B         1.19 KB Remove-ZipEntry.md
Archive            5/29/2023  3:49 PM       842.00  B         2.88 KB Set-ZipEntryContent.md
Matz answered 13/10, 2023 at 15:2 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.