CAB composition
Overview
CAB is a native Microsoft Windows archive format often used in installers and software distribution packages. Aspose.ZIP lets you compose CAB archives with the CabArchive class and add entries from file paths, streams, file metadata, or deferred stream providers.
CAB composition in Aspose.ZIP uses CabEntrySettings with either CabMsZipCompressionSettings or CabStoreCompressionSettings. If you do not specify settings, MSZip compression is used by default.
Compose CAB archive
The following sample creates a CAB archive with two files using the default compression settings.
1 using (CabArchive archive = new CabArchive())
2 {
3 archive.CreateEntry("alice29.txt", "alice29.txt");
4 archive.CreateEntry("asyoulik.txt", "asyoulik.txt");
5 archive.Save("result.cab");
6 }Store entries without compression
If you need to keep CAB blocks uncompressed, pass CabStoreCompressionSettings in CabEntrySettings.
1 using (FileStream source = File.OpenRead("alice29.txt"))
2 {
3 CabEntrySettings settings = new CabEntrySettings(new CabStoreCompressionSettings());
4
5 using (CabArchive archive = new CabArchive())
6 {
7 archive.CreateEntry("alice29.txt", source, settings);
8 archive.Save("stored.cab");
9 }
10 }Compose CAB archive from directory
Aspose.ZIP can recursively add all files from a directory with the CreateEntries method. Set includeRootDirectory to false if you want only relative paths inside the archive.
1 using (CabArchive archive = new CabArchive())
2 {
3 archive.CreateEntries(@"C:\Data\Reports", includeRootDirectory: false);
4 archive.Save("reports.cab");
5 }Compose CAB archive with deferred sources
The overload accepting a stream provider is useful when a source stream should be opened only at save time. CabSaveOptions lets you close such streams immediately after the corresponding entry has been compressed.
1 using (CabArchive archive = new CabArchive())
2 {
3 archive.CreateEntry("alice29.txt", () => File.OpenRead("alice29.txt"));
4 archive.CreateEntry("asyoulik.txt", () => File.OpenRead("asyoulik.txt"));
5 archive.Save("lazy.cab", new CabSaveOptions() { CloseEntrySource = true });
6 }