使用 PowerShell 计算 zip 文件中的文件和文件夹 [英] Count files and folders in a zip file using PowerShell

查看:27
本文介绍了使用 PowerShell 计算 zip 文件中的文件和文件夹的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

您如何计算 zip 文件中的文件和文件夹?我正在运行备份验证,需要将 zip 文件的文件夹结构与 Windows 文件夹的文件夹结构进行比较.最终目标是获得一个布尔值,表示源计数是否等于 zip 内容计数.到目前为止,这是我所拥有的:

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

这会获取所有内容的原始计数,但希望获得不同的文件计数和文件夹计数或某种比较结构的方法.使用 7-zip 也可以.这些 zip 文件可能会变得非常大,因此先解压太费时,无法选择.

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.

推荐答案

如果你可以使用 7z.exe,你或许可以这样做:

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

& 'C:Program Files7-Zip7z.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
  }

这篇关于使用 PowerShell 计算 zip 文件中的文件和文件夹的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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