6 releases (breaking)

0.5.0 Nov 24, 2023
0.4.1 Jun 20, 2022
0.4.0 May 4, 2022
0.3.0 Sep 13, 2021
0.1.0 Feb 20, 2018

#96 in Compression

Download history 4323/week @ 2023-08-13 4707/week @ 2023-08-20 5098/week @ 2023-08-27 4760/week @ 2023-09-03 5175/week @ 2023-09-10 4879/week @ 2023-09-17 5538/week @ 2023-09-24 6722/week @ 2023-10-01 6047/week @ 2023-10-08 6381/week @ 2023-10-15 5615/week @ 2023-10-22 5341/week @ 2023-10-29 5262/week @ 2023-11-05 6029/week @ 2023-11-12 5085/week @ 2023-11-19 5608/week @ 2023-11-26

22,389 downloads per month
Used in 27 crates (9 directly)

MIT license



Build Status Crates.io Documentation

A pure Rust library for reading/writing Windows cabinet (CAB) files.

Documentation: https://docs.rs/cab


rust-cab is made available under the MIT License.


A library for reading/writing Windows cabinet (CAB) files.


CAB is an archive file format used by Windows. A cabinet file can contain multiple compressed files, which are divided into "folders" (no relation to filesystem folders/directories); files in the same folder are compressed together, and each folder in the cabinet can potentially use a different compression scheme. The CAB file format supports multiple different compression schemes; this library can recognize all of them when reading metadata for an existing cabinet file, but currently only supports encoding/decoding some of them, as shown:

Compression Supported
Uncompressed Yes
MSZIP (Deflate) Yes
Quantum No
LZX Yes (decode only)

Example usage

Use the Cabinet type to read an existing cabinet file:

use cab;
use std::fs;
use std::io;

let cab_file = fs::File::open("path/to/cabinet.cab").unwrap();
let mut cabinet = cab::Cabinet::new(cab_file).unwrap();
// List all files in the cabinet, with file sizes and compression types:
for folder in cabinet.folder_entries() {
    for file in folder.file_entries() {
        println!("File {} ({} B) is compressed with {:?}",
// Decompress a particular file in the cabinet and save it to disk:
let mut reader = cabinet.read_file("images/example.png").unwrap();
let mut writer = fs::File::create("out/example.png").unwrap();
io::copy(&mut reader, &mut writer).unwrap();

Creating a new cabinet file is a little more involved. Because of how the cabinet file is structured on disk, the library has to know the names of all the files that will be in the cabinet up front, before it can start writing anything to disk. However, we don't want to have to hold all the file contents in memory at once. Therefore, cabinet creation happens in two steps: first, create a CabinetBuilder and specify all filenames and other metadata, and then second, stream each file's data into a CabinetWriter, one at a time:

use cab;
use std::fs;
use std::io;

let mut cab_builder = cab::CabinetBuilder::new();
// Add a single file in its own folder:
// Add several more files, compressed together in a second folder:
    let folder = cab_builder.add_folder(cab::CompressionType::MsZip);
    // We can also specify metadata on individual files:
        let file = folder.add_file("documents/hidden.txt");
// Now, we'll actually construct the cabinet file on disk:
let cab_file = fs::File::create("path/to/cabinet.cab").unwrap();
let mut cab_writer = cab_builder.build(cab_file).unwrap();
while let Some(mut writer) = cab_writer.next_file().unwrap() {
    let mut reader = fs::File::open(writer.file_name()).unwrap();
    io::copy(&mut reader, &mut writer).unwrap();
// Print the file size of the cabinet file we just created:
let mut cab_file = cab_writer.finish().unwrap();
println!("Cabinet size: {} B", cab_file.metadata().unwrap().len());


~24K SLoC