Class Bzip2Archive
Namespace: Aspose.Zip.Bzip2
Assembly: Aspose.Zip.dll (25.2.0)
This class represents bzip2 archive file. Use it to compose or extract bzip2 archives.
public class Bzip2Archive : 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
bzip2 compresses files using the Burrows-Wheeler block sorting text compression algorithm, and Huffman coding. See more: https://en.wikipedia.org/wiki/Bzip2
Constructors
Bzip2Archive()
Initializes a new instance of the Aspose.Zip.Bzip2.Bzip2Archive class prepared for compressing.
public Bzip2Archive()
Examples
The following example shows how to compress a file.
using (Bzip2Archive archive = new Bzip2Archive())
{
archive.SetSource("data.bin");
archive.Save("archive.bz2");
}
Bzip2Archive(Stream, Bzip2LoadOptions)
Initializes a new instance of the Aspose.Zip.Bzip2.Bzip2Archive class prepared for decompressing.
public Bzip2Archive(Stream sourceStream, Bzip2LoadOptions loadOptions = null)
Parameters
sourceStream
Stream
The source of the archive.
loadOptions
Bzip2LoadOptions
The options to load archive with.
Examples
Open an archive from a stream and extract it to a MemoryStream
var ms = new MemoryStream();
using (Bzip2Archive archive = new Bzip2Archive(File.OpenRead("archive.bz2")))
archive.Open().CopyTo(ms);
Remarks
This constructor does not decompress. See Aspose.Zip.Bzip2.Bzip2Archive.Open method for decompressing.
Exceptions
Premature stream end.
Wrong signature bytes.
Bzip2Archive(string, Bzip2LoadOptions)
Initializes a new instance of the Aspose.Zip.Bzip2.Bzip2Archive class prepared for decompressing.
public Bzip2Archive(string path, Bzip2LoadOptions loadOptions = null)
Parameters
path
string
The path to the archive file.
loadOptions
Bzip2LoadOptions
The options to load archive with.
Examples
Open an archive from file by path and extract it to a MemoryStream
var ms = new MemoryStream();
using (Bzip2Archive archive = new Bzip2Archive("archive.bz2"))
archive.Open().CopyTo(ms);
Remarks
This constructor does not decompress. See Aspose.Zip.Bzip2.Bzip2Archive.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 not found.
The specified path is invalid, such as being on an unmapped drive.
The file is already open.
Premature stream end.
Wrong signature bytes.
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 (Bzip2Archive archive = new Bzip2Archive("archive.bz2"))
{
archive.Extract(httpResponseStream);
}
Exceptions
destination
does not support writing.
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 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.
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 existing directory.
If 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.
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
Usage:
Stream decompressed = archive.Open();
.NET 4.0 and higher - use Stream.CopyTo method:
decompressed.CopyTo(httpResponse.OutputStream)
.NET 3.5 and before - copy bytes manually:
byte[] buffer = new byte[8192];
int bytesRead;
while (0 < (bytesRead = decompressed.Read(buffer, 0, buffer.Length)))
fileStream.Write(buffer, 0, bytesRead);
```</p>
#### Remarks
Read from the stream to get original content of file. See examples section.
### <a id="Aspose_Zip_Bzip2_Bzip2Archive_Save_System_IO_Stream_Aspose_Zip_Bzip2_Bzip2SaveOptions_"></a> Save\(Stream, Bzip2SaveOptions\)
Saves archive to the stream provided.
```csharp
public void Save(Stream outputStream, Bzip2SaveOptions saveOptions = null)
Parameters
outputStream
Stream
Destination stream.
saveOptions
Bzip2SaveOptions
Options for saving a bzip2 archive. If not specified, 900 Kb block size would be used.
Examples
Writes compressed data to http response stream.
using (var archive = new Bzip2Archive())
{
archive.SetSource(new FileInfo("data.bin"));
archive.Save(httpResponse.OutputStream);
}
Remarks
outputStream
must be writable.
Exceptions
Source of data to be archived has not been provided.
outputStream
is not writable.
File source is read-only or is a directory.
The specified file source path is invalid, such as being on an unmapped drive.
The File source is already open.
Save(string, Bzip2SaveOptions)
Saves archive to destination file provided.
public void Save(string destinationFileName, Bzip2SaveOptions saveOptions = null)
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.
saveOptions
Bzip2SaveOptions
Options for saving a bzip2 archive. If not specified, 900 Kb block size would be used.
Examples
Writes compressed data to file.
using (var archive = new Bzip2Archive())
{
archive.SetSource(new FileInfo("data.bin"));
archive.Save("data.bz2");
}
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.
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 (Bzip2Archive archive = new Bzip2Archive())
{
archive.SetSource(new MemoryStream(new byte[] { 0x00,0xFF }));
archive.Save("archive.bz2");
}
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 (Bzip2Archive archive = new Bzip2Archive())
{
archive.SetSource(new FileInfo("data.bin"));
archive.Save("archive.bz2");
}
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 (Bzip2Archive archive = new Bzip2Archive())
{
archive.SetSource("data.bin");
archive.Save("archive.bz2");
}
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.
SetSource(TarArchive, TarFormat)
Sets the content to be compressed within the archive.
public void SetSource(TarArchive tarArchive, TarFormat format = TarFormat.UsTar)
Parameters
tarArchive
TarArchive
Tar archive to be compressed.
format
TarFormat
Defines tar header format.
Examples
using (var tarArchive = new TarArchive())
{
tarArchive.CreateEntry("first.bin", "data1.bin");
tarArchive.CreateEntry("second.bin", "data2.bin");
using (var bzippedArchive = new Bzip2Archive())
{
bzippedArchive.SetSource(tarArchive);
bzippedArchive.Save("archive.tar.bz2");
}
}
Remarks
Use this method to compose joint tar.bz2 archive.
SetSource(CpioArchive, CpioFormat)
Sets the content to be compressed within the archive.
public void SetSource(CpioArchive cpioArchive, CpioFormat format = CpioFormat.OldAscii)
Parameters
cpioArchive
CpioArchive
Cpio archive to be compressed.
format
CpioFormat
Defines cpio header format.
Examples
using (var cpioArchive = new CpioArchive())
{
cpioArchive.CreateEntry("first.bin", "data1.bin");
cpioArchive.CreateEntry("second.bin", "data2.bin");
using (var bzippedArchive = new Bzip2Archive())
{
bzippedArchive.SetSource(cpioArchive);
bzippedArchive.Save("archive.cpio.bz2");
}
}
Remarks
Use this method to compose joint cpio.bz2 archive.