2015-02-05 2 views
2

Powershell v4를 사용하여 tar.gz 보관 파일을 만드는 문제를 해결하기위한 솔루션을 찾고 있습니다. 이러한 기능을 제공하기 위해 Microsoft에서 만들거나 확인한 내선 번호/패킷을 찾을 수 없습니다. 그러한 솔루션이 존재합니까 [타르 ing 및 gzip - ing]?powershell을 사용하여 tar gz 파일을 만드는 방법

답변

1

Microsoft는in .Net 2.0을 구현했습니다. C# 및 PowerShell의 많은 다른 예제가 있습니다. Powershell Community Extensions에는 Write-Zip 및 Write-Tar 기능도 있습니다. gzip을위한 함수는 다음과 같습니다. 다중 입력과 더 나은 출력 명명을 처리하기 위해 실제로 업데이트되어야합니다. 또한 한 번에 하나의 파일 만 처리합니다. 그러나 어떻게 완료했는지 알고 싶다면 시작해야합니다.

function New-GZipArchive 
{ 
    [CmdletBinding(SupportsShouldProcess=$true, 
        PositionalBinding=$true)] 
    [OutputType([Boolean])] 
    Param 
    (
     # The input file(s) 
     [Parameter(Mandatory=$true, 
        ValueFromPipeline=$true, 
        Position=0)] 
     [ValidateNotNull()] 
     [ValidateNotNullOrEmpty()] 
     [ValidateScript({Test-Path $_})] 
     [String] 
     $fileIn, 

     # The path to the requested output file 
     [Parameter(Position=1)] 
     [ValidateNotNull()] 
     [ValidateNotNullOrEmpty()] 
     #validation is done in the script as the only real way to determine if it is a valid path is to try it 
     [String] 
     $fileOut, 

     # Param3 help description 
     [Switch] 
     $Clobber 
    ) 
    Process 
    { 
     if ($pscmdlet.ShouldProcess("$fileIn", "Zip file to $fileOut")) 
     { 
      if($fileIn -eq $fileOut){ 
       Write-Error "You can't zip a file into itself" 
       return 
      } 
      if(Test-Path $fileOut){ 
       if($Clobber){ 
        Remove-Item $fileOut -Force -Verbose 
       }else{ 
        Write-Error "The output file already exists and the Clobber parameter was not specified. Please input a non-existent filename or specify the Clobber parameter" 
        return 
       } 
      } 
      try{ 
       #create read stream     
       $fsIn = New-Object System.IO.FileStream($fileIn, [System.IO.FileMode]::Open, [System.IO.FileAccess]::Read, [System.IO.FileShare]::Read) 
       #create out stream 
       $fsOut = New-Object System.IO.FileStream($fileOut, [System.IO.FileMode]::CreateNew, [System.IO.FileAccess]::Write, [System.IO.FileShare]::None) 
       #create gzip stream using out file stream 
       $gzStream = New-Object System.IO.Compression.GZipStream($fsout, [System.IO.Compression.CompressionMode]::Compress) 
       #create a shared buffer 
       $buffer = New-Object byte[](262144) #256KB 
       do{ 
        #read into buffer 
        $read = $fsIn.Read($buffer,0,262144) 
        #write buffer back out 
        $gzStream.Write($buffer,0,$read) 
       } 
       while($read -ne 0) 
      } 
      catch{ 
       #really should add better error handling 
       throw 
      } 
      finally{ 
       #cleanup 
       if($fsIn){ 
        $fsIn.Close() 
        $fsIn.Dispose() 
       } 
       if($gzStream){ 
        $gzStream.Close() 
        $gzStream.Dispose() 
       } 
       if($fsOut){ 
        $fsOut.Close() 
        $fsOut.Dispose() 
       } 
      } 
     } 
    } 
} 

사용법 :

dir *.txt | %{New-GZipArchive $_.FullName $_.FullName.Replace('.txt','.gz') -verbose -clobber} 
VERBOSE: Performing operation "Zip file to C:\temp\a.gz" on Target "C:\temp\a.txt". 
VERBOSE: Performing operation "Remove file" on Target "C:\temp\a.gz". 
VERBOSE: Performing operation "Zip file to C:\temp\b.gz" on Target "C:\temp\b.txt". 
VERBOSE: Performing operation "Remove file" on Target "C:\temp\b.gz". 
VERBOSE: Performing operation "Zip file to C:\temp\c.gz" on Target "C:\temp\c.txt". 
VERBOSE: Performing operation "Remove file" on Target "C:\temp\c.gz". 
+1

무엇 용기 설정에 대한 처리? – user3376246

+1

powershell 커뮤니티 확장 프로그램 https://pscx.codeplex.com/에서 쓰기 -tar 스크립트가 있습니다. 나는 Windows에서 tar 파일로 작업 할 필요가 없었기 때문에 더 쉬운 방법을 모릅니다. – StephenP

+0

Powershell Community Extensions가 Github에 있습니다 : https://github.com/Pscx/Pscx – garie

관련 문제