Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Unzipping a .gz file using C#

Tags:

c#

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#?

like image 523
Ed. Avatar asked Aug 28 '09 16:08

Ed.


2 Answers

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.

like image 50
Charlie Salts Avatar answered Sep 28 '22 02:09

Charlie Salts


sharpziplib should be able to do this

like image 43
Stefan Egli Avatar answered Sep 28 '22 03:09

Stefan Egli