Rebex ZIP

ZIP and GZIP compression .NET library

Download 30-day free trial Buy from $199
More .NET libraries

Back to feature list...

GZIP

Creating GZIP archives 

To create a GZIP file, use Gzip.Compress() method:

// compress data to GZIP file
// (new GZIP file is created = existing GZIP file is overwritten)
var gzip = new Rebex.IO.Compression.Gzip();
gzip.Compress(@"C:\MyData\file.txt", TraversalMode.MatchFilesShallow, @"C:\MyData\file.txt.gz");

Note: GZIP specification makes it possible to include more than one file in a GZIP archive. However, many GZIP readers can only access the first file in the GZIP archive, so it's better not to utilize this feature.

Extracting GZIP archives 

To extract a GZIP file, use Gzip.Decompress() method:

// decompress data from GZIP file
// (overwrite already-existing files in the target directory)
var gzip = new Rebex.IO.Compression.Gzip();
gzip.Decompress(@"C:\MyData\file.txt.gz", @"C:\MyData");

Note: This will extract all files from the GZIP archive. However, most GZIP archives only contain a single file.

Creating GZIP using stream-based API 

To fine-tune creation of GZIP files, use the GzipCompressionStream class. This makes it possible to create files to be compressed on-the-fly, create archives in memory, and more.

// initialize GZIP compression stream
using (var gzip = new Rebex.IO.Compression.Streams.GzipCompressionStream(
        File.Open(@"C:\MyData\on-the-fly.txt.gz", FileMode.CreateNew), // underlying stream
        OpenMode.Write, // mode of operation
        "on-the-fly.txt" // name of file in .gz file
        ))
{
    // write some text into the stream
    using (var writer = new StreamWriter(gzip))
    {
        writer.WriteLine("Content of this file,");
        writer.WriteLine("was created on the fly.");
    }
}

Note: GZIP specification makes it possible to include more than one file in a GZIP archive. However, many GZIP readers can only access the first file in the GZIP archive, so it's better not to utilize this feature.

Extracting GZIP using stream-based API 

To fine-tune extraction of GZIP files, use the GzipDecompressionStream class.

// initialize GZIP decompression stream
using (var gzip = new Rebex.IO.Compression.Streams.GzipDecompressionStream(
    File.OpenRead(@"C:\MyData\file.txt.gz"), OpenMode.Read))
{
    // iterate through all files inside GZIP archive
    // (unless you only wish to process the first one)
    while (gzip.ReadNextFileHeader())
    {
        Console.WriteLine("Extracting file '{0}'", gzip.FileName);

        // read everything from current GZIP item into a memory stream
        var ms = new MemoryStream();
        gzip.CopyTo(ms);

        Console.WriteLine("Decompressed length: {0} bytes", ms.Length);
    }
}

Back to feature list...