2 releases
Uses new Rust 2024
| 0.8.16 | Jan 25, 2026 |
|---|---|
| 0.8.14 | Jan 25, 2026 |
#384 in Compression
130KB
3K
SLoC
API of totebag crate
🗣️ Overview
This is the README for the totebag crate, which provides the API of the totebag tool for extracting/archiving files and directories in multiple formats.
The totebag crate provides a unified API for handling various archive formats, making it easy for developers to integrate archiving and extraction functionality into their Rust applications.
It abstracts the differences between various archive formats, providing a consistent interface for working with archives.
Supported archive formats
- Ar
- Cab
- Cpio
- Tar
- Tar+Gzip
- Tar+Bzip2
- Tar+Xz
- Tar+Zstd
- Zip
- 7z
- Lha, Lzh (extraction only)
- Rar (extraction only)
🚶 How to use
💚 Archiving files and directories
use std::path::PathBuf;
let config = totebag::ArchiveConfig::builder()
.dest("results/test.zip") // destination file.
.rebase_dir(PathBuf::from("new")) // rebased directory in the archive file.
.overwrite(true) // set overwrite flag of the destination file.
.build();
let targets: Vec<PathBuf> = vec!["src", "Cargo.toml"].iter() // files to be archived.
.map(|s| PathBuf::from(s)).collect::<Vec<_>>();
match totebag::archive(&targets, &config) {
Ok(_) => println!("archiving is done"),
Err(e) => eprintln!("error: {:?}", e),
}
💛 Extracting the archive file
use std::path::PathBuf;
let config = totebag::ExtractConfig::builder()
.dest("results") // set the destination directory.
.build();
match totebag::extract("extracting_archive_file.zip", &config) {
Ok(r) => println!("{:?}", r),
Err(e) => println!("error: {:?}", e),
}
Compression level
| Level | |
|---|---|
| Ar | N/A |
| Cab | 0: None, otherwise: MsZIP; see CompressionType. |
| Cpio | 0-3: Odc, 4-6: Newc, 7: Crc, 8: Bin(LittleEndian), 9: Bin(BigEndian); see kpea::Format. |
| Gzip | See Compression. |
| Bzip2 | See Compression. |
| Xz | See XzEncoder. |
| Zstd | Map 1-22 to 0-9 See Encoder. |
| Zip | 0: No compression, 1-3: Deflate (10, 24, 264), 4-6: Bzip2 (1, 6, 9), 7-9: Zstd (-7, 3, 22); see FileOptions. |
| 7z | 0-4: LZMA, 5-9: LZMA64 (SevenZMethod) |
💙 List entries in an archive file
The list function returns a string-formatted list of entries in the archive file.
use std::path::PathBuf;
let file = PathBuf::from("listing_archive_file.zip");
let config = totebag::ListConfig::new(totebag::OutputFormat::Default);
match totebag::list(file, &config) {
Ok(formatted_list) => println!("{:?}", formatted_list),
Err(e) => println!("error: {:?}", e),
}
The entries function returns a vector of Entry objects that are reflected in the archive file.
use std::path::PathBuf;
let file = PathBuf::from("listing_archive_file.zip");
match totebag::entries(file) {
Ok(entries) => println!("{:?}", entries),
Err(e) => println!("error: {:?}", e),
}
Dependencies
~18–34MB
~510K SLoC