web-dev-qa-db-ja.com

Powershellでファイルを解凍する方法

.Zipファイルがあり、Powershellを使用してその内容全体を解凍する必要があります。私はこれをやっていますが、うまくいかないようです。

$Shell = New-Object -ComObject Shell.application
$Zip = $Shell.NameSpace("C:\a.Zip")
MkDir("C:\a")
foreach ($item in $Zip.items()) {
  $Shell.Namespace("C:\a").CopyHere($item)
}

どうしましたか?ディレクトリC:\aはまだ空です。

186
Uli Kunkel

これはSystem.IO.Compression.ZipFileのExtractToDirectoryを使った簡単な方法です。

Add-Type -AssemblyName System.IO.Compression.FileSystem
function Unzip
{
    param([string]$zipfile, [string]$outpath)

    [System.IO.Compression.ZipFile]::ExtractToDirectory($zipfile, $outpath)
}

Unzip "C:\a.Zip" "C:\a"

ターゲットフォルダが存在しない場合は、ExtractToDirectoryによって作成されます。

ファイルを圧縮して抽出する方法

212

PowerShell v5以降では、 Expand-Archiveコマンド (およびCompress-Archive)が組み込まれています。

Expand-Archive c:\a.Zip -DestinationPath c:\a
422
Keith Hill

PowerShell v5.1では、これはv5とは少し異なります。 MSのドキュメントによると、アーカイブファイルのパスを指定するために-Pathパラメータが必要です。

Expand-Archive -Path Draft.Zip -DestinationPath C:\Reference

それ以外の場合は、これが実際のパスになる可能性があります。

Expand-Archive -Path c:\Download\Draft.Zip -DestinationPath C:\Reference

拡張アーカイブDoc

20
NIK

こんにちはそれは私のために働いています..

$Shell = New-Object -ComObject Shell.application
$Zip = $Shell.NameSpace("put ur Zip file path here")
foreach ($item in $Zip.items()) {
  $Shell.Namespace("destination where files need to unzip").CopyHere($item)
}
11
Abhijit

expand-archiveを使用しますが、アーカイブにちなんで名付けられたディレクトリを自動作成します:

function unzip ($file) {
    $dirname = (Get-Item $file).Basename
    New-Item -Force -ItemType directory -Path $dirname
    expand-archive $file -OutputPath $dirname -ShowProgress
}
2
mikemaccana

Expand-Archiveコマンドレットをパラメーターセットの1つと共に使用します。

Expand-Archive -LiteralPath C:\source\file.Zip -DestinationPath C:\destination
Expand-Archive -Path file.Zip -DestinationPath C:\destination
1
Saleh
function unzip {
    param (
        [string]$archiveFilePath,
        [string]$destinationPath
    )

    if ($archiveFilePath -notlike '?:\*') {
        $archiveFilePath = [System.IO.Path]::Combine($PWD, $archiveFilePath)
    }

    if ($destinationPath -notlike '?:\*') {
        $destinationPath = [System.IO.Path]::Combine($PWD, $destinationPath)
    }

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

    $archiveFile = [System.IO.File]::Open($archiveFilePath, [System.IO.FileMode]::Open)
    $archive = [System.IO.Compression.ZipArchive]::new($archiveFile)

    if (Test-Path $destinationPath) {
        foreach ($item in $archive.Entries) {
            $destinationItemPath = [System.IO.Path]::Combine($destinationPath, $item.FullName)

            if ($destinationItemPath -like '*/') {
                New-Item $destinationItemPath -Force -ItemType Directory > $null
            } else {
                New-Item $destinationItemPath -Force -ItemType File > $null

                [System.IO.Compression.ZipFileExtensions]::ExtractToFile($item, $destinationItemPath, $true)
            }
        }
    } else {
        [System.IO.Compression.ZipFileExtensions]::ExtractToDirectory($archive, $destinationPath)
    }
}

使用方法

unzip 'Applications\Site.Zip' 'C:\inetpub\wwwroot\Site'
1
user1624251

Shell.Application.Namespace.Folder.CopyHere() を使用し、コピー中にプログレスバーを非表示にしたい、またはより多くのオプションを使用したい人のためのドキュメントは、次のとおりです。
https://docs.Microsoft.com/ja-jp/windows/desktop/Shell/folder-copyhere

PowerShellを使用してプログレスバーを非表示にして確認を無効にするには、次のようなコードを使用できます。

# We should create folder before using it for Shell operations as it is required
New-Item -ItemType directory -Path "C:\destinationDir" -Force

$Shell = New-Object -ComObject Shell.Application
$Zip = $Shell.Namespace("C:\archive.Zip")
$items = $Zip.items()
$Shell.Namespace("C:\destinationDir").CopyHere($items, 1556)

WindowsコアバージョンでのShell.Applicationの使用制限
https://docs.Microsoft.com/ja-jp/windows-server/administration/server-core/what-is-server-core

Windows core のバージョンでは、デフォルトで Microsoft-Windows-Server-Shell-Package はインストールされていないため、Shell.applicatonは機能しません。

:この方法でアーカイブを抽出すると時間がかかり、WindowsのGUIが遅くなる可能性があります

0
Matej Ridzon