Powershell:在脚本中使用[系统.IO.压缩.ZipFileExtensions]::创建条目来源文件

4uqofj5v  于 2023-10-18  发布在  Shell
关注(0)|答案(1)|浏览(113)

我在脚本中使用[System.IO.Compression.ZipFileExtensions]::EntryFromFile来创建zip存档,这允许我使用/更改存档中文件的路径/名称格式。这是一个示例:

Add-Type -assembly "system.io.compression.filesystem" 
$compressionLevel = [System.IO.Compression.CompressionLevel]::Fastest
$zip = [System.IO.Compression.ZipFile]::Open(c:\temp\archive.zip, 'update')
$files = (Get-ChildItem c:\folder | where-object {$_.Attributes -ne 'Directory'} 
$files| ForEach-Object {[System.IO.Compression.ZipFileExtensions]::CreateEntryFromFile($zip, $_.FullName,($_.Name),$compressionLevel)}
$zip.Dispose()

它工作正常,但它列出了PowerShell执行屏幕中的所有文件,我不喜欢。所以我为每个添加到归档的文件都得到了这个:

Archive          : System.IO.Compression.ZipArchive
CompressedLength : 
FullName         : file1.txt
LastWriteTime    : 11/16/2022 11:55:43 AM -05:00
Length           : 
Name             : file1.txt

有没有办法避免在执行过程中打印所有这些信息?
谢谢你,谢谢
我到处找都找不到

更新:########

下面是我在压缩文件时如何使用try/catch
方法#1:

Add-Type -assembly "system.io.compression.filesystem" 
$compressionLevel = [System.IO.Compression.CompressionLevel]::Fastest
$zip = [System.IO.Compression.ZipFile]::Open(c:\temp\archive.zip, 'update')
$files = (Get-ChildItem c:\folder | where-object {$_.Attributes -ne 'Directory'} 

ForEach ($file in $files){
try {
[System.IO.Compression.ZipFileExtensions]::CreateEntryFromFile($zip, $_.FullName,($_.Name),$compressionLevel)
} catch { # cannot access file
Write-Warning  "Could not archive $($_.FullName). `n $($_.Exception.Message)"  
$Error_Message = $_.Exception.Message
write-warning $Error_Message
write-output "$(get-date) :ERROR Something went Wrong.  ErrorMessage : $Error_Message  " | out-file err_log.txt -Append -Force; 

}Finally{
$zip.Dispose()
}
}

我收到错误消息,它指向$Error_Message中的一个文件。
$Error_Message:使用“4”参数调用“EntryEntryFromFile”时出现异常:“进程无法访问文件'c:\folder\process.log',因为它正被另一个进程使用。"
方法2:
与“compress-archive”方法相同:

try {
Compress-Archive -Path (Get-ChildItem c:\folder | where-object {$_.Attributes -ne 'Directory'}).FullName -DestinationPath 'c:\temp\archive.zip'
} catch { # cannot access file
Write-Warning  "Could not archive $($_.FullName). `n $($_.Exception.Message)"  
$Error_Message = $_.Exception.Message
write-warning $Error_Message
write-output "$(get-date) :ERROR Something went Wrong.  ErrorMessage : $Error_Message  " | out-file err_log.txt -Append -Force; 
} finally{ 
}

我确实在屏幕上看到了错误-“进程无法访问文件'c:\folder\process.log',因为它正被另一个进程使用。”,但下面是$Error_Message:
$Error_Message:使用“1”参数调用“.ctor”时出现异常:“流不可读。"
不知道如何使压缩存档方法工作,并抓住适当的错误信息。
谢谢-

unftdfkk

unftdfkk1#

将数据库EntryFromFile()通过管道传输到Out-Null
Compress-Archive命令能完成所需的任务吗?
Get-ChildItem之前不应该有和左括号。

Add-Type -assembly "system.io.compression.filesystem" 
$compressionLevel = [System.IO.Compression.CompressionLevel]::Fastest
$zip = [System.IO.Compression.ZipFile]::Open((Join-Path -Path $Env:TEMP -ChildPath 'archive.zip'), 'update')
$files = Get-ChildItem 'c:\src' | Where-Object {$_.Attributes -ne 'Directory'} 
$files |
    ForEach-Object {
        [System.IO.Compression.ZipFileExtensions]::CreateEntryFromFile($zip, $_.FullName,($_.Name),$compressionLevel)
    } |
    Out-Null
$zip.Dispose()

更新:

捕捉异常。您必须决定如果归档条目创建失败该怎么做。

Add-Type -assembly "system.io.compression.filesystem" 
$compressionLevel = [System.IO.Compression.CompressionLevel]::Fastest
$zip = [System.IO.Compression.ZipFile]::Open((Join-Path -Path $Env:TEMP -ChildPath 'archive.zip'), 'update')
$files = Get-ChildItem 'c:\src' | Where-Object {$_.Attributes -ne 'Directory'} 
$files |
    ForEach-Object {
        $ThisFile = $_
        try {
            [System.IO.Compression.ZipFileExtensions]::CreateEntryFromFile($zip, $_.FullName,($_.Name),$compressionLevel) 2>$null
        } catch {
            Write-Warning -Message "Failed to add $($ThisFile.FullName) to archive."
        }
    } |
    Out-Null
$zip.Dispose()

相关问题