简体   繁体   English

使用PowerShell对zip文件中的文件和文件夹进行计数

[英]Count files and folders in a zip file using PowerShell

How do you count the files and folders in a zip file? 您如何计算zip文件中的文件和文件夹? I am running a verification of backups and need to compare the folder structure of the zip file against the folder structure of the windows folder is was made from. 我正在运行备份验证,需要将zip文件的文件夹结构与Windows文件夹的文件夹结构进行比较。 The final goal is to have a boolean value of if the source count is equal to the zip content count. 最终目标是具有布尔值,如果源计数等于zip内容计数。 This is as far as I have, so far: 到目前为止,这是我所拥有的:

 [Reflection.Assembly]::LoadWithPartialName('System.IO.Compression.FileSystem') |out-null
 $ZipContents=[IO.Compression.ZipFile]::OpenRead($FilePath).Entries
 $ZipContents.Count

This gets a raw count of everything, but hoping for a distinct count of files and a count of folders or some way to compare structures. 这得到了所有内容的原始计数,但是希望有不同的文件计数和文件夹计数,或者希望通过某种方式比较结构。 Using 7-zip would be fine, also. 使用7-zip也可以。 These zip files can get very large, so extracting first would be too time consuming to be an option. 这些zip文件可能会变得非常大,因此,首先提取文件将非常耗时,无法选择。

If you can use 7z.exe , you may be able to do something like this: 如果可以使用7z.exe ,则可以执行以下操作:

& 'C:\Program Files\7-Zip\7z.exe' l <zipfilename> |
  Select-Object -Last 1 |
  Select-String '([0-9]+) files, ([0-9]+) folders' |
  ForEach-Object {
    $fileCount = [Int] $_.Matches[0].Groups[1].Value
    $dirCount = [Int] $_.Matches[0].Groups[2].Value
  }

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM