Class CabArchive
Namespace: Aspose.Zip.Cab
Assembly: Aspose.Zip.dll (25.12.0)
This class represents a CAB archive file.
public class CabArchive : IArchive, IDisposableInheritance
Implements
Inherited Members
object.GetType() , object.MemberwiseClone() , object.ToString() , object.Equals(object?) , object.Equals(object?, object?) , object.ReferenceEquals(object?, object?) , object.GetHashCode()
Constructors
CabArchive(CabEntrySettings)
Initializes a new instance of the Aspose.Zip.Cab.CabArchive class prepared for compressing.
public CabArchive(CabEntrySettings settings = null)Parameters
settings CabEntrySettings
Examples
The following example shows how to compress a file.
using (var archive = new CabArchive())
{
archive.CreateEntry("first.bin", "data.bin");
archive.Save("archive.cab");
}Compress a file using specific compression settings.
using (var archive = new CabArchive())
{
var settings = new CabEntrySettings(new CabStoreCompressionSettings());
archive.CreateEntry("entry.bin", "data.bin", settings);
archive.Save("archive.cab");
}CabArchive(Stream, CabLoadOptions)
Initializes a new instance of the Aspose.Zip.Cab.CabArchive class and composes an entry list can be extracted from the archive.
public CabArchive(Stream sourceStream, CabLoadOptions loadOptions = null)Parameters
sourceStream Stream
The source of the archive. It must be seekable.
loadOptions CabLoadOptions
Options to load existing archive with.
Examples
The following example shows how to extract all the entries to a directory.
using (var archive = new CabArchive(File.OpenRead("archive.cab")))
{
archive.ExtractToDirectory("C:\\extracted");
}Remarks
This constructor does not unpack any entry. See Aspose.Zip.Cab.CabEntry.Open method for unpacking.
Exceptions
sourceStream is null.
sourceStream is not seekable.
sourceStream is not valid CAB archive.
The stream is too short.
CabArchive(string, CabLoadOptions)
Initializes a new instance of the Aspose.Zip.Cab.CabArchive class and composes an entry list can be extracted from the archive.
public CabArchive(string path, CabLoadOptions loadOptions = null)Parameters
path string
The path to the archive file.
loadOptions CabLoadOptions
Options to load existing archive with.
Examples
The following example shows how to extract all the entries to a directory.
using (var archive = new CabArchive("archive.cab")) hj
{
archive.ExtractToDirectory("C:\\extracted");
}Remarks
This constructor does not unpack any entry. See Aspose.Zip.Cab.CabEntry.Open method for unpacking.
Exceptions
path is null.
The caller does not have the required permission to access.
The path is empty, contains only white spaces, or contains invalid characters.
Access to file path is denied.
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.
File at path contains a colon (:) in the middle of the string.
Archive has been disposed and cannot be used.
The file is not found.
The specified path is invalid, such as being on an unmapped drive.
The file is already open.
The file is too short.
Properties
Entries
Gets entries of Aspose.Zip.Cab.CabEntry type constituting the archive.
public ReadOnlyCollection<CabEntry> Entries { get; }Property Value
ReadOnlyCollection < CabEntry >
Exceptions
Archive has been disposed and cannot be used.
Methods
CreateEntries(DirectoryInfo, bool)
Adds to the archive all files, recursively, from the specified directory.
public CabArchive CreateEntries(DirectoryInfo directory, bool includeRootDirectory = true)Parameters
directory DirectoryInfo
Directory to compress.
includeRootDirectory bool
Indicates whether to include the root directory name in entry paths.
Returns
The current Aspose.Zip.Cab.CabArchive instance.
Examples
using (var archive = new CabArchive())
{
var directory = new DirectoryInfo("logs");
archive.CreateEntries(directory);
archive.Save("logs.cab");
}Exceptions
directory is null.
Archive has been disposed and cannot be used.
directory cannot be found.
The caller does not have the required permission to access directory or its content.
Access to directory or one of its files is denied.
An I/O error occurs while accessing directory.
A generated entry path exceeds the system-defined maximum length.
The archive is prepared for extraction and cannot add entries.
CreateEntries(string, bool)
Adds to the archive all files recursively from the specified directory path.
public CabArchive CreateEntries(string sourceDirectory, bool includeRootDirectory = true)Parameters
sourceDirectory string
Directory path to compress.
includeRootDirectory bool
Indicates whether to include the root directory name in entry paths.
Returns
The current Aspose.Zip.Cab.CabArchive instance.
Examples
using (var archive = new CabArchive(new CabEntrySettings(new CabStoreCompressionSettings())))
{
archive.CreateEntries("data", includeRootDirectory: false);
archive.Save("stored_data.cab");
}Exceptions
Archive has been disposed and cannot be used.
sourceDirectory is null.
sourceDirectory cannot be found.
The caller does not have the required permission to access sourceDirectory.
Access to sourceDirectory is denied.
The specified sourceDirectory exceeds the system-defined maximum length.
sourceDirectory is empty, contains only white spaces, or contains invalid characters.
An I/O error occurs while accessing sourceDirectory.
The archive is prepared for extraction and cannot add entries.
CreateEntry(string, string, CabEntrySettings)
Create a single entry within the archive.
public CabEntry CreateEntry(string name, string path, CabEntrySettings newEntrySettings = null)Parameters
name string
The name of the entry.
path 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 Aspose.Zip.Cab.CabEntry item.
Returns
Cab entry instance.
Examples
using (var archive = new CabArchive())
{
archive.CreateEntry("entry.bin", "data.bin");
archive.Save("archive.cab");
}Remarks
The entry name is solely set within name parameter. The file name provided in path parameter does not affect the entry name.
Exceptions
path is null.
The caller does not have the required permission to access.
The path is empty, contains only white spaces, or contains invalid characters.
Access to file path is denied.
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.
File at path contains a colon (:) in the middle of the string.
Archive has been disposed and cannot be used.
The archive is prepared for extraction and cannot add entries.
CreateEntry(string, Stream, CabEntrySettings)
Create a single entry within the archive.
public CabEntry CreateEntry(string name, Stream source, CabEntrySettings newEntrySettings = null)Parameters
name string
The name of the entry.
source Stream
The input stream for the entry.
newEntrySettings CabEntrySettings
Compression and encryption settings used for added Aspose.Zip.Cab.CabEntry item.
Returns
Cab entry instance.
Examples
using (var archive = new CabArchive())
{
using (var dataStream = new MemoryStream(File.ReadAllBytes("data.bin")))
{
archive.CreateEntry("stream-entry.bin", dataStream);
archive.Save("archive.cab");
}
}using (var archive = new CabArchive())
{
var settings = new CabEntrySettings(new CabStoreCompressionSettings());
archive.CreateEntry("stream-entry.bin", dataStream, settings);
archive.Save("archive.cab");
}Exceptions
Archive has been disposed and cannot be used.
The archive is prepared for extraction and cannot add entries.
CreateEntry(string, FileInfo, CabEntrySettings)
Create a single entry within the archive.
public CabEntry CreateEntry(string name, FileInfo fileInfo, CabEntrySettings newEntrySettings = null)Parameters
name string
The name of the entry.
fileInfo FileInfo
The metadata of file to be compressed.
newEntrySettings CabEntrySettings
Compression and encryption settings used for added Aspose.Zip.Cab.CabEntry item.
Returns
Cab entry instance.
Examples
using (var archive = new CabArchive(new CabEntrySettings(new CabMsZipCompressionSettings())))
{
var sourceFile = new FileInfo("logs\\log.txt");
archive.CreateEntry("log.txt", sourceFile);
archive.Save("archive.cab");
}Remarks
The entry name is solely set within name parameter. The file name provided in fileInfo parameter does not affect the entry name.
Exceptions
fileInfo is read-only or is a directory.
The specified path is invalid, such as being on an unmapped drive.
The file is already open.
fileInfo represents a file that cannot be found.
The caller does not have the required permission to access fileInfo.
Archive has been disposed and cannot be used.
The archive is prepared for extraction and cannot add entries.
Dispose(bool)
Performs application-defined tasks associated with freeing, releasing, or resetting unmanaged resources.
protected virtual void Dispose(bool disposing)Parameters
disposing bool
Whether managed resources should be disposed.
Dispose()
Performs application-defined tasks associated with freeing, releasing, or resetting unmanaged resources.
public void Dispose()ExtractToDirectory(string)
Extracts all the files in the archive to the directory provided.
public void ExtractToDirectory(string destinationDirectory)Parameters
destinationDirectory string
The path to the directory to place the extracted files in.
Examples
using (var archive = new CabArchive("archive.cab"))
{
archive.ExtractToDirectory("C:\\extracted");
}Remarks
If the directory does not exist, it will be created.
Exceptions
path is null
The specified path, file name, or both exceed the system-defined maximum length.
The caller does not have the required permission to access the existing directory.
If the directory does not exist, a path contains a colon character (:) that is not part of a drive label (“C:").
path is a zero-length string, contains only white space, or contains one or more invalid characters. You can query for invalid characters by using the System.IO.Path.GetInvalidPathChars method. -or- path is prefixed with, or contains, only a colon character (:).
The directory specified by path is a file. -or- The network name is not known.
The archive is corrupted.
Archive has been disposed and cannot be used.
The archive is prepared for composition and cannot be extracted.
In .NET Framework 4.0 and above: Thrown when the extraction is canceled via the provided cancellation token.
Save(Stream, CabSaveOptions)
Saves archive to the stream provided.
public void Save(Stream outputStream, CabSaveOptions saveOptions = null)Parameters
outputStream Stream
Destination stream.
saveOptions CabSaveOptions
Options for archive saving.
Examples
using (FileStream cabFile = File.Open("archive.cab", FileMode.Create))
{
using (var archive = new CabArchive())
{
archive.CreateEntry("entry.bin", "data.bin");
archive.Save(cabFile);
}
}Remarks
outputStream must be writable.
Exceptions
outputStream is not writable.
The archive is disposed.
The archive is prepared for extraction and cannot be saved.
Save(string, CabSaveOptions)
Saves archive to the destination file provided.
public void Save(string destinationFileName, CabSaveOptions saveOptions = null)Parameters
destinationFileName 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.
Examples
using (var archive = new Archive())
{
archive.CreateEntry("entry.bin", "data.bin");
archive.Save("archive.zip", new ArchiveSaveOptions() { Encoding = Encoding.ASCII });
}Remarks
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.
Exceptions
destinationFileName is null.
The caller does not have the required permission to access.
The destinationFileName is empty, contains only white spaces, or contains invalid characters.
Access to file destinationFileName is denied.
The specified destinationFileName, 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.
File at destinationFileName contains a colon (:) in the middle of the string.
The file is not found.
The archive is opened for extraction.
The specified path is invalid, such as being on an unmapped drive.
The file is already open.
Archive has been disposed and cannot be used.