Class Bzip2Archive
- java.lang.Object
-
- com.aspose.zip.Bzip2Archive
-
- All Implemented Interfaces:
IArchive,IArchiveFileEntry,AutoCloseable
public class Bzip2Archive extends Object 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
-
-
Constructor Summary
Constructors Constructor Description Bzip2Archive()Initializes a new instance of theBzip2Archiveclass prepared for compressing.Bzip2Archive(InputStream sourceStream)Initializes a new instance of theBzip2Archiveclass prepared for decompressing.Bzip2Archive(InputStream sourceStream, Bzip2LoadOptions loadOptions)Initializes a new instance of theBzip2Archiveclass prepared for decompressing.Bzip2Archive(String path)Initializes a new instance of theBzip2Archiveclass prepared for decompressing.Bzip2Archive(String path, Bzip2LoadOptions loadOptions)Initializes a new instance of theBzip2Archiveclass prepared for decompressing.
-
Method Summary
All Methods Instance Methods Concrete Methods Modifier and Type Method Description voidclose()voidextract(OutputStream destination)Extracts the archive to the stream provided.Fileextract(String path)Extracts the archive to the file by path.voidextractToDirectory(String destinationDirectory)Extracts content of the archive to the directory provided.Iterable<IArchiveFileEntry>getFileEntries()Gets entries ofIArchiveFileEntrytype constituting the bzip2 archive.ArchiveFormatgetFormat()Gets the archive format.LonggetLength()Gets length.StringgetName()The name of original file.InputStreamopen()Opens the archive for extraction and provides a stream with archive content.voidsave(OutputStream outputStream)Saves archive to the stream provided.voidsave(OutputStream outputStream, Bzip2SaveOptions saveOptions)Saves archive to the stream provided.voidsave(String destinationFileName)Saves archive to the destination file provided.voidsave(String destinationFileName, Bzip2SaveOptions saveOptions)Saves archive to the destination file provided.voidsetSource(CpioArchive cpioArchive)Sets the content to be compressed within the archive.voidsetSource(CpioArchive cpioArchive, CpioFormat format)Sets the content to be compressed within the archive.voidsetSource(TarArchive tarArchive)Sets the content to be compressed within the archive.voidsetSource(TarArchive tarArchive, TarFormat format)Sets the content to be compressed within the archive.voidsetSource(File file)Sets the content to be compressed within the archive.voidsetSource(InputStream source)Sets the content to be compressed within the archive.voidsetSource(String path)Sets the content to be compressed within the archive.
-
-
-
Constructor Detail
-
Bzip2Archive
public Bzip2Archive()
Initializes a new instance of the
Bzip2Archiveclass 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
public Bzip2Archive(InputStream sourceStream)
Initializes a new instance of the
Bzip2Archiveclass prepared for decompressing.Open an archive from a stream and extract it to a
ByteArrayOutputStreamByteArrayOutputStream 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()method for decompressing.- Parameters:
sourceStream- the source of the archive
-
Bzip2Archive
public Bzip2Archive(InputStream sourceStream, Bzip2LoadOptions loadOptions)
Initializes a new instance of the
Bzip2Archiveclass prepared for decompressing.Open an archive from a stream and extract it to a
ByteArrayOutputStreamByteArrayOutputStream 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()method for decompressing.- Parameters:
sourceStream- the source of the archiveloadOptions- the options to load archive with
-
Bzip2Archive
public Bzip2Archive(String path)
Initializes a new instance of the
Bzip2Archiveclass prepared for decompressing.Open an archive from file by path and extract it to a
MemoryStreamByteArrayOutputStream 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()method for decompressing.- Parameters:
path- the path to the archive file- Throws:
com.aspose.ms.System.ArgumentNullException-pathis null.com.aspose.ms.System.SecurityException- The caller does not have the required permission to access.com.aspose.ms.System.ArgumentException- Thepathis empty, contains only white spaces, or contains invalid characters.com.aspose.ms.System.IO.PathTooLongException- The specifiedpath, 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.com.aspose.ms.System.NotSupportedException- File atpathcontains a colon (:) in the middle of the string.
-
Bzip2Archive
public Bzip2Archive(String path, Bzip2LoadOptions loadOptions)
Initializes a new instance of the
Bzip2Archiveclass prepared for decompressing.Open an archive from file by path and extract it to a
MemoryStreamByteArrayOutputStream 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()method for decompressing.- Parameters:
path- the path to the archive fileloadOptions- the options to load archive with- Throws:
com.aspose.ms.System.ArgumentNullException-pathis null.com.aspose.ms.System.SecurityException- The caller does not have the required permission to access.com.aspose.ms.System.ArgumentException- Thepathis empty, contains only white spaces, or contains invalid characters.com.aspose.ms.System.IO.PathTooLongException- The specifiedpath, 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.com.aspose.ms.System.NotSupportedException- File atpathcontains a colon (:) in the middle of the string.
-
-
Method Detail
-
getFileEntries
public final Iterable<IArchiveFileEntry> getFileEntries()
Gets entries of
IArchiveFileEntrytype constituting the bzip2 archive.- Specified by:
getFileEntriesin interfaceIArchive- Returns:
- entries of
IArchiveFileEntrytype constituting the bzip2 archive
-
getFormat
public final ArchiveFormat getFormat()
Gets the archive format.
-
getName
public final String getName()
The name of original file.
- Specified by:
getNamein interfaceIArchiveFileEntry- Returns:
- the name of the original file
-
getLength
public final Long getLength()
Gets length.- Specified by:
getLengthin interfaceIArchiveFileEntry- Returns:
- length
-
setSource
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:
source- the input stream for the archive
-
setSource
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:
file- the reference to a file to be compressed
-
setSource
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:
path- path to file to be compressed- Throws:
com.aspose.ms.System.ArgumentNullException-pathis null.com.aspose.ms.System.SecurityException- The caller does not have the required permission to access.com.aspose.ms.System.ArgumentException- Thepathis empty, contains only white spaces, or contains invalid characters.com.aspose.ms.System.IO.PathTooLongException- The specifiedpath, 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.com.aspose.ms.System.NotSupportedException- File atpathcontains a colon (:) in the middle of the string.
-
setSource
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:
tarArchive- tar archive to be compressed
-
setSource
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:
tarArchive- tar archive to be compressedformat- defines tar header format
-
setSource
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:
cpioArchive- cpio archive to be compressed
-
setSource
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:
cpioArchive- cpio archive to be compressedformat- defines cpio header format
-
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:
- the stream that represents the contents of the archive
-
extract
public final void extract(OutputStream destination)
Extracts the archive to the stream provided.
try (Bzip2Archive archive = new Bzip2Archive("archive.bz2")) { archive.extract(httpResponseStream); }- Specified by:
extractin interfaceIArchiveFileEntry- Parameters:
destination- destination stream. Must be writable- Throws:
com.aspose.ms.System.ArgumentException-destinationdoes not support writing.
-
extractToDirectory
public final void extractToDirectory(String destinationDirectory)
Extracts content of the archive to the directory provided.
- Specified by:
extractToDirectoryin interfaceIArchive- Parameters:
destinationDirectory- The path to the directory to place the extracted files in.If the directory does not exist, it will be created.
- Throws:
com.aspose.ms.System.ArgumentNullException-destinationDirectoryis null.com.aspose.ms.System.IO.PathTooLongException- 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.com.aspose.ms.System.SecurityException- The caller does not have the required permission to access the existing directory.com.aspose.ms.System.NotSupportedException- If the directory does not exist, the path contains a colon character (:) that is not part of a drive label ("C:\").com.aspose.ms.System.ArgumentException-destinationDirectoryis a zero-length string, contains only white space, or contains one or more invalid characters.com.aspose.ms.System.IO.IOException- The directory specified by path is a file. -or- The network name is not known.
-
extract
public final File extract(String path)
Extracts the archive to the file by path.
- Specified by:
extractin interfaceIArchiveFileEntry- Parameters:
path- the path to destination file. If the file already exists, it will be overwritten- Returns:
- the file info of the extracted file
-
save
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:
outputStream- destination stream.- Throws:
com.aspose.ms.System.InvalidOperationException- The source of data to be archived has not been provided.com.aspose.ms.System.ArgumentException-outputStreamis not writable.com.aspose.ms.System.IO.DirectoryNotFoundException- The specified file source path is invalid, such as being on an unmapped drive.com.aspose.ms.System.IO.IOException- The File source is already open.
-
save
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:
outputStream- destination stream.saveOptions- options for saving a bzip2 archive. If not specified, 900 Kb block size would be used- Throws:
com.aspose.ms.System.InvalidOperationException- The source of data to be archived has not been provided.com.aspose.ms.System.ArgumentException-outputStreamis not writable.com.aspose.ms.System.IO.DirectoryNotFoundException- The specified file source path is invalid, such as being on an unmapped drive.com.aspose.ms.System.IO.IOException- The File source is already open.
-
save
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:
destinationFileName- the path of the archive to be created. If the specified file name points to an existing file, it will be overwritten- Throws:
com.aspose.ms.System.ArgumentNullException-destinationFileNameis null.com.aspose.ms.System.SecurityException- The caller does not have the required permission to access.com.aspose.ms.System.ArgumentException- ThedestinationFileNameis empty, contains only white spaces, or contains invalid characters.com.aspose.ms.System.IO.PathTooLongException- The specifieddestinationFileName, 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.com.aspose.ms.System.NotSupportedException- File atdestinationFileNamecontains a colon (:) in the middle of the string.
-
save
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:
destinationFileName- the path of the archive to be created. If the specified file name points to an existing file, it will be overwrittensaveOptions- options for saving a bzip2 archive. If not specified, 900 Kb block size would be used- Throws:
com.aspose.ms.System.ArgumentNullException-destinationFileNameis null.com.aspose.ms.System.SecurityException- The caller does not have the required permission to access.com.aspose.ms.System.ArgumentException- ThedestinationFileNameis empty, contains only white spaces, or contains invalid characters.com.aspose.ms.System.IO.PathTooLongException- The specifieddestinationFileName, 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.com.aspose.ms.System.NotSupportedException- File atdestinationFileNamecontains a colon (:) in the middle of the string.
-
close
public void close()
- Specified by:
closein interfaceAutoCloseable- Specified by:
closein interfaceIArchive
-
-