### Implement File Compression and Decompression Source: https://context7.com/bozaro/lz4-rs/llms.txt A complete example showing how to process files using the streaming API. It includes logic to detect file extensions and perform either compression or decompression accordingly. ```rust use std::env; use std::fs::File; use std::io::{self, Result}; use std::path::{Path, PathBuf}; use lz4::{Decoder, EncoderBuilder}; fn main() { println!("LZ4 version: {}", lz4::version()); for path in env::args().skip(1).map(PathBuf::from) { if let Some("lz4") = path.extension().and_then(|e| e.to_str()) { decompress(&path, &path.with_extension("")).unwrap(); } else { compress(&path, &path.with_extension("lz4")).unwrap(); } } } fn compress(source: &Path, destination: &Path) -> Result<()> { let mut input_file = File::open(source)?; let output_file = File::create(destination)?; let mut encoder = EncoderBuilder::new().level(4).build(output_file)?; io::copy(&mut input_file, &mut encoder)?; let (_output, result) = encoder.finish(); result } fn decompress(source: &Path, destination: &Path) -> Result<()> { let input_file = File::open(source)?; let mut decoder = Decoder::new(input_file)?; let mut output_file = File::create(destination)?; io::copy(&mut decoder, &mut output_file)?; Ok(()) } ``` -------------------------------- ### Block Mode Compression Examples in Rust Source: https://context7.com/bozaro/lz4-rs/llms.txt Shows various ways to perform one-shot compression using the `lz4::block::compress` function. It demonstrates using different compression modes (default, FAST, HIGHCOMPRESSION) and controlling the size prefix. Dependencies include `lz4::block::{compress, CompressionMode}`. ```rust use lz4::block::{compress, CompressionMode}; fn block_compress_examples() -> std::io::Result<()> { let data = vec![0u8; 1024]; // Default compression with size prefix (enables auto-detect on decompress) let compressed_with_prefix = compress(&data, None, true)?; // Default compression without size prefix (requires known size on decompress) let compressed_no_prefix = compress(&data, None, false)?; // Fast compression with acceleration parameter (1-100, higher = faster but less compression) let fast_compressed = compress(&data, Some(CompressionMode::FAST(1)), true)?; // High compression mode with level (1-12, higher = better compression) let high_compressed = compress(&data, Some(CompressionMode::HIGHCOMPRESSION(9)), true)?; println!("Original: {} bytes", data.len()); println!("Default compressed: {} bytes", compressed_with_prefix.len()); println!("Fast compressed: {} bytes", fast_compressed.len()); println!("High compressed: {} bytes", high_compressed.len()); Ok(()) } ``` -------------------------------- ### Rust LZ4 Compression and Decompression Example Source: https://github.com/bozaro/lz4-rs/blob/master/README.md This Rust code snippet demonstrates how to use the lz4-rs library to compress and decompress files. It includes functions for both compression and decompression, handling file I/O and utilizing the EncoderBuilder and Decoder from the lz4 crate. The main function iterates through command-line arguments to process files, compressing those without a .lz4 extension and decompressing those that have it. ```Rust extern crate lz4; use std::env; use std::fs::File; use std::io::{self, Result}; use std::path::{Path, PathBuf}; use lz4::{Decoder, EncoderBuilder}; fn main() { println!("LZ4 version: {}", lz4::version()); for path in env::args().skip(1).map(PathBuf::from) { if let Some("lz4") = path.extension().and_then(|e| e.to_str()) { decompress(&path, &path.with_extension("")).unwrap(); } else { compress(&path, &path.with_extension("lz4")).unwrap(); } } } fn compress(source: &Path, destination: &Path) -> Result<()> { println!("Compressing: {} -> {}", source.display(), destination.display()); let mut input_file = File::open(source)?; let output_file = File::create(destination)?; let mut encoder = EncoderBuilder::new() .level(4) .build(output_file)?; io::copy(&mut input_file, &mut encoder)?; let (_output, result) = encoder.finish(); result } fn decompress(source: &Path, destination: &Path) -> Result<()> { println!("Decompressing: {} -> {}", source.display(), destination.display()); let input_file = File::open(source)?; let mut decoder = Decoder::new(input_file)?; let mut output_file = File::create(destination)?; io::copy(&mut decoder, &mut output_file)?; Ok(()) } ``` -------------------------------- ### Block Mode Compression Source: https://context7.com/bozaro/lz4-rs/llms.txt Provides examples for one-shot LZ4 block compression with and without size prefix, and different compression modes. ```APIDOC ## Block Mode Compression ### Description This section details the block mode API for LZ4 compression. It offers functions for one-shot compression of data blocks, with options to include or omit a size prefix for automatic decompression size detection. Various compression modes like FAST and HIGHCOMPRESSION are also demonstrated. ### Method N/A (Rust library usage) ### Endpoint N/A (Rust library usage) ### Parameters N/A (Rust library usage) ### Request Example ```rust use lz4::block::{compress, CompressionMode}; fn block_compress_examples() -> std::io::Result<()> { let data = vec![0u8; 1024]; // Default compression with size prefix (enables auto-detect on decompress) let compressed_with_prefix = compress(&data, None, true)?; // Default compression without size prefix (requires known size on decompress) let compressed_no_prefix = compress(&data, None, false)?; // Fast compression with acceleration parameter (1-100, higher = faster but less compression) let fast_compressed = compress(&data, Some(CompressionMode::FAST(1)), true)?; // High compression mode with level (1-12, higher = better compression) let high_compressed = compress(&data, Some(CompressionMode::HIGHCOMPRESSION(9)), true)?; println!("Original: {} bytes", data.len()); println!("Default compressed: {} bytes", compressed_with_prefix.len()); println!("Fast compressed: {} bytes", fast_compressed.len()); println!("High compressed: {} bytes", high_compressed.len()); Ok(()) } ``` ### Response N/A (Rust library usage) ### Response Example N/A (Rust library usage) ``` -------------------------------- ### Streaming Compression with EncoderBuilder in Rust Source: https://context7.com/bozaro/lz4-rs/llms.txt Demonstrates how to use EncoderBuilder to create compression streams. It shows compressing data to a file and to an in-memory buffer. Dependencies include `lz4::EncoderBuilder` and Rust's standard `std::io` traits. ```rust use lz4::EncoderBuilder; use std::fs::File; use std::io::{self, Write}; fn compress_file(input_path: &str, output_path: &str) -> io::Result<()> { let mut input_file = File::open(input_path)?; let output_file = File::create(output_path)?; // Create encoder with compression level 4 (range 0-16, higher = better compression) let mut encoder = EncoderBuilder::new() .level(4) .build(output_file)?; // Copy data through the encoder io::copy(&mut input_file, &mut encoder)?; // Finish encoding and get the output writer back let (output_file, result) = encoder.finish(); result?; Ok(()) } // Compress to in-memory buffer fn compress_bytes(data: &[u8]) -> io::Result> { let mut encoder = EncoderBuilder::new() .level(1) .build(Vec::new())?; encoder.write_all(data)?; let (compressed, result) = encoder.finish(); result?; Ok(compressed) } ``` -------------------------------- ### Streaming Compression with EncoderBuilder Source: https://context7.com/bozaro/lz4-rs/llms.txt Demonstrates how to use EncoderBuilder to compress data to a file or an in-memory buffer using LZ4. ```APIDOC ## Streaming Compression with EncoderBuilder ### Description This section covers the usage of `EncoderBuilder` for creating LZ4 compression encoders. It allows configuration of compression level, block size, and other options. Examples show compressing data to a file and to a `Vec` buffer. ### Method N/A (Rust library usage) ### Endpoint N/A (Rust library usage) ### Parameters N/A (Rust library usage) ### Request Example ```rust use lz4::EncoderBuilder; use std::fs::File; use std::io::{self, Write}; fn compress_file(input_path: &str, output_path: &str) -> io::Result<()> { let mut input_file = File::open(input_path)?; let output_file = File::create(output_path)?; // Create encoder with compression level 4 (range 0-16, higher = better compression) let mut encoder = EncoderBuilder::new() .level(4) .build(output_file)?; // Copy data through the encoder io::copy(&mut input_file, &mut encoder)?; // Finish encoding and get the output writer back let (output_file, result) = encoder.finish(); result?; Ok(()) } // Compress to in-memory buffer fn compress_bytes(data: &[u8]) -> io::Result> { let mut encoder = EncoderBuilder::new() .level(1) .build(Vec::new())?; encoder.write_all(data)?; let (compressed, result) = encoder.finish(); result?; Ok(compressed) } ``` ### Response N/A (Rust library usage) ### Response Example N/A (Rust library usage) ``` -------------------------------- ### Configure EncoderBuilder for Custom Compression Source: https://context7.com/bozaro/lz4-rs/llms.txt Shows how to use EncoderBuilder to fine-tune compression settings such as compression level, block size, block mode, checksums, and auto-flushing behavior. ```rust use lz4::{EncoderBuilder, BlockSize, BlockMode, ContentChecksum}; use std::io::Write; fn configured_encoder_example() -> std::io::Result> { let data = b"Example data to compress with custom settings"; let mut encoder = EncoderBuilder::new() .level(9) .block_size(BlockSize::Max256KB) .block_mode(BlockMode::Linked) .checksum(ContentChecksum::ChecksumEnabled) .auto_flush(true) .build(Vec::new())?; encoder.write_all(data)?; let (compressed, result) = encoder.finish(); result?; Ok(compressed) } ``` -------------------------------- ### Streaming Decompression with Decoder Source: https://context7.com/bozaro/lz4-rs/llms.txt Illustrates how to use the LZ4 Decoder to decompress data from a file or an in-memory buffer. ```APIDOC ## Streaming Decompression with Decoder ### Description This section explains the usage of the `Decoder` struct for transparent LZ4 decompression. It wraps any type implementing `Read` and provides decompression capabilities. Examples cover decompressing from a file and from a `&[u8]` slice. ### Method N/A (Rust library usage) ### Endpoint N/A (Rust library usage) ### Parameters N/A (Rust library usage) ### Request Example ```rust use lz4::Decoder; use std::fs::File; use std::io::{self, Read}; fn decompress_file(input_path: &str, output_path: &str) -> io::Result<()> { let input_file = File::open(input_path)?; let mut output_file = File::create(output_path)?; // Create decoder wrapping the input file let mut decoder = Decoder::new(input_file)?; // Copy decompressed data to output io::copy(&mut decoder, &mut output_file)?; // Finish decoding and verify stream completeness let (input_file, result) = decoder.finish(); result?; Ok(()) } // Decompress from in-memory buffer fn decompress_bytes(compressed: &[u8]) -> io::Result> { use std::io::Cursor; let mut decoder = Decoder::new(Cursor::new(compressed))?; let mut decompressed = Vec::new(); decoder.read_to_end(&mut decompressed)?; let (_, result) = decoder.finish(); result?; Ok(decompressed) } ``` ### Response N/A (Rust library usage) ### Response Example N/A (Rust library usage) ``` -------------------------------- ### Perform Block Mode Decompression in Rust Source: https://context7.com/bozaro/lz4-rs/llms.txt Demonstrates how to decompress data using the lz4 block API. It covers both automatic size detection when using a size prefix and manual size specification for raw compressed blocks. ```rust use lz4::block::{compress, decompress}; fn block_decompress_examples() -> std::io::Result<()> { let original = b"Hello, LZ4 compression! This is some test data.".to_vec(); // Compress with size prefix for automatic size detection let compressed_with_prefix = compress(&original, None, true)?; // Decompress with None - size is read from the prefix let decompressed = decompress(&compressed_with_prefix, None)?; assert_eq!(original, decompressed); // Compress without size prefix let compressed_no_prefix = compress(&original, None, false)?; // Decompress with explicit size (must be known beforehand) let decompressed = decompress(&compressed_no_prefix, Some(original.len() as i32))?; assert_eq!(original, decompressed); Ok(()) } ``` -------------------------------- ### Streaming Decompression with Decoder in Rust Source: https://context7.com/bozaro/lz4-rs/llms.txt Illustrates using the `Decoder` to decompress LZ4 streams. It covers decompressing data from a file and from an in-memory buffer. This requires `lz4::Decoder` and `std::io` traits. ```rust use lz4::Decoder; use std::fs::File; use std::io::{self, Read}; fn decompress_file(input_path: &str, output_path: &str) -> io::Result<()> { let input_file = File::open(input_path)?; let mut output_file = File::create(output_path)?; // Create decoder wrapping the input file let mut decoder = Decoder::new(input_file)?; // Copy decompressed data to output io::copy(&mut decoder, &mut output_file)?; // Finish decoding and verify stream completeness let (input_file, result) = decoder.finish(); result?; Ok(()) } // Decompress from in-memory buffer fn decompress_bytes(compressed: &[u8]) -> io::Result> { use std::io::Cursor; let mut decoder = Decoder::new(Cursor::new(compressed))?; let mut decompressed = Vec::new(); decoder.read_to_end(&mut decompressed)?; let (_, result) = decoder.finish(); result?; Ok(decompressed) } ``` -------------------------------- ### Retrieve LZ4 C Library Version Source: https://context7.com/bozaro/lz4-rs/llms.txt Retrieves and parses the underlying LZ4 C library version number, which is useful for debugging and ensuring compatibility. ```rust use lz4::version; fn check_lz4_version() { let ver = version(); let major = ver / 10000; let minor = (ver % 10000) / 100; let patch = ver % 100; println!("LZ4 version: {}.{}.{} ({})", major, minor, patch, ver); } ``` === COMPLETE CONTENT === This response contains all available snippets from this library. No additional content exists. Do not make further requests.