Class CabArchive

Class CabArchive

Namespace: Aspose.Zip.Cab
Assembly: Aspose.Zip.dll (25.12.0)

This class represents a CAB archive file.

public class CabArchive : IArchive, IDisposable

Inheritance

object CabArchive

Implements

IArchive , IDisposable

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

ArgumentNullException

sourceStream is null.

ArgumentException

sourceStream is not seekable.

InvalidDataException

sourceStream is not valid CAB archive.

EndOfStreamException

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

ArgumentNullException

path is null.

SecurityException

The caller does not have the required permission to access.

ArgumentException

The path is empty, contains only white spaces, or contains invalid characters.

UnauthorizedAccessException

Access to file path is denied.

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.

NotSupportedException

File at path contains a colon (:) in the middle of the string.

ObjectDisposedException

Archive has been disposed and cannot be used.

FileNotFoundException

The file is not found.

DirectoryNotFoundException

The specified path is invalid, such as being on an unmapped drive.

IOException

The file is already open.

EndOfStreamException

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

ObjectDisposedException

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

CabArchive

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

ArgumentNullException

directory is null.

ObjectDisposedException

Archive has been disposed and cannot be used.

DirectoryNotFoundException

directory cannot be found.

SecurityException

The caller does not have the required permission to access directory or its content.

UnauthorizedAccessException

Access to directory or one of its files is denied.

IOException

An I/O error occurs while accessing directory.

PathTooLongException

A generated entry path exceeds the system-defined maximum length.

InvalidOperationException

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

CabArchive

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

ObjectDisposedException

Archive has been disposed and cannot be used.

ArgumentNullException

sourceDirectory is null.

DirectoryNotFoundException

sourceDirectory cannot be found.

SecurityException

The caller does not have the required permission to access sourceDirectory.

UnauthorizedAccessException

Access to sourceDirectory is denied.

PathTooLongException

The specified sourceDirectory exceeds the system-defined maximum length.

ArgumentException

sourceDirectory is empty, contains only white spaces, or contains invalid characters.

IOException

An I/O error occurs while accessing sourceDirectory.

InvalidOperationException

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

CabEntry

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

ArgumentNullException

path is null.

SecurityException

The caller does not have the required permission to access.

ArgumentException

The path is empty, contains only white spaces, or contains invalid characters.

UnauthorizedAccessException

Access to file path is denied.

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.

NotSupportedException

File at path contains a colon (:) in the middle of the string.

ObjectDisposedException

Archive has been disposed and cannot be used.

InvalidOperationException

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

CabEntry

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

ObjectDisposedException

Archive has been disposed and cannot be used.

InvalidOperationException

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

CabEntry

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

UnauthorizedAccessException

fileInfo is read-only or is a directory.

DirectoryNotFoundException

The specified path is invalid, such as being on an unmapped drive.

IOException

The file is already open.

FileNotFoundException

fileInfo represents a file that cannot be found.

SecurityException

The caller does not have the required permission to access fileInfo.

ObjectDisposedException

Archive has been disposed and cannot be used.

InvalidOperationException

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

ArgumentNullException

path is null

PathTooLongException

The specified path, file name, or both exceed the system-defined maximum length.

SecurityException

The caller does not have the required permission to access the existing directory.

NotSupportedException

If the directory does not exist, a path contains a colon character (:) that is not part of a drive label (“C:").

ArgumentException

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 (:).

IOException

The directory specified by path is a file. -or- The network name is not known.

InvalidDataException

The archive is corrupted.

ObjectDisposedException

Archive has been disposed and cannot be used.

InvalidOperationException

The archive is prepared for composition and cannot be extracted.

OperationCanceledException

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

ArgumentException

outputStream is not writable.

ObjectDisposedException

The archive is disposed.

InvalidOperationException

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

ArgumentNullException

destinationFileName is null.

SecurityException

The caller does not have the required permission to access.

ArgumentException

The destinationFileName is empty, contains only white spaces, or contains invalid characters.

UnauthorizedAccessException

Access to file destinationFileName is denied.

PathTooLongException

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.

NotSupportedException

File at destinationFileName contains a colon (:) in the middle of the string.

FileNotFoundException

The file is not found.

InvalidOperationException

The archive is opened for extraction.

DirectoryNotFoundException

The specified path is invalid, such as being on an unmapped drive.

IOException

The file is already open.

ObjectDisposedException

Archive has been disposed and cannot be used.

 English