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

ConstructorDescription
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

MethodDescription
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:

ParameterTypeDescription
settingsCabEntrySettingsthe 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:

ParameterTypeDescription
sourceStreamjava.io.InputStreamthe 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:

ParameterTypeDescription
sourceStreamjava.io.InputStreamthe source of the archive
loadOptionsCabLoadOptionsOptions 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:

ParameterTypeDescription
pathjava.lang.Stringthe 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:

ParameterTypeDescription
pathjava.lang.Stringthe path to the archive file
loadOptionsCabLoadOptionsOptions 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:

ParameterTypeDescription
directoryjava.io.FileDirectory 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:

ParameterTypeDescription
directoryjava.io.FileDirectory to compress.
includeRootDirectorybooleanIndicates 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:

ParameterTypeDescription
sourceDirectoryjava.lang.StringDirectory 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:

ParameterTypeDescription
sourceDirectoryjava.lang.StringDirectory path to compress.
includeRootDirectorybooleanIndicates 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:

ParameterTypeDescription
namejava.lang.StringThe name of the entry.
fileInfojava.io.FileThe 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:

ParameterTypeDescription
namejava.lang.StringThe name of the entry.
fileInfojava.io.FileThe metadata of file to be compressed.
newEntrySettingsCabEntrySettingsCompression 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:

ParameterTypeDescription
namejava.lang.StringThe name of the entry.
sourcejava.io.InputStreamThe 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:

ParameterTypeDescription
namejava.lang.StringThe name of the entry.
sourcejava.io.InputStreamThe input stream for the entry.
newEntrySettingsCabEntrySettingsCompression 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:

ParameterTypeDescription
namejava.lang.StringThe name of the entry.
pathjava.lang.StringThe 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:

ParameterTypeDescription
namejava.lang.StringThe name of the entry.
pathjava.lang.StringThe fully qualified name of the new file, or the relative file name to be compressed.
newEntrySettingsCabEntrySettingsCompression 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:

ParameterTypeDescription
namejava.lang.StringThe name of the entry.
streamProviderjava.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:

ParameterTypeDescription
namejava.lang.StringThe name of the entry.
streamProviderjava.util.function.Supplier<java.io.InputStream>The method providing input stream for the entry.
newEntrySettingsCabEntrySettingsCompression 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:

ParameterTypeDescription
destinationDirectoryjava.lang.Stringthe 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:

ParameterTypeDescription
outputStreamjava.io.OutputStreamDestination 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:

ParameterTypeDescription
outputStreamjava.io.OutputStreamDestination stream.
saveOptionsCabSaveOptionsOptions 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:

ParameterTypeDescription
destinationFileNamejava.lang.StringThe 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:

ParameterTypeDescription
destinationFileNamejava.lang.StringThe path of the archive to be created. If the specified file name points to an existing file, it will be overwritten.
saveOptionsCabSaveOptionsOptions 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. |