source

Powershell에서 파일을 압축 해제하려면 어떻게 해야 합니까?

myloves 2023. 4. 8. 10:18

Powershell에서 파일을 압축 해제하려면 어떻게 해야 합니까?

나는 가지고 있다.zipPowershell을 사용하여 전체 콘텐츠를 압축 해제해야 합니다.이렇게 하고 있는데 작동하지 않는 것 같습니다.

$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아직 비어 있습니다.

PowerShell v5+에는 Expand-Archive 명령(Compress-Archive)이 포함되어 있습니다.

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

다음은 시스템에서 ExtractToDirectory를 사용하는 간단한 방법입니다.IO. 압축.Zip 파일:

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가 폴더를 만듭니다.기타 주의사항:

  • 기존 파일을 덮어쓰지 않고 IOException을 트리거합니다.
  • 이 방법에는 적어도가 필요합니다.NET Framework 4.5, Windows Vista 이후에 사용 가능.
  • 상대 경로는 현재 작업 디렉토리에 따라 해결되지 않습니다.를 참조해 주십시오.PowerShell의 NET 개체가 현재 디렉토리를 사용합니까?

다음 항목도 참조하십시오.

PowerShell v5.1에서는 v5와 약간 다릅니다.MS의 문서에 따르면, 이 시스템은-Pathparameter를 지정하여 아카이브 파일 경로를 지정합니다.

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

또는 실제 경로일 수 있습니다.

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

확장 아카이브 문서

사용하다Expand-Archive다음 매개 변수 중 하나가 설정된 cmdlet:

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

나한테는 효과가 있는데..

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

내장된 powershell 방식 사용Expand-Archive

Expand-Archive -LiteralPath C:\archive.zip -DestinationPath C:\

사용.expand-archive그러나 아카이브 이름을 딴 디렉토리를 자동으로 생성합니다.

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

Shell을 사용하고 싶은 분들을 위해.응용 프로그램.네임스페이스Folder.CopyHere() 복사 중에 진행 표시줄을 숨기거나 다른 옵션을 사용하려면 다음 문서를 참조하십시오.
https://learn.microsoft.com/en-us/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)

Shell의 사용Windows 코어 버전의 응용 프로그램:
https://learn.microsoft.com/en-us/windows-server/administration/server-core/what-is-server-core

Windows 코어 버전에서는 기본적으로 Microsoft-Windows-Server-Shell-Package가 설치되지 않으므로 shell.application은 작동하지 않습니다.

메모: 이 방법으로 아카이브를 추출하면 시간이 오래 걸리고 Windows GUI 속도가 느려질 수 있습니다.

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'

ForEach내부 ZIP 파일 각각을 루프 처리한다.$filepath변수

    foreach($file in $filepath)
    {
        $zip = $shell.NameSpace($file.FullName)
        foreach($item in $zip.items())
        {
            $shell.Namespace($file.DirectoryName).copyhere($item)
        }
        Remove-Item $file.FullName
    }

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