如何在powershell中列出zip中的文件? [英] How to list the files in a zip in powershell?

查看:116
本文介绍了如何在powershell中列出zip中的文件?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我是 powershell 的新手,希望列出目录中 zip 文件中包含的所有文件.我不想使用任何第三方工具.

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 

等等.

不幸的是我的环境不是 4.5 而是 4.0.我能够写出这段代码,但它仍然需要大量的解析来清理,因为解压缩会提供很多额外的信息.

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

有没有更简单的方法来解析这个.unzip -l 输出中有很多额外信息,例如列标题、分隔符和日期以及每个文件名之前的其他日期.

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.

推荐答案

在 .NET Framework 4.5 中,有一个非常方便的 ZipFile 类.
要列出存档文件中的条目,您可以在 Powershell 中像这样使用它:

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

这篇关于如何在powershell中列出zip中的文件?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆