CabArchive
Inheritance: java.lang.Object
All Implemented Interfaces: com.aspose.zip.ICompressionArchive, java.lang.AutoCloseable
public class CabArchive implements ICompressionArchive, AutoCloseable
This class represents a CAB archive file.
Constructors
| Constructor | Description |
|---|---|
| CabArchive(CabEntrySettings settings) | Initializes a new instance of the CabArchive class prepared for compressing. |
| CabArchive(InputStream sourceStream) | Initializes a new instance of the CabArchive class and composes an entry list can be extracted from the archive. |
| CabArchive(InputStream sourceStream, CabLoadOptions loadOptions) | Initializes a new instance of the CabArchive class and composes an entry list can be extracted from the archive. |
| CabArchive(String path) | Initializes a new instance of the CabArchive class and composes an entry list can be extracted from the archive. |
| CabArchive(String path, CabLoadOptions loadOptions) | Initializes a new instance of the CabArchive class and composes an entry list can be extracted from the archive. |
Methods
| Method | Description |
|---|---|
| close() | {@inheritDoc} |
| createEntries(File directory) | Adds to the archive all files, recursively, from the specified directory. |
| createEntries(File directory, boolean includeRootDirectory) | Adds to the archive all files, recursively, from the specified directory. |
| createEntries(String sourceDirectory) | Adds to the archive all files recursively from the specified directory path. |
| createEntries(String sourceDirectory, boolean includeRootDirectory) | Adds to the archive all files recursively from the specified directory path. |
| createEntry(String name, File fileInfo) | Create a single entry within the archive. |
| createEntry(String name, File fileInfo, CabEntrySettings newEntrySettings) | Create a single entry within the archive. |
| createEntry(String name, InputStream source) | Create a single entry within the archive. |
| createEntry(String name, InputStream source, CabEntrySettings newEntrySettings) | Create a single entry within the archive and specific settings. |
| createEntry(String name, String path) | Create a single entry within the archive. |
| createEntry(String name, String path, CabEntrySettings newEntrySettings) | Create a single entry within the archive. |
| createEntry(String name, Supplier<InputStream> streamProvider) | Create a single entry within the archive. |
| createEntry(String name, Supplier<InputStream> streamProvider, CabEntrySettings newEntrySettings) | Create a single entry within the archive. |
| extractToDirectory(String destinationDirectory) | Extracts all the files in the archive to the directory provided. |
| getEntries() | Gets entries of CabEntry type constituting the archive. |
| getFileEntries() | Gets entries of IArchiveFileEntry type constituting the cab archive. |
| getFormat() | Gets the archive format. |
| save(OutputStream outputStream) | Saves archive to the stream provided. |
| save(OutputStream outputStream, CabSaveOptions saveOptions) | Saves archive to the stream provided with specific options. |
| save(String destinationFileName) | Saves archive to the destination file provided. |
| save(String destinationFileName, CabSaveOptions saveOptions) | Saves archive to the destination file provided. |
CabArchive(CabEntrySettings settings)
public CabArchive(CabEntrySettings settings)
Initializes a new instance of the CabArchive class prepared for compressing.
Compress a file using specific compression settings.
CabEntrySettings settings = new CabEntrySettings(new CabStoreCompressionSettings()));
try (CabArchive archive = new CabArchive(settings))
{
archive.createEntry("entry.bin", "data.bin");
archive.save("archive.cab");
} catch (IOException ex) {
}
Parameters:
| Parameter | Type | Description |
|---|---|---|
| settings | CabEntrySettings | the source of the archive |
CabArchive(InputStream sourceStream)
public CabArchive(InputStream sourceStream)
Initializes a new instance of the CabArchive class and composes an entry list can be extracted from the archive.
The following example shows how to extract all the entries to a directory.
try (CabArchive archive = new CabArchive(new FileInputStream("archive.cab"))) {
archive.extractToDirectory("C:\\extracted");
} catch (IOException ex) {
}
This constructor does not unpack any entry. See CabEntry.open() method for unpacking.
Parameters:
| Parameter | Type | Description |
|---|---|---|
| sourceStream | java.io.InputStream | the source of the archive |
CabArchive(InputStream sourceStream, CabLoadOptions loadOptions)
public CabArchive(InputStream sourceStream, CabLoadOptions loadOptions)
Initializes a new instance of the CabArchive class and composes an entry list can be extracted from the archive.
The following example shows how to extract all the entries to a directory.
try (CabArchive archive = new CabArchive(new FileInputStream("archive.cab"))) {
archive.extractToDirectory("C:\\extracted");
} catch (IOException ex) {
}
This constructor does not unpack any entry. See CabEntry.open() method for unpacking.
Parameters:
| Parameter | Type | Description |
|---|---|---|
| sourceStream | java.io.InputStream | the source of the archive |
| loadOptions | CabLoadOptions | Options to load existing archive with. |
CabArchive(String path)
public CabArchive(String path)
Initializes a new instance of the CabArchive class and composes an entry list can be extracted from the archive.
The following example shows how to extract all the entries to a directory.
try (CabArchive archive = new CabArchive("archive.cab")) {
archive.extractToDirectory("C:\\extracted");
} catch (IOException ex) {
}
This constructor does not unpack any entry. See CabEntry.open() method for unpacking.
Parameters:
| Parameter | Type | Description |
|---|---|---|
| path | java.lang.String | the path to the archive file |
CabArchive(String path, CabLoadOptions loadOptions)
public CabArchive(String path, CabLoadOptions loadOptions)
Initializes a new instance of the CabArchive class and composes an entry list can be extracted from the archive.
The following example shows how to extract all the entries to a directory.
try (CabArchive archive = new CabArchive("archive.cab")) {
archive.extractToDirectory("C:\\extracted");
} catch (IOException ex) {
}
This constructor does not unpack any entry. See CabEntry.open() method for unpacking.
Parameters:
| Parameter | Type | Description |
|---|---|---|
| path | java.lang.String | the path to the archive file |
| loadOptions | CabLoadOptions | Options to load existing archive with. |
close()
public void close()
createEntries(File directory)
public final CabArchive createEntries(File directory)
Adds to the archive all files, recursively, from the specified directory.
try (var archive = new CabArchive())
{
File directory = new File("C:/Logs");
archive.createEntries(directory);
archive.save("logs.cab");
} catch (IOException ex) {
}
Parameters:
| Parameter | Type | Description |
|---|---|---|
| directory | java.io.File | Directory to compress. |
Returns: CabArchive - The current CabArchive instance.
createEntries(File directory, boolean includeRootDirectory)
public final CabArchive createEntries(File directory, boolean includeRootDirectory)
Adds to the archive all files, recursively, from the specified directory.
try (var archive = new CabArchive())
{
File directory = new File("C:/Logs");
archive.createEntries(directory, false);
archive.save("logs.cab");
} catch (IOException ex) {
}
Parameters:
| Parameter | Type | Description |
|---|---|---|
| directory | java.io.File | Directory to compress. |
| includeRootDirectory | boolean | Indicates whether to include the root directory name in entry paths. |
Returns: CabArchive - The current CabArchive instance.
createEntries(String sourceDirectory)
public final CabArchive createEntries(String sourceDirectory)
Adds to the archive all files recursively from the specified directory path.
try (var archive = new CabArchive())
{
archive.createEntries("C:/Logs");
archive.save("logs.cab");
} catch (IOException ex) {
}
Parameters:
| Parameter | Type | Description |
|---|---|---|
| sourceDirectory | java.lang.String | Directory path to compress. |
Returns: CabArchive - The current CabArchive instance.
createEntries(String sourceDirectory, boolean includeRootDirectory)
public final CabArchive createEntries(String sourceDirectory, boolean includeRootDirectory)
Adds to the archive all files recursively from the specified directory path.
try (var archive = new CabArchive())
{
archive.createEntries("C:/Logs", false);
archive.save("logs.cab");
} catch (IOException ex) {
}
Parameters:
| Parameter | Type | Description |
|---|---|---|
| sourceDirectory | java.lang.String | Directory path to compress. |
| includeRootDirectory | boolean | Indicates whether to include the root directory name in entry paths. |
Returns: CabArchive - The current CabArchive instance.
createEntry(String name, File fileInfo)
public final CabEntry createEntry(String name, File fileInfo)
Create a single entry within the archive.
try (CabArchive archive = new CabArchive())
{
var sourceFile = new java.io.File("logs\\log.txt");
archive.createEntry("log.txt", sourceFile);
archive.save("archive.cab");
} catch (IOException ex) {
}
Parameters:
| Parameter | Type | Description |
|---|---|---|
| name | java.lang.String | The name of the entry. |
| fileInfo | java.io.File | The metadata of file to be compressed. |
The entry name is solely set within name parameter. The file name provided in fileInfo parameter does not affect the entry name. |
Returns: CabEntry - CAB entry instance.
createEntry(String name, File fileInfo, CabEntrySettings newEntrySettings)
public final CabEntry createEntry(String name, File fileInfo, CabEntrySettings newEntrySettings)
Create a single entry within the archive.
try (CabArchive archive = new CabArchive())
{
CabEntrySettings settings = new CabEntrySettings();
File sourceFile = new File("logs\\log.txt");
archive.createEntry("log.txt", sourceFile, settings);
archive.save("archive.cab");
} catch (IOException ex) {
}
Parameters:
| Parameter | Type | Description |
|---|---|---|
| name | java.lang.String | The name of the entry. |
| fileInfo | java.io.File | The metadata of file to be compressed. |
| newEntrySettings | CabEntrySettings | Compression and encryption settings used for added CabEntry item. |
The entry name is solely set within name parameter. The file name provided in fileInfo parameter does not affect the entry name. |
Returns: CabEntry - CAB entry instance.
createEntry(String name, InputStream source)
public final CabEntry createEntry(String name, InputStream source)
Create a single entry within the archive.
try (CabArchive archive = new CabArchive(); FileInputStream stream = new FileInputStream("stream-entry.bin"))
{
archive.createEntry("stream-entry.bin", stream);
archive.save("archive.cab");
} catch (IOException ex) {
}
Parameters:
| Parameter | Type | Description |
|---|---|---|
| name | java.lang.String | The name of the entry. |
| source | java.io.InputStream | The input stream for the entry. |
Returns: CabEntry - Cab entry instance.
createEntry(String name, InputStream source, CabEntrySettings newEntrySettings)
public final CabEntry createEntry(String name, InputStream source, CabEntrySettings newEntrySettings)
Create a single entry within the archive and specific settings.
try (CabArchive archive = new CabArchive(); FileInputStream stream = new FileInputStream("stream-entry.bin"))
{
CabEntrySettings settings = new CabEntrySettings(new CabStoreCompressionSettings());
archive.createEntry("stream-entry.bin", stream, settings);
archive.save("archive.cab");
} catch (IOException ex) {
}
Parameters:
| Parameter | Type | Description |
|---|---|---|
| name | java.lang.String | The name of the entry. |
| source | java.io.InputStream | The input stream for the entry. |
| newEntrySettings | CabEntrySettings | Compression and encryption settings used for added CabEntry item. |
Returns: CabEntry - Cab entry instance.
createEntry(String name, String path)
public final CabEntry createEntry(String name, String path)
Create a single entry within the archive.
try (CabArchive archive = new CabArchive())
{
archive.createEntry("entry.bin", "data.bin");
archive.save("archive.cab");
} catch (IOException ex) {
}
Parameters:
| Parameter | Type | Description |
|---|---|---|
| name | java.lang.String | The name of the entry. |
| path | java.lang.String | The fully qualified name of the new file, or the relative file name to be compressed. |
The entry name is solely set within name parameter. The file name provided in path parameter does not affect the entry name. |
Returns: CabEntry - Cab entry instance.
createEntry(String name, String path, CabEntrySettings newEntrySettings)
public final CabEntry createEntry(String name, String path, CabEntrySettings newEntrySettings)
Create a single entry within the archive.
try (CabArchive archive = new CabArchive())
{
CabEntrySettings settings = new CabEntrySettings();
archive.createEntry("entry.bin", "data.bin", settings);
archive.save("archive.cab");
} catch (IOException ex) {
}
Parameters:
| Parameter | Type | Description |
|---|---|---|
| name | java.lang.String | The name of the entry. |
| path | java.lang.String | The fully qualified name of the new file, or the relative file name to be compressed. |
| newEntrySettings | CabEntrySettings | Compression and encryption settings used for added CabEntry item. |
The entry name is solely set within name parameter. The file name provided in path parameter does not affect the entry name. |
Returns: CabEntry - Cab entry instance.
createEntry(String name, Supplier<InputStream> streamProvider)
public final CabEntry createEntry(String name, Supplier<InputStream> streamProvider)
Create a single entry within the archive.
try (CabArchive archive = new CabArchive())
{
archive.createEntry("log.txt", () -> new FileInputStream("log.txt"));
archive.save("archive.cab");
} catch (IOException ex) {
}
Parameters:
| Parameter | Type | Description |
|---|---|---|
| name | java.lang.String | The name of the entry. |
| streamProvider | java.util.function.Supplier<java.io.InputStream> | The method providing input stream for the entry. |
Returns: CabEntry - CAB entry instance.
createEntry(String name, Supplier<InputStream> streamProvider, CabEntrySettings newEntrySettings)
public final CabEntry createEntry(String name, Supplier<InputStream> streamProvider, CabEntrySettings newEntrySettings)
Create a single entry within the archive.
try (CabArchive archive = new CabArchive())
{
CabEntrySettings settings = new CabEntrySettings();
archive.createEntry("log.txt", () -> new FileInputStream("log.txt"), settings);
archive.save("archive.cab");
} catch (IOException ex) {
}
Parameters:
| Parameter | Type | Description |
|---|---|---|
| name | java.lang.String | The name of the entry. |
| streamProvider | java.util.function.Supplier<java.io.InputStream> | The method providing input stream for the entry. |
| newEntrySettings | CabEntrySettings | Compression and encryption settings used for added CabEntry item. |
Returns: CabEntry - CAB entry instance.
extractToDirectory(String destinationDirectory)
public final void extractToDirectory(String destinationDirectory)
Extracts all the files in the archive to the directory provided.
try (CabArchive archive = new CabArchive("archive.cab")) {
archive.extractToDirectory("C:\\extracted");
} catch (IOException ex) {
}
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 |
getEntries()
public final List<CabEntry> getEntries()
Gets entries of CabEntry type constituting the archive.
Returns: java.util.List<com.aspose.zip.CabEntry> - entries of CabEntry type constituting the archive
getFileEntries()
public final Iterable<IArchiveFileEntry> getFileEntries()
Gets entries of IArchiveFileEntry type constituting the cab archive.
Returns: java.lang.Iterable<com.aspose.zip.IArchiveFileEntry> - entries of IArchiveFileEntry type constituting the cab archive
getFormat()
public final ArchiveFormat getFormat()
Gets the archive format.
Returns: ArchiveFormat - the archive format
save(OutputStream outputStream)
public final void save(OutputStream outputStream)
Saves archive to the stream provided.
try (CabArchive archive = new CabArchive(); FileOutputStream cabFile = new FileOutputStream("archive.cab"))
{
archive.createEntry("entry.bin", "data.bin");
archive.save(cabFile);
} catch (IOException ex) {
}
Parameters:
| Parameter | Type | Description |
|---|---|---|
| outputStream | java.io.OutputStream | Destination stream. |
outputStream must be writable. |
save(OutputStream outputStream, CabSaveOptions saveOptions)
public final void save(OutputStream outputStream, CabSaveOptions saveOptions)
Saves archive to the stream provided with specific options.
try (CabArchive archive = new CabArchive(); FileOutputStream cabFile = new FileOutputStream("archive.cab"))
{
CabSaveOptions options = new CabSaveOptions();
options.setSkipChecksumCalculation(true);
archive.createEntry("entry.bin", "data.bin");
archive.save(cabFile, options);
} catch (IOException ex) {
}
Parameters:
| Parameter | Type | Description |
|---|---|---|
| outputStream | java.io.OutputStream | Destination stream. |
| saveOptions | CabSaveOptions | Options for archive saving. |
outputStream must be writable. |
save(String destinationFileName)
public final void save(String destinationFileName)
Saves archive to the destination file provided.
try (CabArchive archive = new CabArchive())
{
archive.createEntry("entry.bin", "data.bin");
archive.save("archive.cab");
} catch (IOException ex) {
}
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. |
It is possible to save an archive to the same path as it was loaded from. However, this is not recommended because this approach uses copying to a temporary file. |
save(String destinationFileName, CabSaveOptions saveOptions)
public final void save(String destinationFileName, CabSaveOptions saveOptions)
Saves archive to the destination file provided.
try (CabArchive archive = new CabArchive())
{
CabSaveOptions options = new CabSaveOptions();
options.setSkipChecksumCalculation(true);
archive.createEntry("entry.bin", "data.bin");
archive.save("archive.cab", options);
} catch (IOException ex) {
}
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 | CabSaveOptions | Options for archive saving. |
It is possible to save an archive to the same path as it was loaded from. However, this is not recommended because this approach uses copying to a temporary file. |