Unzipping a GZip with PowerShell works but can I extract directly to file?

11.7k Views Asked by At

Internet is an amazing place to be, I found this code that allows me to inflate a tar.gz file to .tar:

Function DeGZip-File{
    Param(
        $infile,
        $outfile = ($infile -replace '\.gz$','')
        )
    $input = New-Object System.IO.FileStream $inFile, ([IO.FileMode]::Open), ([IO.FileAccess]::Read), ([IO.FileShare]::Read)
    $output = New-Object System.IO.FileStream $outFile, ([IO.FileMode]::Create), ([IO.FileAccess]::Write), ([IO.FileShare]::None)
    $gzipStream = New-Object System.IO.Compression.GzipStream $input, ([IO.Compression.CompressionMode]::Decompress)
    $buffer = New-Object byte[](1024)
    while($true){
        $read = $gzipstream.Read($buffer, 0, 1024)
        if ($read -le 0){break}
        $output.Write($buffer, 0, $read)
        }
    $gzipStream.Close()
    $output.Close()
    $input.Close()
}
DeGZip-File "C:\temp\maxmind\temp.tar.gz" "C:\temp\maxmind\temp.tar"

Now I have a .tar file in my hands. but how can I open that?

My goal is to extract directly from .tar.gz to file.

1

There are 1 best solutions below

0
user1 On BEST ANSWER
Get-ChildItem -Path $InputPath -Filter "*.tar.gz" | 
Foreach-Object {
    tar -xvzf $_.FullName -C $OutputPath
}