programing

zip 파일 생성/추출 및 기존 파일/내용 덮어쓰기

telebox 2023. 9. 24. 12:47
반응형

zip 파일 생성/추출 및 기존 파일/내용 덮어쓰기

Add-Type -A System.IO.Compression.FileSystem
[IO.Compression.ZipFile]::CreateFromDirectory('foo', 'foo.zip')
[IO.Compression.ZipFile]::ExtractToDirectory('foo.zip', 'bar')

는 이 답변에서 PowerShell을 통해 .zip 파일을 생성하고 추출하는 코드를 찾았지만, 저의 평판이 낮기 때문에 그 답변에 대한 코멘트로서 질문을 할 수 없습니다.

  • 만들기 - 기존 .zip 파일을 사용자와 상호 작용 없이 덮어쓰는 방법?
  • 추출 - 기존 파일 및 폴더를 사용자 상호 작용 없이 덮어쓰는 방법(로보카피처럼 사용) mir함수).

PowerShell에 내장되어 있습니다..zip사용할 필요가 없는 유틸리티버전 5 이상의 NET 클래스 메소드. TheCompress-Archive -Path논쟁은 또한 a를 갖습니다.string[]여러 폴더/파일을 대상 zip으로 압축할 수 있도록 입력합니다.


지퍼:

Compress-Archive -Path C:\Foo -DestinationPath C:\Foo.zip -CompressionLevel Optimal -Force

또한 있습니다.-Update스위치를 바꾸다

압축 풀기:

Expand-Archive -Path C:\Foo.zip -DestinationPath C:\Foo -Force

5 이전 버전의 PowerShell은 이 스크립트를 실행할 수 있습니다.

@Ola-M 업데이트 해주셔서 감사합니다.

@maximilian-burszley님께 업데이트 감사드립니다.

function Unzip($zipfile, $outdir)
{
    Add-Type -AssemblyName System.IO.Compression.FileSystem
    $archive = [System.IO.Compression.ZipFile]::OpenRead($zipfile)
    try
    {
        foreach ($entry in $archive.Entries)
        {
            $entryTargetFilePath = [System.IO.Path]::Combine($outdir, $entry.FullName)
            $entryDir = [System.IO.Path]::GetDirectoryName($entryTargetFilePath)

            #Ensure the directory of the archive entry exists
            if(!(Test-Path $entryDir )){
                New-Item -ItemType Directory -Path $entryDir | Out-Null 
            }

            #If the entry is not a directory entry, then extract entry
            if(!$entryTargetFilePath.EndsWith("\")){
                [System.IO.Compression.ZipFileExtensions]::ExtractToFile($entry, $entryTargetFilePath, $true);
            }
        }
    }
    finally
    {
        $archive.Dispose()
    }
}

Unzip -zipfile "$zip" -outdir "$dir"

언급URL : https://stackoverflow.com/questions/45618605/create-extract-zip-file-and-overwrite-existing-files-content

반응형