2010. 8. 25. 13:26
안녕하세요. 엉스데브 입니다.

PowerShell 에서 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
  }
}

profile 파일에 이 함수를 넣어두고 요렇게 사용하면 됩니다.




<참고 URL>
http://blogs.msdn.com/b/daiken/archive/2007/02/12/compress-files-with-windows-powershell-then-package-a-windows-vista-sidebar-gadget.aspx
Posted by 알 수 없는 사용자
:
BLOG main image
Windows Server를 공부 하는 사람들의 팀블로그 by 마성민

카테고리

분류 전체보기 (76)
Windows (2)
Powershell (56)
AD (0)
Exchange (6)
System Center (9)
IIS (0)
SQL (3)
Sharepoint (0)

태그목록

Tistory Cumulus Flash tag cloud by BLUEnLIVE requires Flash Player 9 or better.

Total :
Today : Yesterday :