Bzip2Archive
Inheritance: java.lang.Object
All Implemented Interfaces: com.aspose.zip.IArchive, com.aspose.zip.IArchiveFileEntry, java.lang.AutoCloseable
public class Bzip2Archive implements IArchive, IArchiveFileEntry, AutoCloseable
This class represents bzip2 archive file. Use it to compose or extract bzip2 archives.
bzip2 compresses files using the Burrows-Wheeler block sorting text compression algorithm, and Huffman coding. See more: Bzip2
Constructors
| Constructor | Description |
|---|---|
| Bzip2Archive() | Initializes a new instance of the Bzip2Archive class prepared for compressing. |
| Bzip2Archive(InputStream sourceStream) | Initializes a new instance of the Bzip2Archive class prepared for decompressing. |
| Bzip2Archive(InputStream sourceStream, Bzip2LoadOptions loadOptions) | Initializes a new instance of the Bzip2Archive class prepared for decompressing. |
| Bzip2Archive(String path) | Initializes a new instance of the Bzip2Archive class prepared for decompressing. |
| Bzip2Archive(String path, Bzip2LoadOptions loadOptions) | Initializes a new instance of the Bzip2Archive class prepared for decompressing. |
Methods
| Method | Description |
|---|---|
| close() | {@inheritDoc} |
| extract(OutputStream destination) | Extracts the archive to the stream provided. |
| extract(String path) | Extracts the archive to the file by path. |
| extractToDirectory(String destinationDirectory) | Extracts content of the archive to the directory provided. |
| getFileEntries() | Gets entries of IArchiveFileEntry type constituting the bzip2 archive. |
| getFormat() | Gets the archive format. |
| getLength() | Gets length. |
| getName() | The name of original file. |
| open() | Opens the archive for extraction and provides a stream with archive content. |
| save(OutputStream outputStream) | Saves archive to the stream provided. |
| save(OutputStream outputStream, Bzip2SaveOptions saveOptions) | Saves archive to the stream provided. |
| save(String destinationFileName) | Saves archive to the destination file provided. |
| save(String destinationFileName, Bzip2SaveOptions saveOptions) | Saves archive to the destination file provided. |
| setSource(CpioArchive cpioArchive) | Sets the content to be compressed within the archive. |
| setSource(CpioArchive cpioArchive, CpioFormat format) | Sets the content to be compressed within the archive. |
| setSource(TarArchive tarArchive) | Sets the content to be compressed within the archive. |
| setSource(TarArchive tarArchive, TarFormat format) | Sets the content to be compressed within the archive. |
| setSource(File file) | Sets the content to be compressed within the archive. |
| setSource(InputStream source) | Sets the content to be compressed within the archive. |
| setSource(String path) | Sets the content to be compressed within the archive. |
Bzip2Archive()
public Bzip2Archive()
Initializes a new instance of the Bzip2Archive class prepared for compressing.
The following example shows how to compress a file.
try (Bzip2Archive archive = new Bzip2Archive()) {
archive.setSource("data.bin");
archive.save("archive.bz2");
}
### Bzip2Archive(InputStream sourceStream) {#Bzip2Archive-java.io.InputStream-}
public Bzip2Archive(InputStream sourceStream)
Initializes a new instance of the [Bzip2Archive](../../com.aspose.zip/bzip2archive) class prepared for decompressing.
Open an archive from a stream and extract it to a `ByteArrayOutputStream`
ByteArrayOutputStream ms = new ByteArrayOutputStream();
try (Bzip2Archive archive = new Bzip2Archive(new FileInputStream("archive.bz2"))) {
InputStream decompressed = archive.open();
byte[] b = new byte[8192];
int bytesRead;
while (0 < (bytesRead = decompressed.read(b, 0, b.length))) {
ms.write(b, 0, bytesRead);
}
} catch (IOException ex) {
System.out.println(ex);
}
```
This constructor does not decompress. See [open()](../../com.aspose.zip/bzip2archive\#open--) method for decompressing.
**Parameters:**
| Parameter | Type | Description |
| --- | --- | --- |
| sourceStream | java.io.InputStream | the source of the archive |
### Bzip2Archive(InputStream sourceStream, Bzip2LoadOptions loadOptions) {#Bzip2Archive-java.io.InputStream-com.aspose.zip.Bzip2LoadOptions-}
```
public Bzip2Archive(InputStream sourceStream, Bzip2LoadOptions loadOptions)
```
Initializes a new instance of the [Bzip2Archive](../../com.aspose.zip/bzip2archive) class prepared for decompressing.
Open an archive from a stream and extract it to a `ByteArrayOutputStream`
```
ByteArrayOutputStream ms = new ByteArrayOutputStream();
try (Bzip2Archive archive = new Bzip2Archive(new FileInputStream("archive.bz2"))) {
InputStream decompressed = archive.open();
byte[] b = new byte[8192];
int bytesRead;
while (0 < (bytesRead = decompressed.read(b, 0, b.length))) {
ms.write(b, 0, bytesRead);
}
} catch (IOException ex) {
System.out.println(ex);
}
This constructor does not decompress. See [open()](../../com.aspose.zip/bzip2archive\#open--) method for decompressing.
**Parameters:**
| Parameter | Type | Description |
| --- | --- | --- |
| sourceStream | java.io.InputStream | the source of the archive |
| loadOptions | [Bzip2LoadOptions](../../com.aspose.zip/bzip2loadoptions) | the options to load archive with |
### Bzip2Archive(String path) {#Bzip2Archive-java.lang.String-}
public Bzip2Archive(String path)
Initializes a new instance of the [Bzip2Archive](../../com.aspose.zip/bzip2archive) class prepared for decompressing.
Open an archive from file by path and extract it to a `MemoryStream`
ByteArrayOutputStream ms = new ByteArrayOutputStream();
try (Bzip2Archive archive = new Bzip2Archive("archive.bz2")) {
InputStream decompressed = archive.open();
byte[] b = new byte[8192];
int bytesRead;
while (0 < (bytesRead = decompressed.read(b, 0, b.length))) {
ms.write(b, 0, bytesRead);
}
} catch (IOException ex) {
System.out.println(ex);
}
```
This constructor does not decompress. See [open()](../../com.aspose.zip/bzip2archive\#open--) method for decompressing.
**Parameters:**
| Parameter | Type | Description |
| --- | --- | --- |
| path | java.lang.String | the path to the archive file |
### Bzip2Archive(String path, Bzip2LoadOptions loadOptions) {#Bzip2Archive-java.lang.String-com.aspose.zip.Bzip2LoadOptions-}
```
public Bzip2Archive(String path, Bzip2LoadOptions loadOptions)
```
Initializes a new instance of the [Bzip2Archive](../../com.aspose.zip/bzip2archive) class prepared for decompressing.
Open an archive from file by path and extract it to a `MemoryStream`
```
ByteArrayOutputStream ms = new ByteArrayOutputStream();
try (Bzip2Archive archive = new Bzip2Archive("archive.bz2")) {
InputStream decompressed = archive.open();
byte[] b = new byte[8192];
int bytesRead;
while (0 < (bytesRead = decompressed.read(b, 0, b.length))) {
ms.write(b, 0, bytesRead);
}
} catch (IOException ex) {
System.out.println(ex);
}
This constructor does not decompress. See [open()](../../com.aspose.zip/bzip2archive\#open--) method for decompressing.
**Parameters:**
| Parameter | Type | Description |
| --- | --- | --- |
| path | java.lang.String | the path to the archive file |
| loadOptions | [Bzip2LoadOptions](../../com.aspose.zip/bzip2loadoptions) | the options to load archive with |
### close() {#close--}
public void close()
### extract(OutputStream destination) {#extract-java.io.OutputStream-}
public final void extract(OutputStream destination)
Extracts the archive to the stream provided.
try (Bzip2Archive archive = new Bzip2Archive("archive.bz2")) {
archive.extract(httpResponseStream);
}
```
**Parameters:**
| Parameter | Type | Description |
| --- | --- | --- |
| destination | java.io.OutputStream | destination stream. Must be writable |
### extract(String path) {#extract-java.lang.String-}
```
public final File extract(String path)
```
Extracts the archive to the file by path.
**Parameters:**
| Parameter | Type | Description |
| --- | --- | --- |
| path | java.lang.String | the path to destination file. If the file already exists, it will be overwritten |
**Returns:**
java.io.File - the file info of the extracted file
### extractToDirectory(String destinationDirectory) {#extractToDirectory-java.lang.String-}
```
public final void extractToDirectory(String destinationDirectory)
```
Extracts content of the archive to the directory provided.
**Parameters:**
| Parameter | Type | Description |
| --- | --- | --- |
| destinationDirectory | java.lang.String | The path to the directory to place the extracted files in.
If the directory does not exist, it will be created. |
### getFileEntries() {#getFileEntries--}
```
public final Iterable<IArchiveFileEntry> getFileEntries()
```
Gets entries of [IArchiveFileEntry](../../com.aspose.zip/iarchivefileentry) type constituting the bzip2 archive.
**Returns:**
java.lang.Iterable<com.aspose.zip.IArchiveFileEntry> - entries of [IArchiveFileEntry](../../com.aspose.zip/iarchivefileentry) type constituting the bzip2 archive
### getFormat() {#getFormat--}
```
public final ArchiveFormat getFormat()
```
Gets the archive format.
**Returns:**
[ArchiveFormat](../../com.aspose.zip/archiveformat) - the archive format
### getLength() {#getLength--}
```
public final Long getLength()
```
Gets length.
**Returns:**
java.lang.Long - length
### getName() {#getName--}
```
public final String getName()
```
The name of original file.
**Returns:**
java.lang.String - the name of the original file
### open() {#open--}
```
public final InputStream open()
```
Opens the archive for extraction and provides a stream with archive content.
Usage:
```
try (InputStream decompressed = archive.open()) {
byte[] buffer = new byte[8192];
int bytesRead;
while (0 < (bytesRead = decompressed.read(buffer, 0, buffer.length))) {
fileStream.write(buffer, 0, bytesRead);
}
} catch (IOException ex) {
System.out.println(ex);
}
Read from the stream to get the original content of the file. See examples section.
**Returns:**
java.io.InputStream - the stream that represents the contents of the archive
### save(OutputStream outputStream) {#save-java.io.OutputStream-}
public final void save(OutputStream outputStream)
Saves archive to the stream provided.
Write compressed data to an output stream.
try (Bzip2Archive archive = new Bzip2Archive()) {
archive.setSource(new File("data.bin"));
archive.save(outputStream);
}
```
**Parameters:**
| Parameter | Type | Description |
| --- | --- | --- |
| outputStream | java.io.OutputStream | destination stream. |
### save(OutputStream outputStream, Bzip2SaveOptions saveOptions) {#save-java.io.OutputStream-com.aspose.zip.Bzip2SaveOptions-}
```
public final void save(OutputStream outputStream, Bzip2SaveOptions saveOptions)
```
Saves archive to the stream provided.
Write compressed data to an output stream.
```
try (Bzip2Archive archive = new Bzip2Archive()) {
archive.setSource(new File("data.bin"));
archive.save(outputStream);
}
**Parameters:**
| Parameter | Type | Description |
| --- | --- | --- |
| outputStream | java.io.OutputStream | destination stream. |
| saveOptions | [Bzip2SaveOptions](../../com.aspose.zip/bzip2saveoptions) | options for saving a bzip2 archive. If not specified, 900 Kb block size would be used |
### save(String destinationFileName) {#save-java.lang.String-}
public final void save(String destinationFileName)
Saves archive to the destination file provided.
Writes compressed data to file.
try (Bzip2Archive archive = new Bzip2Archive()) {
archive.setSource(new File("data.bin"));
archive.save("data.bz2");
}
```
**Parameters:**
| Parameter | Type | Description |
| --- | --- | --- |
| destinationFileName | java.lang.String | the path of the archive to be created. If the specified file name points to an existing file, it will be overwritten |
### save(String destinationFileName, Bzip2SaveOptions saveOptions) {#save-java.lang.String-com.aspose.zip.Bzip2SaveOptions-}
```
public final void save(String destinationFileName, Bzip2SaveOptions saveOptions)
```
Saves archive to the destination file provided.
Writes compressed data to file.
```
try (Bzip2Archive archive = new Bzip2Archive()) {
archive.setSource(new File("data.bin"));
archive.save("data.bz2");
}
**Parameters:**
| Parameter | Type | Description |
| --- | --- | --- |
| destinationFileName | java.lang.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](../../com.aspose.zip/bzip2saveoptions) | options for saving a bzip2 archive. If not specified, 900 Kb block size would be used |
### setSource(CpioArchive cpioArchive) {#setSource-com.aspose.zip.CpioArchive-}
public final void setSource(CpioArchive cpioArchive)
Sets the content to be compressed within the archive.
try (CpioArchive cpioArchive = new CpioArchive()) {
cpioArchive.createEntry("first.bin", "data1.bin");
cpioArchive.createEntry("second.bin", "data2.bin");
try (Bzip2Archive bzippedArchive = new Bzip2Archive()) {
bzippedArchive.setSource(cpioArchive);
bzippedArchive.save("archive.cpio.bz2");
}
}
```
Use this method to compose joint cpio.bz2 archive.
**Parameters:**
| Parameter | Type | Description |
| --- | --- | --- |
| cpioArchive | [CpioArchive](../../com.aspose.zip/cpioarchive) | cpio archive to be compressed |
### setSource(CpioArchive cpioArchive, CpioFormat format) {#setSource-com.aspose.zip.CpioArchive-com.aspose.zip.CpioFormat-}
```
public final void setSource(CpioArchive cpioArchive, CpioFormat format)
```
Sets the content to be compressed within the archive.
```
try (CpioArchive cpioArchive = new CpioArchive()) {
cpioArchive.createEntry("first.bin", "data1.bin");
cpioArchive.createEntry("second.bin", "data2.bin");
try (Bzip2Archive bzippedArchive = new Bzip2Archive()) {
bzippedArchive.setSource(cpioArchive);
bzippedArchive.save("archive.cpio.bz2");
}
}
Use this method to compose joint cpio.bz2 archive.
**Parameters:**
| Parameter | Type | Description |
| --- | --- | --- |
| cpioArchive | [CpioArchive](../../com.aspose.zip/cpioarchive) | cpio archive to be compressed |
| format | [CpioFormat](../../com.aspose.zip/cpioformat) | defines cpio header format |
### setSource(TarArchive tarArchive) {#setSource-com.aspose.zip.TarArchive-}
public final void setSource(TarArchive tarArchive)
Sets the content to be compressed within the archive.
try (TarArchive tarArchive = new TarArchive()) {
tarArchive.createEntry("first.bin", "data1.bin");
tarArchive.createEntry("second.bin", "data2.bin");
try (Bzip2Archive bzippedArchive = new Bzip2Archive()) {
bzippedArchive.setSource(tarArchive);
bzippedArchive.save("archive.tar.bz2");
}
}
```
Use this method to compose joint tar.bz2 archive.
**Parameters:**
| Parameter | Type | Description |
| --- | --- | --- |
| tarArchive | [TarArchive](../../com.aspose.zip/tararchive) | tar archive to be compressed |
### setSource(TarArchive tarArchive, TarFormat format) {#setSource-com.aspose.zip.TarArchive-com.aspose.zip.TarFormat-}
```
public final void setSource(TarArchive tarArchive, TarFormat format)
```
Sets the content to be compressed within the archive.
```
try (TarArchive tarArchive = new TarArchive()) {
tarArchive.createEntry("first.bin", "data1.bin");
tarArchive.createEntry("second.bin", "data2.bin");
try (Bzip2Archive bzippedArchive = new Bzip2Archive()) {
bzippedArchive.setSource(tarArchive);
bzippedArchive.save("archive.tar.bz2");
}
}
Use this method to compose joint tar.bz2 archive.
**Parameters:**
| Parameter | Type | Description |
| --- | --- | --- |
| tarArchive | [TarArchive](../../com.aspose.zip/tararchive) | tar archive to be compressed |
| format | [TarFormat](../../com.aspose.zip/tarformat) | defines tar header format |
### setSource(File file) {#setSource-java.io.File-}
public final void setSource(File file)
Sets the content to be compressed within the archive.
try (Bzip2Archive archive = new Bzip2Archive()) {
archive.setSource(new File("data.bin"));
archive.save("archive.bz2");
}
```
**Parameters:**
| Parameter | Type | Description |
| --- | --- | --- |
| file | java.io.File | the reference to a file to be compressed |
### setSource(InputStream source) {#setSource-java.io.InputStream-}
```
public final void setSource(InputStream source)
```
Sets the content to be compressed within the archive.
```
try (Bzip2Archive archive = new Bzip2Archive()) {
archive.setSource(new ByteArrayInputStream(new byte[] { 0x00, (byte) 0xFF }));
archive.save("archive.bz2");
}
**Parameters:**
| Parameter | Type | Description |
| --- | --- | --- |
| source | java.io.InputStream | the input stream for the archive |
### setSource(String path) {#setSource-java.lang.String-}
public final void setSource(String path)
Sets the content to be compressed within the archive.
try (Bzip2Archive archive = new Bzip2Archive()) {
archive.setSource("data.bin");
archive.save("archive.bz2");
}
```
**Parameters:**
| Parameter | Type | Description |
| --- | --- | --- |
| path | java.lang.String | path to file to be compressed |