Unzip GZ files using Powershell
I have  Windows 2008 Server with some GZ type files in a folder. I would like to script unzipping them using Powershell, can someone tell me if this is possible and how I would do it?

The folder is d:\data_files\ and I'd like to uncompress all the .gz files in there

I've searched around on the Net but can't find much that deals with GZ files specifically. The files are all named data-1.gz, data-2.gz etc Hello

I have  Windows 2008 Server with some GZ type files in a folder. I would like to script unzipping them using Powershell, can someone tell me if this is possible and how I would do it?

The folder is d:\data_files\ and I'd like to uncompress all the .gz files in there

I've searched around on the Net but can't find much that deals with GZ files specifically.
December 31st, 2012 8:30pm

I would be tempted to use 7-Zip which includes a shell program (as opposed to GUI), and can work with GZIP archives.

Powershell can start the 7z.exe process and pass parameters to it.

You can download 7-Zip here: http://www.7-zip.org/

The command line arguments can be found by running 7-zip and using the help function.

Here's some simple code which will list the files in an archive using this method.

## 7-Zip List command parameters

$argumentlist="l $($myarchive)"

## Execute command

start-process 'C:\Program Files\7-Zip\7z.exe' -argumentlist $argumentlist -wait -RedirectStandardOutput $tempfile

Free Windows Admin Tool Kit Click here and download it now
December 31st, 2012 9:59pm

I also found that .Net 2.0 and above has native code for dealing with gzip files.

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

$infile='C:\Temp\DECfpc1new.csv.gz'
$outfile='c:\temp\DECfpc1new.csv'

DeGZip-File $infile $outfile

You can supply the function with the full path of the file to be unzipped, and the full path of the unzipped file.

If you don't supply the unzipped file path, the function will unzip the file into the same folder as the source, and remove the .gz extension.

January 2nd, 2013 10:47pm

I just wanted to update this thread, I wrote a function based on RiffyRiot's suggestion that writes the strings to memory instead of a file.  You can find ConvertFrom-GZip here:

http://gallery.technet.microsoft.com/scriptcenter/ConvertFrom-GZip-edcbf6a9

Free Windows Admin Tool Kit Click here and download it now
January 11th, 2014 1:19am

Thanks, Riffy, that little snippet saved me an hour.

Upvoted.

August 18th, 2015 3:25pm

This topic is archived. No further replies will be accepted.

Other recent topics Other recent topics