libarchive2
Safe Rust bindings for libarchive v3.8.1, providing cross-platform archive reading and writing capabilities.
Features
- Memory Safe: Comprehensive lifetime tracking prevents use-after-free bugs
- Type Safe: Idiomatic Rust API with strong typing and error handling
- Zero Cost Abstractions: RAII resource management with no runtime overhead
- Full Feature Support: All libarchive v3.8.1 capabilities exposed safely
- Multiple Formats: TAR, ZIP, 7z, AR, CPIO, ISO9660, XAR, MTREE, WARC, and more
- Multiple Compressions: gzip, bzip2, xz, zstd, lz4, compress, and more
- Cross-Platform: macOS, Windows, Linux, iOS, and Android
- Encryption Support: Read and write password-protected archives
- Streaming I/O: Custom callback support for network streams and custom sources
- ACL & Extended Attributes: Full support for advanced file metadata
Why libarchive2?
This crate provides a production-ready, memory-safe Rust interface to libarchive with:
- ✅ Compile-time safety checks preventing common FFI errors
- ✅ Comprehensive lifetime management eliminating use-after-free bugs
- ✅ Proper error propagation with idiomatic
Resulttypes - ✅ Thread-safe design (
Sendbut notSync- matching libarchive semantics) - ✅ Builder pattern for ergonomic API construction
- ✅ Extensive documentation with safety guarantees explained
- ✅ Zero warnings from clippy and rustc
Architecture
This crate consists of two layers:
- libarchive2-sys: Low-level FFI bindings generated with bindgen
- libarchive2: High-level safe Rust API with lifetime tracking and error handling
Platform Support
All platforms have been tested with cross-compilation from macOS (ARM64). The build system automatically configures appropriate toolchains and library dependencies for each target platform.
macOS (x86_64, aarch64)
Prerequisites:
Build:
Linux (x86_64, aarch64)
Native Build Prerequisites (Debian/Ubuntu):
Native Build Prerequisites (Fedora/RHEL):
Native Build:
Cross-Compilation from macOS:
# Install Linux cross-compiler toolchain
# Add target
# Build
Windows (x86_64)
Native Build Prerequisites:
- Visual Studio 2019 or later (with C++ tools) OR MinGW-w64
- CMake 3.15 or later
- vcpkg (recommended for dependencies):
vcpkg install zlib bzip2 liblzma zstd lz4
Native Build (MSVC):
cargo build --target x86_64-pc-windows-msvc
Native Build (MinGW):
Cross-Compilation from macOS/Linux:
# Install MinGW toolchain
# or
# Add target
# Build
iOS (aarch64)
Prerequisites:
- Xcode with iOS SDK
- Compression libraries (can be built from source or via CocoaPods)
Build:
Note: You may need to adjust library search paths in your project configuration.
Android (aarch64, armv7, x86_64, i686)
Prerequisites:
- Android NDK r21 or later
- Set
ANDROID_NDK_HOMEenvironment variable
Build:
# Set NDK path
# Build for various Android targets
Features:
- All compression formats enabled (zlib, bzip2, xz/lzma, zstd, lz4)
- ACL (Access Control Lists) support enabled
- XATTR (Extended Attributes) support enabled
Platform Support Matrix
| Platform | Architectures | Status | Notes |
|---|---|---|---|
| macOS | x86_64, ARM64 (M1/M2) | ✅ Fully Supported | All features enabled |
| Windows (GNU) | x86_64 | ✅ Supported | Cross-compilation tested from macOS |
| Windows (MSVC) | x86_64 | ⚠️ Untested | Should work but not tested |
| Linux | x86_64 | ✅ Supported | Cross-compilation tested from macOS |
| iOS | ARM64, x86_64 (simulator) | ✅ Supported | All features enabled |
| Android | ARM64, ARMv7, x86_64, x86 | ✅ Supported | All features enabled |
| WebAssembly | wasm32 | ❌ Not Supported | libarchive requires POSIX types unavailable in WASM |
Why WASM is Not Supported
libarchive v3.8.1 is not compatible with WebAssembly because:
- The library requires POSIX types (
pid_t,uid_t,gid_t,mode_t) that don't exist in WASM - Depends on system calls and OS-level file system operations not available in WebAssembly
- CMake configuration fails when trying to detect these platform-specific types
If WASM support is critical for your use case, consider using pure-Rust archive libraries like tar or zip crates instead.
Quick Start
Add this to your Cargo.toml:
[]
= "0.1"
Usage Examples
Reading an Archive
use ;
Creating an Archive
use ;
Reading from Memory (Lifetime-Safe)
use ReadArchive;
Writing to Memory (Compile-Time Safety)
use ;
Reading Encrypted Archives
use ;
Writing Encrypted Archives
use ;
Custom Callbacks for Streaming
use ;
use Read;
Pattern Matching and Filtering
use ;
Reading Directly from Disk
use ;
Examples
See the examples/ directory for comprehensive usage examples:
| Example | Description |
|---|---|
version_info.rs |
Display libarchive version information |
create_archive.rs |
Create a tar.gz archive with files and directories |
read_archive.rs |
Read and display archive contents |
read_encrypted_archive.rs |
Read encrypted/password-protected archives |
write_encrypted.rs |
Create encrypted ZIP archives |
callback_write.rs |
Use custom callbacks for streaming |
filter_archive.rs |
Pattern-based filtering of archive entries |
read_disk_example.rs |
Read files directly from filesystem |
write_disk_example.rs |
Extract archives to disk |
extract_archive.rs |
Full-featured extraction with options |
Run examples with:
Supported Formats
Archive Formats (Read/Write)
| Format | Read | Write | Notes |
|---|---|---|---|
| TAR (POSIX) | ✅ | ✅ | Modern TAR format |
| TAR (GNU) | ✅ | ✅ | GNU extensions |
| TAR (USTAR) | ✅ | ✅ | POSIX.1-1988 |
| ZIP | ✅ | ✅ | With encryption support |
| 7-Zip | ✅ | ✅ | With encryption support |
| AR | ✅ | ✅ | Unix archive format |
| CPIO | ✅ | ✅ | Traditional Unix format |
| ISO 9660 | ✅ | ✅ | CD-ROM filesystem |
| XAR | ✅ | ✅ | Extensible archive format |
| MTREE | ✅ | ✅ | BSD manifest format |
| Shar | ✅ | ✅ | Shell archive |
| WARC | ✅ | ✅ | Web ARChive |
| RAR | ✅ | ❌ | Read-only |
| RAR5 | ✅ | ❌ | Read-only |
| LHA | ✅ | ❌ | Read-only |
| CAB | ✅ | ❌ | Read-only |
Compression Formats
| Compression | Read | Write | Notes |
|---|---|---|---|
| None | ✅ | ✅ | Uncompressed |
| Gzip | ✅ | ✅ | Most common |
| Bzip2 | ✅ | ✅ | Better compression |
| XZ/LZMA | ✅ | ✅ | Best compression |
| Zstd | ✅ | ✅ | Fast modern compression |
| LZ4 | ✅ | ✅ | Extremely fast |
| Compress | ✅ | ✅ | LZW compression |
| UUEncode | ✅ | ✅ | Legacy encoding |
| LZIP | ✅ | ❌ | LZMA-based |
| LRZIP | ✅ | ✅ | Long-range compression |
| LZOP | ✅ | ✅ | LZO-based |
| GRZIP | ✅ | ✅ | Grid-friendly |
Safety Guarantees
This crate provides strong memory safety guarantees:
- No use-after-free: Lifetimes prevent dangling pointers at compile time
- No data races:
Sendbut notSyncenforces single-threaded access - No null pointer dereferences: All null checks before FFI calls
- No buffer overflows: All buffer operations bounds-checked
- Proper error handling: All FFI errors converted to Rust
Resulttypes - Resource cleanup: RAII ensures archives are always properly closed
See COMPREHENSIVE_CODE_REVIEW.md for detailed safety analysis.
Performance
Zero-cost abstractions mean this crate has no runtime overhead compared to using libarchive directly from C:
- No heap allocations beyond what libarchive requires
- Inline function calls eliminate FFI overhead where possible
- Direct memory access with no intermediate copies
- Efficient buffer management with pre-allocated buffers
License
This project follows the same license as libarchive itself (BSD 2-Clause). See the libarchive submodule for license details.
Contributing
Contributions are welcome! Please ensure that:
- ✅ Code compiles without warnings:
cargo check,cargo clippy - ✅ Code follows Rust 2024 edition standards
- ✅ All existing examples build:
cargo build --examples - ✅ New features include documentation with examples
- ✅ Safety invariants are documented for unsafe code
Building from Source
# Clone with submodules
# Build
# Run all examples
# Check for issues
# View documentation
Troubleshooting
macOS: Library Not Found
If you get linker errors on macOS, ensure libraries are installed via Homebrew and try:
Windows: CMake Not Found
Install CMake from https://cmake.org/download/ and add it to your PATH.
Linux: Missing Development Packages
Ensure all development packages are installed. The exact package names vary by distribution.
Android: NDK Not Found
Ensure the ANDROID_NDK_HOME environment variable is set:
# or
# Example on macOS
Android: Library Linking Errors
All compression libraries (zlib, bzip2, xz/lzma, zstd, lz4) are enabled and will be linked from the Android NDK. If you encounter linking errors, ensure your NDK version is r21 or later.
Cross-Compilation: Sysroot Not Found
For cross-compilation (Windows, Linux), ensure the appropriate toolchain is installed via Homebrew:
# For Windows cross-compilation
# For Linux cross-compilation