Count files and folders in a zip file using PowerShell

淺唱寂寞╮ 提交于 2020-01-06 04:45:09

问题


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
  }


来源:https://stackoverflow.com/questions/45643218/count-files-and-folders-in-a-zip-file-using-powershell

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!