Programming

Powershell에서 파일의 압축을 푸는 방법은 무엇입니까?

procodes 2020. 5. 7. 20:04
반응형

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가 여전히 비어 있습니다.


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가 해당 폴더를 만듭니다. 다른 경고 :

또한보십시오:


PowerShell v5 +에는 다음과 같은 확장 아카이브 명령 (압축 아카이브)이 내장되어 있습니다.

Expand-Archive c:\a.zip -DestinationPath c:\a

PowerShell v5.1에서는 v5와 약간 다릅니다. MS 문서에 따르면 -Path, 아카이브 파일 경로를 지정 하는 매개 변수가 있어야합니다 .

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

그렇지 않으면 실제 경로가 될 수 있습니다.

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

확장 아카이브 문서


저기요 ..

$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)
}

Expand-Archive매개 변수 집합 중 하나와 함께 cmdlet을 사용하십시오 .

Expand-Archive -LiteralPath C:\source\file.Zip -DestinationPath C:\destination
Expand-Archive -Path file.Zip -DestinationPath C:\destination

expand-archive아카이브 이름을 따서 명명 된 디렉토리를 사용 하지만 자동 작성하는 경우 :

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

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'

For those, who want to use Shell.Application.Namespace.Folder.CopyHere() and want to hide progress bars while copying, or use more options, the documentation is here:
https://docs.microsoft.com/en-us/windows/desktop/shell/folder-copyhere

To use powershell and hide progress bars and disable confirmations you can use code like this:

# 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)

Limitations of use of Shell.Application on windows core versions:
https://docs.microsoft.com/en-us/windows-server/administration/server-core/what-is-server-core

On windows core versions, by default the Microsoft-Windows-Server-Shell-Package is not installed, so shell.applicaton will not work.

note: Extracting archives this way will take a long time and can slow down windows gui

참고URL : https://stackoverflow.com/questions/27768303/how-to-unzip-a-file-in-powershell

반응형