Create and Extract GZip Archives using C#

Create GZip in C#

GZip archives (.gz) are used to compress one or more files using the GNU zip compression algorithm. It is commonly used for file compression in UNIX operating systems. In this article, you will learn how to compress files and create GZip (.gz) archives programmatically using C#. Moreover, the article will also cover how to extract GZip archives using C#.

C# API to Create and Extract GZip Archives

To create a GZip archive, we will use Aspose.ZIP for .NET. It is a powerful API that lets you work with popular archive formats including ZIP, 7z, GZip, etc. You can either download the API or install it using NuGet.

PM> Install-Package Aspose.Zip

Create a GZip Archive in C#

The following are the steps to create a GZip archive using C#.

The following code sample shows how to create a GZip archive using C#.

Extract a GZip Archive in C#

The following are the steps to extract a GZip archive using Aspose.ZIP for .NET.

  • Create an object of the GzipArchive class and initialize it with GZip archive’s path.
  • Create a file for extracted content using File.Create(String) method.
  • Open GZip for extraction using GZipArchive.Open() method.
  • Read the extracted content and write it to the file.

The following code sample shows how to extract a GZip archive using C#.

Extract a GZip Archive into Stream in C#

You can also extract a GZip archive into a memory stream object. The following are the steps to perform this operation.

The following code sample shows how to extract a GZip archive into a memory stream using C#.

Get a Free API License

You can get a free temporary license in order to use the API without evaluation limitations.

Conclusion

In this article, you have learned how to create GZip archives using C#. Furthermore, you have seen how to extract the files in GZip archives programmatically. You can explore more about Aspose.ZIP for .NET using documentation. In addition, you can contact us to share your queries via our forum.

See Also