Skip to content

Instantly share code, notes, and snippets.

@aspose-com-gists
Created May 7, 2021 03:51
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save aspose-com-gists/f0195d651fb08e5141e8d8d6069ae461 to your computer and use it in GitHub Desktop.
Save aspose-com-gists/f0195d651fb08e5141e8d8d6069ae461 to your computer and use it in GitHub Desktop.
Create and Extract GZip in C#
using (GzipArchive archive = new GzipArchive())
{
// set source
archive.SetSource("data.bin");
// create archive
archive.Save("archive.gz");
}
// create a memory stream
var ms = new MemoryStream();
// load GZip archive
using (GzipArchive archive = new GzipArchive(File.OpenRead("sample.gz")))
{
// extract and copy to memory stream
archive.Open().CopyTo(ms);
Console.WriteLine(archive.Name);
}
// load the GZip archive
using (var archive = new GzipArchive("archive.gz"))
{
// create a file
using (var extracted = File.Create("data.bin"))
{
// open archive
var unpacked = archive.Open();
byte[] b = new byte[8192];
int bytesRead;
// write to file
while (0 < (bytesRead = unpacked.Read(b, 0, b.Length)))
extracted.Write(b, 0, bytesRead);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment