17

I have a tarred gunzip file called ZippedXmls.tar.gz which has 2 xmls inside it. I need to programmatically unzip this file and the output should be 2 xmls copied in a folder.

How do I achieve this using C#?

demonplus
  • 5,613
  • 12
  • 49
  • 68
Ed.
  • 1,654
  • 7
  • 20
  • 33
  • 2
    How do you store two 2 "xmls" in one .gz file? Or is it actually a .tar.gz file? – dtb Aug 28 '09 at 16:50
  • Be aware that the gzip standard compresses and uncompresses byte streams only - if you have groups of files compressed, they are likely archived as a single file before being gzipped, as dtb has alluded. – Charlie Salts Aug 28 '09 at 17:17

4 Answers4

34

I've used .Net's built-in GZipStream for gzipping byte streams and it works just fine. I suspect that your files are tarred first, before being gzipped.

You've asked for code, so here's a sample, assuming you have a single file that is zipped:

FileStream stream = new FileStream("output.xml", FileMode.Create); // this is the output
GZipStream uncompressed = new GZipStream(stream, CompressionMode.Decompress);

uncompressed.Write(bytes,0,bytes.Length); // write all compressed bytes
uncompressed.Flush();
uncompressed.Close();

stream.Dispose();

Edit:

You've changed your question so that the file is a tar.gz file - technically my answer is not applicable to your situation, but I'll leave it here for folks who want to handle .gz files.

Charlie Salts
  • 13,109
  • 7
  • 49
  • 78
  • yes, the file is tarred and then zipped.how do i retrieve 2 xmls in this case then?Thanks for your help. – Ed. Aug 28 '09 at 17:47
  • sharpziplib is what you want to use, then. It handles tar.gz files. – Charlie Salts Aug 28 '09 at 17:52
  • Actually this code dies in my machine, complaining writing into the compressionstream is not supported.... – Lorgarn Aug 13 '15 at 12:20
  • @Lorgarn Not all stream operations are supported. What sort of stream is it that you're wrapping? – Charlie Salts Aug 13 '15 at 19:34
  • @CharlieSalts I am using the same steams as in the example. I used a FileByteReader to read the compressed source file into a byte array (which would be 'bytes' in the example above). Writing the bytearray in the GZipStream causes the exception. I solved this, by using 2 FileReaders for source and destination file and using the copy function of the GZipStream. – Lorgarn Aug 14 '15 at 09:38
  • 1
    The linked MS documentation has a perfect example – majjam Mar 02 '16 at 16:06
  • How to initialize the bytes variable used in the code uncompressed.Write(bytes,0,bytes.Length)? – Goldfish Dec 05 '17 at 17:39
  • byte[] bytes = new byte[4096]; – Goldfish Dec 05 '17 at 17:43
7

sharpziplib should be able to do this

Stefan Egli
  • 17,398
  • 3
  • 54
  • 75
2

I know this question is ancient, but search engines redirect here for how to extract gzip in C#, so I thought I'd provide a slightly more recent example:

using (var inputFileStream = new FileStream("c:\\myfile.xml.gz", FileMode.Open))
using (var gzipStream = new GZipStream(inputFileStream, CompressionMode.Decompress))
using (var outputFileStream = new FileStream("c:\\myfile.xml", FileMode.Create))
{
    await gzipStream.CopyToAsync(outputFileStream);
}

For what should be the simpler question of how to untar see: Decompress tar files using C#

Lee Richardson
  • 8,331
  • 6
  • 42
  • 65
  • I only get 5kb output file for some reason. But I have something else that works for me, posted as an answer. – bobt Apr 24 '23 at 14:58
0

This works for me.

using ICSharpCode.SharpZipLib.GZip;

// Specify the path to the input and output files
string inputFile = "input.gz";
string outputFile = "output.txt";

// Open the input file for reading
using (FileStream inputStream = new FileStream(inputFile, FileMode.Open))
{
    // Create a GZipInputStream to decompress the input file
    using (GZipInputStream gzipStream = new GZipInputStream(inputStream))
    {
        // Open the output file for writing
        using (FileStream outputStream = new FileStream(outputFile, FileMode.Create))
        {
            // Copy the decompressed data from the GZipInputStream to the output file
            gzipStream.CopyTo(outputStream);
        }
    }
}
bobt
  • 411
  • 3
  • 8