12

I'm searching for solution to solve problem with creating tar.gz archive using Powershell v4. I couldn't find extensions/packets created or verified by Microsoft to provide such functionality. Do such solutions exist [tar-ing and gzip-ing]?

2 Answers 2

26

Another possibility is to call out to the native tar command that Windows now supports (with gzip compression):

tar -cvzf filename.tgz mydir

https://techcommunity.microsoft.com/t5/Containers/Tar-and-Curl-Come-to-Windows/ba-p/382409

1

Microsoft implemented a gzipstream in System.IO.Compression in .Net 2.0 There are tons of examples in C# and many others in powershell. The Powershell Community Extensions also have Write-Zip and Write-Tar functions. Here is a function for gzip I did quite a while back. It should really be updated to handle multiple inputs and better output naming. It also only handles one file at a time. But it should get you started if you want to see how its done.

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()
                }
            }
        }
    }
}

Usage:

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".
3
  • 2
    What about taring process? Commented Feb 6, 2015 at 5:56
  • 1
    Take a look at the powershell community extensions pscx.codeplex.com they have a write-tar script. I don't know of an easier way to do it as I have never needed to work with tar files on Windows.
    – StephenP
    Commented Feb 8, 2015 at 22:48
  • Before I discovered the new tar command I tried Pscx, but it does not currently install cleanly on current PowerShell without this workaround: github.com/Pscx/Pscx/issues/23#issuecomment-356272051,
    – mikebridge
    Commented Feb 28, 2019 at 18:36

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.