-->

Count files and folders in a zip file using PowerS

2019-08-16 23:28发布

问题:

How do you count the files and folders in a zip file? 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. The final goal is to have a boolean value of if the source count is equal to the zip content count. 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. These zip files can get very large, so extracting first would be too time consuming to be an option.

回答1:

If you can use 7z.exe, you may be able to do something like this:

& '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
  }