locked
Unzip GZ files using Powershell RRS feed

  • Question

  • 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.
    Monday, December 31, 2012 5:30 PM

Answers

  • 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


    Inspired by Heineken.

    Monday, December 31, 2012 6:59 PM

All replies

  • 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


    Inspired by Heineken.

    Monday, December 31, 2012 6:59 PM
  • 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.


    Inspired by Heineken.

    Wednesday, January 2, 2013 7:47 PM
  • 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


    Hope that helps! Jason

    Friday, January 10, 2014 10:19 PM
  • Thanks, Riffy, that little snippet saved me an hour.

    Upvoted.


    I need more memory. Wait - what was it I needed?

    Tuesday, August 18, 2015 7:22 PM
  • This is Awesome! works Great.

    Question if I want to reverse it to Compress to .gz

    Would I just change the one line of code from

    $gzipStream = New-Object System.IO.Compression.GzipStream $input, ([IO.Compression.CompressionMode]::Decompress)

    to

    $gzipStream = New-Object System.IO.Compression.GzipStream $input, ([IO.Compression.CompressionMode]::Compress)

    Wednesday, March 29, 2017 2:00 PM
  • Also, can you specify a wildcard in the $infile?

    I am having difficulty with that.  I will have a lot of .gz files in the infile directory.

    I would like it to unzip all of the gzip files.

    into the same directory removing the gz extension.

    Wednesday, March 29, 2017 2:58 PM
  • Thanks for posting degzip function.  I found it useful.
    Tuesday, May 2, 2017 3:49 PM
  • If you have git bash installed , you can use the gunzip utility in it to unzip the gz files:

    CD to the directory where the files are stored

    Start-Process   "C:\Program Files\Git\bin\bash.exe " -ArgumentList "gunzip filename.gz"

    OR

    To gunzip all files: 

    Start-Process   "C:\Program Files\Git\bin\bash.exe " -ArgumentList "gunzip *"

    Tuesday, January 8, 2019 8:07 AM
  • It Worked !!! Thanks a ton 
    Thursday, July 25, 2019 2:19 PM
  • thanks that worked :D

    Wednesday, August 7, 2019 4:31 PM