读取 zip 存档文件的内容

Reading zip archive files for content

我创建了以下代码,但它实际上并没有按预期读取任何 .zip 或 .prd 文件,但其他部分正在运行。你能帮我弄清楚为什么它实际上没有在 ForEach 循环中读取 zip 文件吗?

#Set variables for subversion paths
$ScriptPath = (Split-Path $myinvocation.MyCommand.path -Parent)
#$Package = $ScriptPath

#Create folder for temporary copy of SVN directory
New-Item C:\MyTemp-type directory -ErrorAction SilentlyContinue -Force
New-Item C:\Results -type directory -ErrorAction SilentlyContinue -Force
Test-Path $profile
New-item –type file –force $profile

#Set variables for subversion paths
$CurrentSVNPath = "http://MySVNPath" 
$TempSVNPath = "C:\MyTemp"
$ResultsFilePath = "C:\Results"

#Download a local copy of the SVN Repository
 # Wait for command to finish before moving forward
#$SVNDownloadJob = Start-Job {SVN Checkout $CurrentSVNPath $TempSVNPath}
$SVNDownloadJob = Start-Job {TortoiseProc.exe /command:update /path:"C:\SVN_Repo" /closeonend:1 | Out-Null} 
Wait-Job $SVNDownloadJob
Receive-Job $SVNDownloadJob

#Find pattern to search for within all files from where this script is run
#If you do not want to look for the standard pattern uncomment/comment the $Pattern variables
    #$Pattern = Read-Host 'What pattern are you looing for?'
    $Pattern = "Simulate=`"true`""
    $SearchPattern = “Simulate=`”true`””
    $PackageList = Get-ChildItem –Path $ScriptPath –Recurse –filter “*.zip, *.prd”

#Start searching all folders where this script exists for 'Simulate=true'
$k =foreach ($file in Get-ChildItem -Path $ScriptPath -Recurse | Select-String -pattern $Pattern | Select-Object -Unique path) {$file.path}
$k > "$($ResultsFilePath)\SimulateEqualsTrueZipFiles.txt"

Foreach ( $Package in $PackageList) {
Read-Archive -$_.Path -Format Zip | `
   Where-Object { $_.Name -like "*.zip" } | `
  Expand-Archive -PassThru | select-string $SearchPattern | Select-Object -Unique path {$file.path}
  $k > "$($ResultsFilePath)\SimulateEqualsTrueZipFiles.txt"
  }

您可以使用以下 cmdlet 获取 Zip 文件夹的内容

$ZipFile="Path"
$AllFile=Get-ChildItem $ZipFile -Recurse -Filter '*.zip'
$ObjArray = @() 
foreach ($item in $AllFile){
   $FullName=$item.FullName
    $RawFiles = [IO.Compression.ZipFile]::OpenRead($FullName).Entries            
    $FullPathName=$FullName+"\"+"$RawFile"
foreach($RawFile in $RawFiles) { 

  $object = New-Object -TypeName PSObject            
  $Object | Add-Member -MemberType NoteProperty -Name FileName -Value $RawFile.Name   
  $Object | Add-Member -MemberType NoteProperty -Name FullPath -Value $FullPathName            
  $Object | Add-Member -MemberType NoteProperty -Name CompressedLengthInKB -Value ($RawFile.CompressedLength/1KB).Tostring("00")            
  $Object | Add-Member -MemberType NoteProperty -Name UnCompressedLengthInKB -Value ($RawFile.Length/1KB).Tostring("00")            
  $Object | Add-Member -MemberType NoteProperty -Name FileExtn -Value ([System.IO.Path]::GetExtension($RawFile.FullName))            
  $Object | Add-Member -MemberType NoteProperty -Name ZipFileName -Value $zipfile            
  $ObjArray += $Object            
 }

 }

确保在 运行 上述 cmdlet 之前安装了 dot net 4.5。