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.
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`:$_" }
}