【问题标题】:Powershell, extract-archive and subdirectoriesPowershell、提取存档和子目录
【发布时间】:2020-04-07 11:21:57
【问题描述】:

有没有办法可以使用 Powershell 的 extract-archive 来提取 zip 的子文件夹?

例如,我有一个名为 a.zip 的 zip。在该 zip 中是一个名为 a 的文件夹,在该文件夹内是一个名为 b 的文件夹,其中包含一堆文件。我想将文件夹b 提取到例如C:\SomeFolder,这样最终的结构是C:\SomeFolder\b 而不是C:\SomeFolder\a\b

【问题讨论】:

  • 当然,提取所有,将“b”移动到您想要的位置,删除其余部分。

标签: powershell powershell-5.0


【解决方案1】:

很遗憾,Expand-Archive 是不可能的。如果可能,我建议使用 7zip。
以下是不使用外部命令(例如 7zip)的方法。

using namespace System.IO.Compression
Add-Type -AssemblyName System.IO.Compression.FileSystem

$zipPath = "C:\a.zip"
$destDirPath = "C:\SomeFolder"
$targetEntry = "a/b/"

$zip = [ZipFile]::OpenRead($zipPath)
$zip.Entries.Where{ $_.FullName -match "$targetEntry.*[^/]$" }.ForEach{
    $newFile = [IO.FileInfo]($destDirPath,$_.FullName -join "/")
    $newFile.Directory.Create()
    [ZipFileExtensions]::ExtractToFile($_, $newFile)
}
$zip.Dispose()

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-04-13
    • 1970-01-01
    • 2013-08-21
    • 2021-03-21
    • 1970-01-01
    相关资源
    最近更新 更多