Class GzipArchive
- java.lang.Object
-
- com.aspose.zip.GzipArchive
-
- All Implemented Interfaces:
IArchive,IArchiveFileEntry,AutoCloseable
public class GzipArchive extends Object implements IArchive, IArchiveFileEntry, AutoCloseable
This class represents a gzip archive file. Use it to compose or extract gzip archives.
Gzip compression algorithm is based on the DEFLATE algorithm, which is a combination of LZ77 and Huffman coding.
-
-
Constructor Summary
Constructors Constructor Description GzipArchive()Initializes a new instance of theGzipArchiveclass prepared for compressing.GzipArchive(InputStream sourceStream)Initializes a new instance of theGzipArchiveclass prepared for decompressing.GzipArchive(InputStream sourceStream, boolean parseHeader)Initializes a new instance of theGzipArchiveclass prepared for decompressing.GzipArchive(InputStream sourceStream, GzipLoadOptions options)Initializes a new instance of theGzipArchiveclass prepared for decompressing.GzipArchive(String path)Initializes a new instance of theGzipArchiveclass.GzipArchive(String path, boolean parseHeader)Initializes a new instance of theGzipArchiveclass.GzipArchive(String path, GzipLoadOptions options)Initializes a new instance of theGzipArchiveclass 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 gzip archive.ArchiveFormatgetFormat()Gets the archive format.LonggetLength()Gets size of an original file.StringgetName()The name of the original file.longgetUncompressedSize()Gets size of an 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(String destinationFileName)Saves archive to the destination file provided.voidsetSource(TarArchive tarArchive)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
-
GzipArchive
public GzipArchive()
Initializes a new instance of the
GzipArchiveclass prepared for compressing.The following example shows how to compress a file.
try (GzipArchive archive = new GzipArchive()) { archive.setSource("data.bin"); archive.save("archive.gz"); }
-
GzipArchive
public GzipArchive(InputStream sourceStream)
Initializes a new instance of the
GzipArchiveclass prepared for decompressing.Open an archive from a stream and extract it to a
ByteArrayOutputStreamByteArrayOutputStream ms = new ByteArrayOutputStream(); try (GzipArchive archive = new GzipArchive(Files.newInputStream(java.nio.file.Paths.get("archive.gz")))) { byte[] b = new byte[8192]; int bytesRead; InputStream archiveStream = archive.open(); while (0 < (bytesRead = archiveStream.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.
-
GzipArchive
public GzipArchive(InputStream sourceStream, boolean parseHeader)
Initializes a new instance of the
GzipArchiveclass prepared for decompressing.Open an archive from a stream and extract it to a
ByteArrayOutputStreamByteArrayOutputStream ms = new ByteArrayOutputStream(); try (GzipArchive archive = new GzipArchive(Files.newInputStream(java.nio.file.Paths.get("archive.gz")))) { byte[] b = new byte[8192]; int bytesRead; InputStream archiveStream = archive.open(); while (0 < (bytesRead = archiveStream.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.parseHeader- Whether to parse stream header to figure out properties, including name. Makes sense for seekable stream only.
-
GzipArchive
public GzipArchive(InputStream sourceStream, GzipLoadOptions options)
Initializes a new instance of the
GzipArchiveclass prepared for decompressing.Open an archive from a stream and extract it to a
MemoryStreamByteArrayOutputStream ms = new ByteArrayOutputStream(); GzipLoadOptions options = new GzipLoadOptions(); try (GzipArchive archive = new GzipArchive(new FileInputStream("archive.gz"), options)) { archive.extract(ms); } catch (IOException ex) { }This constructor does not decompress. See
open()method for decompressing.- Parameters:
sourceStream- The source of the archive.options- Options to load the archive with.- Throws:
com.aspose.ms.System.ArgumentNullException-sourceStreamis null.com.aspose.ms.System.IO.EndOfStreamException-sourceStreamis too short.com.aspose.ms.System.IO.InvalidDataException- ThesourceStreamhas wrong signature.
-
GzipArchive
public GzipArchive(String path, GzipLoadOptions options)
Initializes a new instance of the
GzipArchiveclass prepared for decompressing.Open an archive from file by path and extract it to a
MemoryStreamByteArrayOutputStream ms = new ByteArrayOutputStream(); GzipLoadOptions options = new GzipLoadOptions(); try (GzipArchive archive = new GzipArchive("archive.gz", options)) { archive.extract(ms); }This constructor does not decompress. See
open()method for decompressing.- Parameters:
path- The path to the archive file.options- Options to load the archive with.- Throws:
com.aspose.ms.System.ArgumentNullException-pathis null.com.aspose.ms.System.SecurityException- The caller does not have the required permission to accesscom.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.com.aspose.ms.System.IO.EndOfStreamException- The file is too short.com.aspose.ms.System.IO.InvalidDataException- Data in the file has the wrong signature.
-
GzipArchive
public GzipArchive(String path)
Initializes a new instance of the
GzipArchiveclass.Open an archive from file by path and extract it to a
MemoryStreamByteArrayOutputStream ms = new ByteArrayOutputStream(); try (GzipArchive archive = new GzipArchive("archive.gz")) { byte[] b = new byte[8192]; int bytesRead; InputStream archiveStream = archive.open(); while (0 < (bytesRead = archiveStream.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.
-
GzipArchive
public GzipArchive(String path, boolean parseHeader)
Initializes a new instance of the
GzipArchiveclass.Open an archive from file by path and extract it to a
MemoryStreamByteArrayOutputStream ms = new ByteArrayOutputStream(); try (GzipArchive archive = new GzipArchive("archive.gz")) { byte[] b = new byte[8192]; int bytesRead; InputStream archiveStream = archive.open(); while (0 < (bytesRead = archiveStream.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.parseHeader- Whether to parse stream header to figure out properties, including name. Makes sense for seekable stream only.- 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 gzip archive.- Specified by:
getFileEntriesin interfaceIArchive- Returns:
- entries of
IArchiveFileEntrytype constituting the gzip archive.
-
getFormat
public final ArchiveFormat getFormat()
Gets the archive format.
-
getUncompressedSize
public final long getUncompressedSize()
Gets size of an original file.
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.
- Returns:
- size of an original file.
-
getName
public final String getName()
The name of the original file.
- Specified by:
getNamein interfaceIArchiveFileEntry- Returns:
- the name of the original file
-
getLength
public final Long getLength()
Gets size of an original file.
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.
- Specified by:
getLengthin interfaceIArchiveFileEntry- Returns:
- size of an original file
-
setSource
public final void setSource(InputStream source)
Sets the content to be compressed within the archive.
try (GzipArchive archive = new GzipArchive()) { archive.setSource(new ByteArrayInputStream(new byte[] { 0x00, (byte) 0xFF })); archive.save("archive.gz"); }- 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 (GzipArchive archive = new GzipArchive()) { archive.setSource(new File("data.bin")); archive.save("archive.gz"); }- 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.
Open an archive from file by path and extract it to a
MemoryStreamtry (GzipArchive archive = new GzipArchive()) { archive.setSource("data.bin"); archive.save("archive.gz"); }- 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 (GzipArchive gzippedArchive = new GzipArchive()) { gzippedArchive.setSource(tarArchive); gzippedArchive.save("archive.tar.gz"); } }Use this method to compose joint tar.gz archive.
- Parameters:
tarArchive- Tar archive to be compressed.
-
open
public final InputStream open()
Opens the archive for extraction and provides a stream with archive content.
Extracts the archive and copies extracted content to file stream.
try (GzipArchive archive = new GzipArchive("archive.gz")) { try (FileOutputStream extracted = new FileOutputStream("data.bin")) { InputStream unpacked = archive.open(); byte[] b = new byte[8192]; int bytesRead; while (0 < (bytesRead = unpacked.read(b, 0, b.length))) { extracted.write(b, 0, bytesRead); } } } catch (IOException ex) { System.out.println(ex); }Read from the stream to get the original content of a 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 (GzipArchive archive = new GzipArchive("archive.gz")) { archive.extract(httpResponseStream); }- Specified by:
extractin interfaceIArchiveFileEntry- Parameters:
destination- Destination stream. Must be writable.- Throws:
com.aspose.ms.System.ArgumentException-destinationdoes not support writing.
-
save
public final void save(OutputStream outputStream)
Saves archive to the stream provided.
Writes compressed data to http response stream.
try (GzipArchive archive = new GzipArchive()) { archive.setSource(new File("data.bin")); archive.save(httpResponseStream); }- Parameters:
outputStream- Destination stream.outputStreammust be writable.- Throws:
com.aspose.ms.System.ArgumentException-outputStreamis not writable.com.aspose.ms.System.InvalidOperationException- Source has not been supplied.
-
save
public final void save(String destinationFileName)
Saves archive to the destination file provided.
try (GzipArchive archive = new GzipArchive()) { archive.setSource("data.bin"); archive.save("archive.gz"); }- 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-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.
-
close
public void close()
- Specified by:
closein interfaceAutoCloseable- Specified by:
closein interfaceIArchive
-
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
-
-