Class ZstandardArchive
- java.lang.Object
-
- com.aspose.zip.ZstandardArchive
-
- All Implemented Interfaces:
IArchive,IArchiveFileEntry,AutoCloseable
public class ZstandardArchive extends Object implements IArchiveFileEntry, IArchive, AutoCloseable
This class represents a Zstandard archive file. Use it to compose Zstandard archives.
-
-
Constructor Summary
Constructors Constructor Description ZstandardArchive()Initializes a new instance of theZstandardArchiveclass prepared for compressing.ZstandardArchive(InputStream sourceStream)Initializes a new instance of theZstandardArchiveclass prepared for decompressing.ZstandardArchive(InputStream sourceStream, ZstandardLoadOptions options)Initializes a new instance of theZstandardArchiveclass prepared for decompressing.ZstandardArchive(String path)Initializes a new instance of theZstandardArchiveclass.ZstandardArchive(String path, ZstandardLoadOptions options)Initializes a new instance of theZstandardArchiveclass.
-
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 zstandard archive.ArchiveFormatgetFormat()Gets the archive format.LonggetLength()Gets the length of the entry in bytes.StringgetName()Gets the name of the entry within archive.InputStreamopen()Opens the archive for extraction and provides a stream with archive content.voidsave(File destination)Saves archive to the destination file provided.voidsave(File destination, ZstandardSaveOptions settings)Saves archive to the destination file provided.voidsave(OutputStream outputStream)Saves archive to the stream provided.voidsave(OutputStream outputStream, ZstandardSaveOptions settings)Saves archive to the stream provided.voidsave(String destinationFileName)Saves archive to the destination file provided.voidsave(String destinationFileName, ZstandardSaveOptions settings)Saves archive to the destination file provided.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
-
ZstandardArchive
public ZstandardArchive()
Initializes a new instance of the
ZstandardArchiveclass prepared for compressing.The following example shows how to compress a file.
try (ZstandardArchive archive = new ZstandardArchive()) { archive.setSource("data.bin"); archive.save("archive.zst"); }
-
ZstandardArchive
public ZstandardArchive(InputStream sourceStream)
Initializes a new instance of the
ZstandardArchiveclass prepared for decompressing.Open an archive from a stream and extract it to a
ByteArrayOutputStreamByteArrayOutputStream ms = new ByteArrayOutputStream(); try (ZstandardArchive archive = new ZstandardArchive(new FileInputStream("archive.zst"))) { 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
-
ZstandardArchive
public ZstandardArchive(InputStream sourceStream, ZstandardLoadOptions options)
Initializes a new instance of the
ZstandardArchiveclass prepared for decompressing.Open an archive from a stream and extract it to a
ByteArrayOutputStreamByteArrayOutputStream ms = new ByteArrayOutputStream(); try (ZstandardArchive archive = new ZstandardArchive(new FileInputStream("archive.zst"))) { 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 archiveoptions- the options to load archive with
-
ZstandardArchive
public ZstandardArchive(String path)
Initializes a new instance of the
ZstandardArchiveclass.Open an archive from file by path and extract it to a
ByteArrayOutputStreamByteArrayOutputStream ms = new ByteArrayOutputStream(); try (ZstandardArchive archive = new ZstandardArchive("archive.zst")) { 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.
-
ZstandardArchive
public ZstandardArchive(String path, ZstandardLoadOptions options)
Initializes a new instance of the
ZstandardArchiveclass.Open an archive from file by path and extract it to a
ByteArrayOutputStreamByteArrayOutputStream ms = new ByteArrayOutputStream(); try (ZstandardArchive archive = new ZstandardArchive("archive.zst")) { 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 fileoptions- 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
-
getFormat
public final ArchiveFormat getFormat()
Gets the archive format.
-
extract
public final void extract(OutputStream destination)
Extracts the archive to the stream provided.
try (ZstandardArchive archive = new ZstandardArchive("archive.zst")) { archive.extract(httpResponseStream); }- Specified by:
extractin interfaceIArchiveFileEntry- Parameters:
destination- destination stream. Must be writable- Throws:
com.aspose.ms.System.ArgumentException-destinationdoes not support writing.
-
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
- 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
-
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 (ZstandardArchive archive = new ZstandardArchive("archive.zst")) { 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 the file. See examples section.
- Returns:
- the stream that represents the contents of the archive
-
setSource
public final void setSource(InputStream source)
Sets the content to be compressed within the archive.
try (ZstandardArchive archive = new ZstandardArchive()) { archive.setSource(new ByteArrayInputStream(new byte[] { 0x00, (byte) 0xFF })); archive.save("archive.zst"); }- 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 (ZstandardArchive archive = new ZstandardArchive()) { archive.setSource(new File("data.bin")); archive.save("archive.zst"); }- 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 (ZstandardArchive archive = new ZstandardArchive()) { archive.setSource("data.bin"); archive.save("archive.zst"); }- 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.
-
getFileEntries
public final Iterable<IArchiveFileEntry> getFileEntries()
Gets entries of
IArchiveFileEntrytype constituting the zstandard archive.- Specified by:
getFileEntriesin interfaceIArchive- Returns:
- entries of
IArchiveFileEntrytype constituting the zstandard archive
-
getLength
public final Long getLength()
Gets the length of the entry in bytes.- Specified by:
getLengthin interfaceIArchiveFileEntry- Returns:
- the length of the entry in bytes
-
getName
public final String getName()
Gets the name of the entry within archive.
- Specified by:
getNamein interfaceIArchiveFileEntry- Returns:
- the name of the entry within archive
-
save
public final void save(OutputStream outputStream)
Saves archive to the stream provided.
Write compressed data to http response stream.
try (ZstandardArchive archive = new ZstandardArchive()) { archive.setSource(new File("data.bin")); archive.save(outputStream); }- Parameters:
outputStream- the destination stream- Throws:
com.aspose.ms.System.ArgumentException-outputStreamis not writable.com.aspose.ms.System.InvalidOperationException- Source has not been supplied.
-
save
public final void save(OutputStream outputStream, ZstandardSaveOptions settings)
Saves archive to the stream provided.
Write compressed data to http response stream.
try (ZstandardArchive archive = new ZstandardArchive()) { archive.setSource(new File("data.bin")); archive.save(outputStream); }- Parameters:
outputStream- the destination streamsettings- the settings for archive composition- 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 (ZstandardArchive archive = new ZstandardArchive()) { archive.setSource(new File("data.bin")); archive.save("result.zst"); }- 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, ZstandardSaveOptions settings)
Saves archive to the destination file provided.
try (ZstandardArchive archive = new ZstandardArchive()) { archive.setSource(new File("data.bin")); archive.save("result.zst"); }- Parameters:
destinationFileName- the path of the archive to be created. If the specified file name points to an existing file, it will be overwrittensettings- the settings for archive composition- 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(File destination)
Saves archive to the destination file provided.
try (ZstandardArchive archive = new ZstandardArchive()) { archive.setSource(new File("data.bin")); archive.save(new File("archive.zst")); }- Parameters:
destination- the file which will be opened as destination stream- Throws:
com.aspose.ms.System.SecurityException- The caller does not have the required permission to open thedestination.com.aspose.ms.System.ArgumentException- The file path is empty or contains only white spaces.com.aspose.ms.System.IO.FileNotFoundException- The file is not found.com.aspose.ms.System.ArgumentNullException-destinationis null.com.aspose.ms.System.IO.DirectoryNotFoundException- The specified path is invalid, such as being on an unmapped drive.com.aspose.ms.System.IO.IOException- The file is already open.
-
save
public final void save(File destination, ZstandardSaveOptions settings)
Saves archive to the destination file provided.
try (ZstandardArchive archive = new ZstandardArchive()) { archive.setSource(new File("data.bin")); archive.save(new File("archive.zst")); }- Parameters:
destination- the file, which will be opened as destination streamsettings- the settings for archive composition- Throws:
com.aspose.ms.System.SecurityException- The caller does not have the required permission to open thedestination.com.aspose.ms.System.ArgumentException- The file path is empty or contains only white spaces.com.aspose.ms.System.IO.FileNotFoundException- The file is not found.com.aspose.ms.System.ArgumentNullException-destinationis null.com.aspose.ms.System.IO.DirectoryNotFoundException- The specified path is invalid, such as being on an unmapped drive.com.aspose.ms.System.IO.IOException- The file is already open.
-
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.
-
-