且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

Powershell 可根据单词/短语以及 Zip 文件和 zip 文件搜索文件

更新时间:2023-12-04 21:32:34

如果您安装了 .Net 4.5,您可以使用 System.IO.Compression.FileSystem 库来创建一个函数,该函数将打开和遍历 zip 文件的内容

If you have .Net 4.5 installed you can use System.IO.Compression.FileSystem library to create a function which will open and traverse contents of your zip files

$searchTerms = @( "test","bingo","false","one two","england")
function openZip($zipFile){
    try{
        [Reflection.Assembly]::LoadWithPartialName( "System.IO.Compression.FileSystem" ) | Out-Null;
        $zipContens = [System.IO.Compression.ZipFile]::OpenRead($zipFile);  
        $zipContens.Entries | % {
            foreach($searchTerm in $searchTerms){
                if ($_.Name -imatch $searchTerm){
                    Write-Output ($_.Name + "," + $_.FullName + "," + $_.CompressedLength + "," + $_.LastWriteTime + "," + $_.Length);
                }
            }               
        }
    }
    catch{
        Write-Output ("There was an error:" + $_.Exception.Message);
    }
}  

然后您可以运行类似这样的程序来获取文件名、zip 中的完整路径、压缩大小等.

You can then run something like this to obtain filename,full path within zip, compressed size etc.

Get-ChildItem -Path -$filePath *.zip -Recurse -ErrorAction SilentlyContinue | %  {
    openZip $_.FullName  >> c:\path\to\report.csv
    }