zip파일 다루기

-- PowerShell 2011. 8. 16. 18:14
336x280(권장), 300x250(권장), 250x250, 200x200 크기의 광고 코드만 넣을 수 있습니다.
이분 포스트를 3개 가져왔는데, ZIP관련 포스트는 상당히 쓸 만할 것 같다.

Windows 2008 R2 서버에서 PowerShell 이용해 사이트를 구축하는 중입니다.헌데 소스파일이 zip으로 압축되어 있어서 PowerShell에서 zip압축을 있는 도구가 필요했습니다.

구글링을 보니바로 사용할 있는 zip파일 처리함수를 찾을 있었습니다.

아래에 정리합니다.
 

1. zip 파일압축풀기.

function Extract-Zip

{

  param([string]$zipfilename, [string] $destination)

 

  if(test-path($zipfilename))

  {  

    $shellApplication = new-object -com shell.application

    $zipPackage = $shellApplication.NameSpace($zipfilename)

    $destinationFolder = $shellApplication.NameSpace($destination)

    $destinationFolder.CopyHere($zipPackage.Items())

  }

}

@ 사용법: Extract-Zip C:\test\zipfile.zip c:\test\destination

 

2. zip 압축파일 새로만들기.

function New-Zip

{

  param([string]$zipfilename)

  set-content $zipfilename ("PK" + [char]5 + [char]6 + ("$([char]0)" * 18))

  (dir $zipfilename).IsReadOnly = $false

}

 

3. 이미 존재하는 zip압축파일에 파일추가하기.(파이프라인이용)

function Add-Zip

{

  param([string]$zipfilename)

 

  if(-not (test-path($zipfilename)))

  {

    set-content $zipfilename ("PK" + [char]5 + [char]6 + ("$([char]0)" * 18))

    (dir $zipfilename).IsReadOnly = $false  

  }

  

  $shellApplication = new-object -com shell.application

  $zipPackage = $shellApplication.NameSpace($zipfilename)

  

  foreach($file in $input) 

  { 

            $zipPackage.CopyHere($file.FullName)

            Start-sleep -milliseconds 500

  }

}

 

4. zip파일 내부파일 목록확인.

function Get-Zip

{

  param([string]$zipfilename)

  if(test-path($zipfilename))

  {

    $shellApplication = new-object -com shell.application

    $zipPackage = $shellApplication.NameSpace($zipfilename)

    $zipPackage.Items() | Select Path

  }

}

 

작동하고, 유용하게 사용하고 있습니다. 다만, 경로를 지정상대경로로 지정경우 오류가 발생하더군요..

사용하다보니 부분이 불편해서 아래와 같이 고쳐서 사용하고 있습니다.

 

# 압축 풀기.

function Extract-Zip

{

  param([string]$zipfilename, [string] $destination=".") #목적지 기본값은 현재 디렉토리

 

  #상대경로를 절대경로로 변경

  $zipfilename = (Get-Item $zipfilename).FullName

  $destination = (Get-Item $destination).FullName

 

  if(test-path($zipfilename))

  {  

    $shellApplication = new-object -com shell.application

    $zipPackage = $shellApplication.NameSpace($zipfilename)

    $destinationFolder = $shellApplication.NameSpace($destination)

    $destinationFolder.CopyHere($zipPackage.Items())

  }

}

 

# 압축 하기

function Add-Zip

{

  param([string]$zipfilename)

 

  # 파일 명만 지정 했을 때 상대경로로 지정 해 줌.

  if(!$zipfilename.Contains("\"))

  {

    $zipfilename = ".\" + $zipfilename

  }

 

  if(-not (test-path($zipfilename)))

  {

    set-content $zipfilename ("PK" + [char]5 + [char]6 + ("$([char]0)" * 18))

    (dir $zipfilename).IsReadOnly = $false  

  }

 

  #상대경로를 절대경로로 변경

  $zipfilename = (Get-Item $zipfilename).FullName

  

  $shellApplication = new-object -com shell.application

  $zipPackage = $shellApplication.NameSpace($zipfilename)

  

  foreach($file in $input) 

  { 

            $zipPackage.CopyHere($file.FullName)

            Start-sleep -milliseconds 500

  }

}


출처 : http://blog.daum.net/sharer77/6900209

'-- PowerShell' 카테고리의 다른 글

powershell로 ping 테스트  (0) 2011.08.17
PowerShell로 웹소스 가져오기  (0) 2011.08.17
복수 개의 파일의 내용을 변경  (0) 2011.08.16
PowerShell 선언된 변수  (0) 2011.08.16
컴퓨터에 메모리 종류 확인  (0) 2011.06.24
posted by 어린왕자악꿍