Created
May 7, 2021 03:51
-
-
Save aspose-com-gists/f0195d651fb08e5141e8d8d6069ae461 to your computer and use it in GitHub Desktop.
Create and Extract GZip in C#
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
using (GzipArchive archive = new GzipArchive()) | |
{ | |
// set source | |
archive.SetSource("data.bin"); | |
// create archive | |
archive.Save("archive.gz"); | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// 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); | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// 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