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가 해당 폴더를 만듭니다. 다른 경고 :
- 기존 파일을 덮어 쓰지 않고 대신 IOException을 트리거합니다.
- 이 방법을 사용하려면 Windows Vista 이상에서 사용 가능한 .NET Framework 4.5 이상이 필요합니다.
- 현재 작업 디렉토리를 기준으로 상대 경로를 확인할 수 없습니다. PowerShell의 .NET 개체가 현재 디렉토리를 사용하지 않는 이유를 참조하십시오 .
또한보십시오:
- 파일을 압축하고 추출하는 방법 (Microsoft Docs)
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
'Programming' 카테고리의 다른 글
요청 실패 : 허용되지 않는 콘텐츠 유형 : AFNetworking 2.0을 사용하는 text / html (0) | 2020.05.07 |
---|---|
프로그래밍 방식으로 segue 만들기 (0) | 2020.05.07 |
Eclipse 오류 : 'Java Virtual Machine을 작성하지 못했습니다' (0) | 2020.05.06 |
폴더의 파일 이름을 순차 번호로 바꾸기 (0) | 2020.05.06 |
HTML에서 "모두 선택"확인란을 구현하는 방법은 무엇입니까? (0) | 2020.05.06 |