gpt4 book ai didi

windows - Powershell-扩展存档和重命名文件,因为其中包含无效字符

转载 作者:行者123 更新时间:2023-12-02 23:50:09 25 4
gpt4 key购买 nike

我无法使用Powershell解压缩存档。

假设我已经下载了file.zipfile.zip的文件名中带有“冒号”的文件。 (即foo:bar.p12)

使用7zip手动解压缩这些文件会自动用下划线(:)替换冒号(_)。工作正常!

在powershell中使用expand-archive会引发以下错误:

> New-Object : Exception calling ".ctor" with "1" argument(s): "The
> given path's format is not supported." At
> C:\Windows\system32\WindowsPowerShell\v1.0\Modules\Microsoft.PowerShell.Archive\Microsoft.PowerShell.Archive.psm1:1004
> char:52
> + ... yFileInfo = New-Object -TypeName System.IO.FileInfo -ArgumentList $cu ...
> + ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
> + CategoryInfo : InvalidOperation: (:) [New-Object], MethodInvocationException
> + FullyQualifiedErrorId : ConstructorInvokedThrowException,Microsoft.PowerShell.Commands.NewObjectCommand

这是我的代码:

$zip_location = "C:\path\to\zipfiles"
$zipfiles = Get-ChildItem $zip_location -Filter *.zip

foreach ($zip in $zipfiles) {
Expand-Archive -Path $zip_location\$zip -DestinationPath $zip_location -Force
}

有没有办法使用Powershell提取这些文件?谢谢你。

最佳答案

使用 ZipFile type手动提取文件:

# Import the containing assembly
Add-Type -AssemblyName System.IO.Compression.FileSystem

try{
# Open the zip file with ZipFile.OpenRead()
$zipFileItem = Get-Item .\Path\To\File.zip
$zipFile = [System.IO.Compression.ZipFile]::OpenRead($zipFileItem.FullName)

foreach($entry in $zipFile.Entries){
# Remove the `:` from any file name
$targetFileName = $entry.Fullname -replace ':','_'

# Create new file on disk, open writable stream
$targetFileStream = $(
New-Item -Path $targetFileName -ItemType File -Force
).OpenWrite()

# Open stream to compressed file, copy to new file stream
$entryStream = $entry.Open()
$entryStream.BaseStream.CopyTo($targetFileStream)

# Clean up
$targetFileStream,$entryStream |ForEach-Object Dispose
}
}
finally{
# clean up
if($zipFile){ $zipFile.Dispose() }
}

关于windows - Powershell-扩展存档和重命名文件,因为其中包含无效字符,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/61253767/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com