Class GzipArchive
Namespace: Aspose.Zip.Gzip
Assembly: Aspose.Zip.dll (25.7.0)
This class represents a gzip archive file. Use it to compose or extract gzip archives.
public class GzipArchive : IArchive, IDisposable, IArchiveFileEntry
Inheritance
Implements
IArchive , IDisposable , IArchiveFileEntry
Inherited Members
object.GetType() , object.MemberwiseClone() , object.ToString() , object.Equals(object?) , object.Equals(object?, object?) , object.ReferenceEquals(object?, object?) , object.GetHashCode()
Remarks
Gzip compression algorithm is based on the DEFLATE algorithm, which is a combination of LZ77 and Huffman coding.
Constructors
GzipArchive()
Initializes a new instance of the Aspose.Zip.Gzip.GzipArchive class prepared for compressing.
public GzipArchive()
Examples
The following example shows how to compress a file.
using (GzipArchive archive = new GzipArchive())
{
archive.SetSource("data.bin");
archive.Save("archive.gz");
}
GzipArchive(Stream, bool)
Initializes a new instance of the Aspose.Zip.Gzip.GzipArchive class prepared for decompressing.
public GzipArchive(Stream sourceStream, bool parseHeader = false)
Parameters
sourceStream
Stream
The source of the archive.
parseHeader
bool
Whether to parse stream header to figure out properties, including name. Makes sense for seekable stream only.
Examples
Open an archive from a stream and extract it to a MemoryStream
var ms = new MemoryStream();
using (GzipArchive archive = new GzipArchive(File.OpenRead("archive.gz")))
archive.Open().CopyTo(ms);
Remarks
This constructor does not decompress. See Aspose.Zip.Gzip.GzipArchive.Open method for decompressing.
Exceptions
sourceStream
is null.
sourceStream
is too short.
The sourceStream
has wrong signature.
GzipArchive(Stream, GzipLoadOptions)
Initializes a new instance of the Aspose.Zip.Gzip.GzipArchive class prepared for decompressing.
public GzipArchive(Stream sourceStream, GzipLoadOptions options)
Parameters
sourceStream
Stream
The source of the archive.
options
GzipLoadOptions
Options to load the archive with.
Examples
Open an archive from a stream and extract it to a MemoryStream
var ms = new MemoryStream();
GzipLoadOptions options = new GzipLoadOptions();
using (GzipArchive archive = new GzipArchive(File.OpenRead("archive.gz"), options))
archive.Extract(ms);
Remarks
This constructor does not decompress. See Aspose.Zip.Gzip.GzipArchive.Open method for decompressing.
Exceptions
sourceStream
is null.
sourceStream
is too short.
The sourceStream
has wrong signature.
GzipArchive(string, GzipLoadOptions)
Initializes a new instance of the Aspose.Zip.Gzip.GzipArchive class prepared for decompressing.
public GzipArchive(string path, GzipLoadOptions options)
Parameters
path
string
The path to the archive file.
options
GzipLoadOptions
Options to load the archive with.
Examples
Open an archive from file by path and extract it to a MemoryStream
var ms = new MemoryStream();
GzipLoadOptions options = new GzipLoadOptions();
using (GzipArchive archive = new GzipArchive("archive.gz", options))
archive.Extract(ms);
Remarks
This constructor does not decompress. See Aspose.Zip.Gzip.GzipArchive.Open method for decompressing.
Exceptions
path
is null.
The caller does not have the required permission to access
The path
is empty, contains only white spaces, or contains invalid characters.
Access to file path
is denied.
The specified path
, file name, or both exceed the system-defined maximum length. For example, on Windows-based platforms, paths must be less than 248 characters, and file names must be less than 260 characters.
File at path
contains a colon (:) in the middle of the string.
The file is too short.
Data in the file has the wrong signature.
GzipArchive(string, bool)
Initializes a new instance of the Aspose.Zip.Gzip.GzipArchive class prepared for decompressing.
public GzipArchive(string path, bool parseHeader = false)
Parameters
path
string
The path to the archive file.
parseHeader
bool
Whether to parse stream header to figure out properties, including name. Makes sense for seekable stream only.
Examples
Open an archive from file by path and extract it to a MemoryStream
var ms = new MemoryStream();
using (GzipArchive archive = new GzipArchive("archive.gz"))
archive.Open().CopyTo(ms);
Remarks
This constructor does not decompress. See Aspose.Zip.Gzip.GzipArchive.Open method for decompressing.
Exceptions
path
is null.
The caller does not have the required permission to access
The path
is empty, contains only white spaces, or contains invalid characters.
Access to file path
is denied.
The specified path
, file name, or both exceed the system-defined maximum length. For example, on Windows-based platforms, paths must be less than 248 characters, and file names must be less than 260 characters.
File at path
contains a colon (:) in the middle of the string.
The file is too short.
Data in the file has the wrong signature.
Properties
Name
Name of the original file.
public string Name { get; }
Property Value
Exceptions
Archive has been disposed and cannot be used.
UncompressedSize
Gets size of an original file.
public ulong UncompressedSize { get; }
Property Value
Remarks
During decompression, this property may contain incorrect size. If the uncompressed file size exceeds 4GB, this property will give a wrong value due to the 32-bit limit in header.
Exceptions
Archive has been disposed and cannot be used.
Methods
Dispose()
Performs application-defined tasks associated with freeing, releasing, or resetting unmanaged resources.
public void Dispose()
Dispose(bool)
Performs application-defined tasks associated with freeing, releasing, or resetting unmanaged resources.
protected virtual void Dispose(bool disposing)
Parameters
disposing
bool
Whether managed resources should be disposed.
Extract(Stream)
Extracts the archive to the stream provided.
public void Extract(Stream destination)
Parameters
destination
Stream
Destination stream. Must be writable.
Examples
using (var archive = new GzipArchive("archive.gz"))
{
archive.Extract(httpResponseStream);
}
Exceptions
destination
does not support writing.
Stream is corrupted and does not contain valid data.
Archive has been disposed and cannot be used.
In .NET Framework 4.0 and above: Thrown when the extraction is canceled via the provided cancellation token.
Extract(string)
Extracts the archive to the file by path.
public FileInfo Extract(string path)
Parameters
path
string
The path to destination file. If the file already exists, it will be overwritten.
Returns
Info of the extracted file.
Exceptions
path
is null.
The caller does not have the required permission to access.
The path
is empty, contains only white spaces, or contains invalid characters.
Access to file path
is denied.
The specified path
, file name, or both exceed the system-defined maximum length. For example, on Windows-based platforms, paths must be less than 248 characters, and file names must be less than 260 characters.
File at path
contains a colon (:) in the middle of the string.
The file is not found.
The specified path is invalid, such as being on an unmapped drive.
The file is already open.
Archive has been disposed and cannot be used.
In .NET Framework 4.0 and above: Thrown when the extraction is canceled via the provided cancellation token.
ExtractToDirectory(string)
Extracts content of the archive to the directory provided.
public void ExtractToDirectory(string destinationDirectory)
Parameters
destinationDirectory
string
The path to the directory to place the extracted files in.
Remarks
If the directory does not exist, it will be created.
Exceptions
destinationDirectory
is null.
The specified path, file name, or both exceed the system-defined maximum length. For example, on Windows-based platforms, paths must be less than 248 characters and file names must be less than 260 characters.
The caller does not have the required permission to access the existing directory.
If the directory does not exist, path contains a colon character (:) that is not part of a drive label (“C:").
destinationDirectory
is a zero-length string, contains only white space, or contains one or more invalid characters. You can query for invalid characters by using the System.IO.Path.GetInvalidPathChars method.
-or- path is prefixed with, or contains, only a colon character (:).
The directory specified by path is a file. -or- The network name is not known.
Archive has been disposed and cannot be used.
In .NET Framework 4.0 and above: Thrown when the extraction is canceled via the provided cancellation token.
Open()
Opens the archive for extraction and provides a stream with archive content.
public Stream Open()
Returns
The stream that represents the contents of the archive.
Examples
Extracts the archive and copies extracted content to file stream.
using (var archive = new GzipArchive("archive.gz"))
{
using (var extracted = File.Create("data.bin"))
{
using (var unpacked = archive.Open())
{
byte[] b = new byte[8192];
int bytesRead;
while (0 < (bytesRead = unpacked.Read(b, 0, b.Length)))
extracted.Write(b, 0, bytesRead);
}
}
}
You may use Stream.CopyTo method for .NET 4.0 and higher: `unpacked.CopyTo(extracted);`
Remarks
Read from the stream to get the original content of a file. See examples section.
Exceptions
Archive has been disposed and cannot be used.
Save(Stream)
Saves archive to the stream provided.
public void Save(Stream outputStream)
Parameters
outputStream
Stream
Destination stream.
Examples
Write compressed data to http response stream.
using (var archive = new GzipArchive())
{
archive.SetSource(new FileInfo("data.bin"));
archive.Save(httpResponse.OutputStream);
}
Remarks
outputStream
must be writable.
Exceptions
outputStream
is not writable.
Archive has been disposed and cannot be used.
Source has not been supplied.
Save(string)
Saves archive to the destination file provided.
public void Save(string destinationFileName)
Parameters
destinationFileName
string
The path of the archive to be created. If the specified file name points to an existing file, it will be overwritten.
Examples
using (var archive = new GzipArchive())
{
archive.SetSource("data.bin");
archive.Save("archive.gz");
}
Exceptions
destinationFileName
is null.
The caller does not have the required permission to access
The destinationFileName
is empty, contains only white spaces, or contains invalid characters.
Access to file destinationFileName
is denied.
The specified destinationFileName
, file name, or both exceed the system-defined maximum length. For example, on Windows-based platforms, paths must be less than 248 characters, and file names must be less than 260 characters.
File at destinationFileName
contains a colon (:) in the middle of the string.
Archive has been disposed and cannot be used.
SetSource(Stream)
Sets the content to be compressed within the archive.
public void SetSource(Stream source)
Parameters
source
Stream
The input stream for the archive.
Examples
using (var archive = new GzipArchive())
{
archive.SetSource(new MemoryStream(new byte[] { 0x00, 0xFF }));
archive.Save("archive.gz");
}
Exceptions
Archive has been disposed and cannot be used.
SetSource(FileInfo)
Sets the content to be compressed within the archive.
public void SetSource(FileInfo fileInfo)
Parameters
fileInfo
FileInfo
The reference to a file to be compressed.
Examples
using (var archive = new GzipArchive())
{
archive.SetSource(new FileInfo("data.bin"));
archive.Save("archive.gz");
}
Exceptions
Archive has been disposed and cannot be used.
SetSource(string)
Sets the content to be compressed within the archive.
public void SetSource(string path)
Parameters
path
string
Path to file to be compressed.
Examples
using (var archive = new GzipArchive())
{
archive.SetSource("data.bin");
archive.Save("archive.gz");
}
Exceptions
path
is null.
The caller does not have the required permission to access
The path
is empty, contains only white spaces, or contains invalid characters.
Access to file path
is denied.
The specified path
, file name, or both exceed the system-defined maximum length. For example, on Windows-based platforms, paths must be less than 248 characters, and file names must be less than 260 characters.
File at path
contains a colon (:) in the middle of the string.
Archive has been disposed and cannot be used.
SetSource(TarArchive)
Sets the content to be compressed within the archive.
public void SetSource(TarArchive tarArchive)
Parameters
tarArchive
TarArchive
Tar archive to be compressed.
Examples
using (var tarArchive = new TarArchive())
{
tarArchive.CreateEntry("first.bin", "data1.bin");
tarArchive.CreateEntry("second.bin", "data2.bin");
using (var gzippedArchive = new GzipArchive())
{
gzippedArchive.SetSource(tarArchive);
gzippedArchive.Save("archive.tar.gz");
}
}
Remarks
Use this method to compose joint tar.gz archive.
Exceptions
Archive has been disposed and cannot be used.