### Build Custom Filter Chains (Rust) Source: https://context7.com/portable-network-archive/liblzma-rs/llms.txt Enables the construction of custom filter chains for specialized compression scenarios. This example demonstrates adding an x86 BCJ filter for improved compression of executables, followed by LZMA2 compression. It requires specifying the filters and an integrity check. ```Rust use liblzma::stream::{Filters, LzmaOptions, Stream, Check}; // Create a filter chain with x86 BCJ filter + LZMA2 let mut filters = Filters::new(); let options = LzmaOptions::new_preset(6).unwrap(); filters.x86(); // Add x86 binary filter (improves compression of executables) filters.lzma2(&options); // Add LZMA2 compression let encoder = Stream::new_stream_encoder(&filters, Check::Crc64).unwrap(); println!("Created encoder with x86 + LZMA2 filter chain"); ``` -------------------------------- ### Rust: Maximum Compression with PRESET_EXTREME Source: https://context7.com/portable-network-archive/liblzma-rs/llms.txt Demonstrates how to use the PRESET_EXTREME flag with XzEncoder for maximum compression ratio, contrasting it with normal compression. This method is slower but yields a better compression ratio. ```rust use liblzma::read::XzEncoder; use liblzma::stream::PRESET_EXTREME; use std::io::Read; let data = vec![42u8; 100_000]; // Normal compression let mut normal_encoder = XzEncoder::new(&data[..], 6); let mut normal_compressed = Vec::new(); normal_encoder.read_to_end(&mut normal_compressed).unwrap(); // Extreme compression (slower but better ratio) let mut extreme_encoder = XzEncoder::new(&data[..], 6 | PRESET_EXTREME); let mut extreme_compressed = Vec::new(); extreme_encoder.read_to_end(&mut extreme_compressed).unwrap(); println!("Normal: {} bytes, Extreme: {} bytes", normal_compressed.len(), extreme_compressed.len()); ``` -------------------------------- ### Migrate from xz2 to liblzma in Cargo.toml Source: https://github.com/portable-network-archive/liblzma-rs/blob/main/README.md Instructions for updating the dependency in the Cargo.toml file to switch from the xz2 crate to the liblzma crate. ```toml [dependencies] -xz2 = "0.1.7" +liblzma = "0.1.7" ``` -------------------------------- ### XzEncoder - Write-Based Compression Source: https://context7.com/portable-network-archive/liblzma-rs/llms.txt Shows how to use the write-based XzEncoder to compress data by writing to an output stream. It highlights the necessity of calling finish() to finalize the compressed stream. ```rust use liblzma::write::XzEncoder; use std::io::Write; let mut compressed_output = Vec::new(); { let mut encoder = XzEncoder::new(&mut compressed_output, 6); encoder.write_all(b"First chunk of data").unwrap(); encoder.write_all(b"Second chunk of data").unwrap(); encoder.finish().unwrap(); } println!("Total compressed size: {} bytes", compressed_output.len()); ``` -------------------------------- ### Rust: Legacy LZMA Format Encoding and Decoding Source: https://context7.com/portable-network-archive/liblzma-rs/llms.txt Shows how to create encoders and decoders for the legacy .lzma format using Stream::new_lzma_encoder and Stream::new_lzma_decoder for compatibility with LZMA Utils. It demonstrates setting up LZMA1 options and using them with XzEncoder and XzDecoder. ```rust use liblzma::stream::{Stream, LzmaOptions}; use liblzma::read::{XzEncoder, XzDecoder}; use std::io::Read; // Create LZMA1 encoder for legacy format let options = LzmaOptions::new_preset(6).unwrap(); let encoder_stream = Stream::new_lzma_encoder(&options).unwrap(); // Use with read-based API let data = b"Legacy format data"; let encoder = XzEncoder::new_stream(&data[..], encoder_stream); // Create matching decoder let decoder_stream = Stream::new_lzma_decoder(u64::MAX).unwrap(); let mut decoder = XzDecoder::new_stream(encoder, decoder_stream); let mut result = Vec::new(); decoder.read_to_end(&mut result).unwrap(); assert_eq!(result, data); ``` -------------------------------- ### Configure LZMA Compression Options (Rust) Source: https://context7.com/portable-network-archive/liblzma-rs/llms.txt Allows fine-tuning of LZMA compression parameters to achieve optimal compression ratios and performance. It provides methods to set dictionary size, literal context bits, position bits, compression mode, match finder, nice length, and depth. ```Rust use liblzma::stream::{LzmaOptions, Stream, Mode, MatchFinder}; let mut options = LzmaOptions::new_preset(6).unwrap(); options .dict_size(1 << 20) // 1MB dictionary .literal_context_bits(3) // Default literal context bits .position_bits(2) // Default position bits .mode(Mode::Normal) // Normal compression mode .match_finder(MatchFinder::BinaryTree4) // BT4 match finder .nice_len(64) // Match length threshold .depth(0); // Auto depth let encoder = Stream::new_lzma_encoder(&options).unwrap(); println!("Created LZMA encoder with custom options"); ``` -------------------------------- ### AutoFinishXzEncoder - Auto-Finalizing Encoder Source: https://context7.com/portable-network-archive/liblzma-rs/llms.txt Illustrates the use of AutoFinishXzEncoder for RAII-style resource management. The stream is automatically finalized when the encoder goes out of scope. ```rust use liblzma::write::XzEncoder; use std::io::Write; let mut output = Vec::new(); { let encoder = XzEncoder::new(&mut output, 6); let mut auto_encoder = encoder.auto_finish(); auto_encoder.write_all(b"Auto-finalized data").unwrap(); } println!("Output size: {} bytes", output.len()); ``` -------------------------------- ### Auto-Detect Compression Format (Rust) Source: https://context7.com/portable-network-archive/liblzma-rs/llms.txt Creates a decoder that automatically detects and handles both .xz and .lzma compressed data formats. This simplifies decompression by eliminating the need to know the specific format beforehand. It takes a memory limit and a flag for concatenated streams. ```Rust use liblzma::stream::{Stream, CONCATENATED}; // Decoder that handles both XZ and LZMA formats automatically let decoder = Stream::new_auto_decoder(u64::MAX, CONCATENATED).unwrap(); println!("Created auto-detecting decoder"); ``` -------------------------------- ### LzmaOptions Source: https://context7.com/portable-network-archive/liblzma-rs/llms.txt Allows fine-tuning of LZMA compression parameters for optimal compression ratios and performance. ```APIDOC ## LzmaOptions - Configure LZMA Compression ### Description Fine-tune LZMA compression parameters for optimal results. ### Method `LzmaOptions::new_preset` and associated methods. ### Parameters - `preset_level` (u8) - Compression preset level (0-9). - `dict_size` (u64) - Dictionary size. - `literal_context_bits` (u32) - Literal context bits. - `position_bits` (u32) - Position bits. - `mode` (Mode) - Compression mode (`Fast`, `Normal`, `Best`). - `match_finder` (MatchFinder) - Match finder algorithm. - `nice_len` (u32) - Nice length for matches. - `depth` (u32) - Search depth. ### Request Example ```rust use liblzma::stream::{LzmaOptions, Stream, Mode, MatchFinder}; let mut options = LzmaOptions::new_preset(6).unwrap(); options .dict_size(1 << 20) .literal_context_bits(3) .position_bits(2) .mode(Mode::Normal) .match_finder(MatchFinder::BinaryTree4) .nice_len(64) .depth(0); let encoder = Stream::new_lzma_encoder(&options).unwrap(); ``` ### Response Example ```rust // LZMA encoder is created with custom options println!("Created LZMA encoder with custom options"); ``` ``` -------------------------------- ### Migrate from xz2 to liblzma in Rust source code Source: https://github.com/portable-network-archive/liblzma-rs/blob/main/README.md Instructions for updating import statements in Rust source files to switch from the xz2 crate to the liblzma crate. ```rust -use xz2; +use liblzma; ``` -------------------------------- ### Migrate from lzma-sys to liblzma-sys Source: https://github.com/portable-network-archive/liblzma-rs/blob/main/liblzma-sys/README.md This snippet shows how to migrate from the `lzma-sys` crate to `liblzma-sys` by updating the `Cargo.toml` file and changing the import statement in Rust source files. This ensures compatibility when switching to the forked crate. ```diff # Cargo.toml [dependencies] -lzma-sys = "0.1.20" +liblzma-sys = "0.1.20" ``` ```diff // *.rs -use lzma_sys; +use liblzma_sys; ``` -------------------------------- ### XzDecoder - BufRead-Based Decompression Source: https://context7.com/portable-network-archive/liblzma-rs/llms.txt Demonstrates efficient decompression using the BufRead-based decoder, suitable for streaming compressed data from buffered sources. ```rust use liblzma::bufread::{XzEncoder, XzDecoder}; use std::io::{BufReader, Read}; let original = b"Original data for bufread test"; let buf_reader = BufReader::new(&original[..]); let encoder = XzEncoder::new(buf_reader, 6); let mut decoder = XzDecoder::new(BufReader::new(encoder)); let mut result = Vec::new(); decoder.read_to_end(&mut result).unwrap(); assert_eq!(result, original); ``` -------------------------------- ### Read-Based Compression with XzEncoder (Rust) Source: https://context7.com/portable-network-archive/liblzma-rs/llms.txt Wraps an uncompressed data source and provides compressed data when read, implementing the `Read` trait. This allows integrating compression directly into read operations, suitable for scenarios where data is processed incrementally. It takes a byte slice and a compression level. ```rust use liblzma::read::XzEncoder; use std::io::Read; let data = "Hello, World! This will be compressed as you read."; let mut encoder = XzEncoder::new(data.as_bytes(), 6); let mut compressed = Vec::new(); encoder.read_to_end(&mut compressed).unwrap(); println!("Read {} compressed bytes", compressed.len()); println!("Total input consumed: {} bytes", encoder.total_in()); println!("Total output produced: {} bytes", encoder.total_out()); ``` -------------------------------- ### Configure Multithreaded Compression (Rust) Source: https://context7.com/portable-network-archive/liblzma-rs/llms.txt Configures multithreaded encoding and decoding streams using `MtStreamBuilder`. This requires enabling the `parallel` feature in `liblzma`. It allows setting the number of threads, compression level, integrity check, block size, timeouts, and memory limits. ```Rust // Requires: liblzma = { version = "0.4", features = ["parallel"] } #[cfg(feature = "parallel")] use liblzma::stream::{MtStreamBuilder, Check}; #[cfg(feature = "parallel")] fn create_parallel_encoder() { let encoder = MtStreamBuilder::new() .threads(4) // Use 4 threads .preset(6) // Compression level 6 .check(Check::Crc64) // CRC64 integrity check .block_size(1 << 20) // 1MB blocks .timeout_ms(300) // 300ms timeout for non-blocking behavior .memlimit_threading(1 << 30) // 1GB memory limit for threading .memlimit_stop(u64::MAX) // No hard memory limit .encoder() .unwrap(); println!("Memory usage estimate: {} bytes", MtStreamBuilder::new().memusage()); } ``` -------------------------------- ### XzEncoder - BufRead-Based Compression Source: https://context7.com/portable-network-archive/liblzma-rs/llms.txt Provides an efficient way to compress data using types that implement BufRead, minimizing extra buffering during the compression process. ```rust use liblzma::bufread::XzEncoder; use std::io::{BufReader, Read}; let data = b"Data for bufread-based compression"; let buf_reader = BufReader::new(&data[..]); let mut encoder = XzEncoder::new(buf_reader, 6); let mut compressed = Vec::new(); encoder.read_to_end(&mut compressed).unwrap(); println!("Compressed {} bytes to {} bytes", data.len(), compressed.len()); ``` -------------------------------- ### Process Data with Dynamic Output (Rust) Source: https://context7.com/portable-network-archive/liblzma-rs/llms.txt Processes input data and appends the compressed or decompressed output to a dynamically sized vector. This method automatically manages buffer growth, simplifying output handling. It takes input data, an output vector, and an action (e.g., Finish) as parameters. ```Rust use liblzma::stream::{Stream, Check, Action, Status}; let mut encoder = Stream::new_easy_encoder(6, Check::Crc64).unwrap(); let input = b"Data to encode"; let mut output = Vec::new(); // Process with automatic output buffer management let status = encoder.process_vec(input, &mut output, Action::Finish).unwrap(); println!("Output size: {}, Status: {:?}", output.len(), status); ``` -------------------------------- ### Legacy LZMA Format Support Source: https://context7.com/portable-network-archive/liblzma-rs/llms.txt Creating encoders and decoders compatible with the legacy .lzma format using LzmaOptions. ```APIDOC ## Stream::new_lzma_encoder - Legacy .lzma Format ### Description Initializes a stream encoder specifically for the legacy LZMA format, ensuring compatibility with older LZMA Utils archives. ### Method N/A (Rust Library Function) ### Parameters #### Request Body - **options** (LzmaOptions) - Required - Configuration options including preset level. ### Request Example ```rust let options = LzmaOptions::new_preset(6).unwrap(); let encoder_stream = Stream::new_lzma_encoder(&options).unwrap(); ``` ### Response #### Success Response (200) - **encoder_stream** (Stream) - A configured stream object ready for legacy LZMA encoding. ``` -------------------------------- ### Stream-based I/O Source: https://context7.com/portable-network-archive/liblzma-rs/llms.txt Streaming compression and decompression using Rust's standard Read and Write traits. ```APIDOC ## XzEncoder ### Description Wraps an uncompressed data source and provides compressed data when read. Implements the Read trait. ### Parameters - **reader** (R: Read) - Required - The source of uncompressed data. - **level** (u32) - Required - Compression level (0-9). ## XzDecoder ### Description Wraps a compressed data source and provides decompressed data when read. Implements the Read trait. ### Parameters - **reader** (R: Read) - Required - The source of compressed XZ data. ``` -------------------------------- ### Parallel Read-Based Compression (Rust) Source: https://context7.com/portable-network-archive/liblzma-rs/llms.txt Creates a parallel compression encoder utilizing the `Read` API. This method is suitable for compressing data streams efficiently across multiple threads. It requires the `parallel` feature and takes the data to be compressed and the compression level as input. ```Rust // Requires: liblzma = { version = "0.4", features = ["parallel"] } #[cfg(feature = "parallel")] use liblzma::read::XzEncoder; #[cfg(feature = "parallel")] use std::io::Read; #[cfg(feature = "parallel")] fn parallel_compress() { let data = vec![0u8; 10 * 1024 * 1024]; // 10MB of data let mut encoder = XzEncoder::new_parallel(&data[..], 6); let mut compressed = Vec::new(); encoder.read_to_end(&mut compressed).unwrap(); println!("Parallel compressed {} MB to {} bytes", data.len() / 1024 / 1024, compressed.len()); } ``` -------------------------------- ### Convenience Functions Source: https://context7.com/portable-network-archive/liblzma-rs/llms.txt High-level functions for simple, single-call compression and decompression of data buffers. ```APIDOC ## encode_all ### Description Compresses all data from a source into XZ format in a single call. ### Method Function Call ### Parameters - **data** (&[u8]) - Required - The source data to compress. - **level** (u32) - Required - Compression level (0-9). ### Request Example let compressed = encode_all(&data[..], 6).unwrap(); ### Response - **Vec** - The compressed XZ data. ## decode_all ### Description Decompresses XZ-formatted data in a single call. ### Method Function Call ### Parameters - **data** (&[u8]) - Required - The XZ compressed data. ### Response - **Vec** - The decompressed data. ``` -------------------------------- ### XzDecoder::new_multi_decoder - Handle Concatenated XZ Streams Source: https://context7.com/portable-network-archive/liblzma-rs/llms.txt Demonstrates how to decode multiple concatenated XZ streams from a single input source using the multi_decoder functionality. It reads concatenated compressed data and outputs the combined original content. ```rust use liblzma::read::{XzEncoder, XzDecoder}; use std::io::Read; let data1 = b"First stream content"; let data2 = b"Second stream content"; let mut combined = Vec::new(); { let mut enc1 = XzEncoder::new(&data1[..], 6); enc1.read_to_end(&mut combined).unwrap(); } { let mut enc2 = XzEncoder::new(&data2[..], 6); enc2.read_to_end(&mut combined).unwrap(); } let mut decoder = XzDecoder::new_multi_decoder(&combined[..]); let mut result = Vec::new(); decoder.read_to_end(&mut result).unwrap(); assert_eq!(result, b"First stream contentSecond stream content"); ``` -------------------------------- ### Stream::new_easy_encoder Source: https://context7.com/portable-network-archive/liblzma-rs/llms.txt Creates a low-level encoding stream with specific compression settings. ```APIDOC ## [FUNCTION] Stream::new_easy_encoder ### Description Creates a low-level encoding stream with preset compression level and integrity check. ### Method N/A (Rust Function) ### Endpoint liblzma::stream::Stream::new_easy_encoder ### Parameters #### Request Body - **preset** (u32) - Required - Compression level. - **check** (Check) - Required - Integrity check type (e.g., Crc64). ### Request Example let mut encoder = Stream::new_easy_encoder(6, Check::Crc64).unwrap(); ``` -------------------------------- ### XzDecoder - Write-Based Decompression Source: https://context7.com/portable-network-archive/liblzma-rs/llms.txt Demonstrates decompressing data using the write-based XzDecoder. Compressed data is written to the decoder, which then writes the original data to the provided output buffer. ```rust use liblzma::write::{XzEncoder, XzDecoder}; use std::io::Write; let mut compressed = Vec::new(); { let mut encoder = XzEncoder::new(&mut compressed, 6); encoder.write_all(b"Hello from write-based compression!").unwrap(); encoder.finish().unwrap(); } let mut decompressed = Vec::new(); { let mut decoder = XzDecoder::new(&mut decompressed); decoder.write_all(&compressed).unwrap(); decoder.finish().unwrap(); } assert_eq!(decompressed, b"Hello from write-based compression!"); ``` -------------------------------- ### Read-Based Decompression with XzDecoder (Rust) Source: https://context7.com/portable-network-archive/liblzma-rs/llms.txt Wraps a compressed data source and provides decompressed data when read, implementing the `Read` trait. This enables seamless decompression within read operations, useful for processing compressed data streams. It takes an object implementing `Read` (e.g., an `XzEncoder` or a file handle) as input. ```rust use liblzma::read::{XzEncoder, XzDecoder}; use std::io::Read; // First compress some data let original = b"Data to round-trip through compression"; let encoder = XzEncoder::new(&original[..], 6); // Then decompress by wrapping the encoder let mut decoder = XzDecoder::new(encoder); let mut result = String::new(); decoder.read_to_string(&mut result).unwrap(); assert_eq!(result, "Data to round-trip through compression"); ``` -------------------------------- ### Stream::new_easy_encoder - Create XZ Encoder Stream Source: https://context7.com/portable-network-archive/liblzma-rs/llms.txt Uses the low-level stream API to create an easy encoder with specific compression settings. This provides granular control over the compression process and status handling. ```rust use liblzma::stream::{Stream, Check, Action, Status}; let mut encoder = Stream::new_easy_encoder(6, Check::Crc64).unwrap(); let input = b"Data to compress with low-level API"; let mut output = vec![0u8; 1024]; let status = encoder.process(input, &mut output, Action::Finish).unwrap(); let compressed_size = encoder.total_out() as usize; output.truncate(compressed_size); println!("Compressed to {} bytes, status: {:?}", compressed_size, status); ``` -------------------------------- ### XzEncoder::new_parallel Source: https://context7.com/portable-network-archive/liblzma-rs/llms.txt Creates a parallel compression encoder using the read API. This is suitable for compressing large amounts of data efficiently across multiple threads. ```APIDOC ## XzEncoder::new_parallel - Parallel Read-Based Compression ### Description Create a parallel compression encoder using the read API. ### Method `XzEncoder::new_parallel` ### Parameters - `data` (&[u8]) - The data to be compressed. - `level` (u8) - The compression level (0-9). ### Request Example ```rust // Requires: liblzma = { version = "0.4", features = ["parallel"] } #[cfg(feature = "parallel")] use liblzma::read::XzEncoder; #[cfg(feature = "parallel")] use std::io::Read; #[cfg(feature = "parallel")] fn parallel_compress() { let data = vec![0u8; 10 * 1024 * 1024]; // 10MB of data let mut encoder = XzEncoder::new_parallel(&data[..], 6); let mut compressed = Vec::new(); encoder.read_to_end(&mut compressed).unwrap(); } ``` ### Response Example ```rust // Compressed data size is printed println!("Parallel compressed {} MB to {} bytes", data.len() / 1024 / 1024, compressed.len()); ``` ``` -------------------------------- ### Create XZ Decoder Stream (Rust) Source: https://context7.com/portable-network-archive/liblzma-rs/llms.txt Creates a low-level decoding stream for XZ data with configurable memory limits and support for concatenated streams. It takes a maximum memory limit and a flag for concatenated stream support as input. The output is a `Stream` object ready for processing. ```Rust use liblzma::stream::{Stream, Check, Action, Status, CONCATENATED}; // Create decoder with max memory limit and support for concatenated streams let mut decoder = Stream::new_stream_decoder(u64::MAX, CONCATENATED).unwrap(); // Assuming `compressed_data` contains valid XZ data let compressed_data: &[u8] = &[]; // placeholder let mut output = vec![0u8; 4096]; let _status = decoder.process(compressed_data, &mut output, Action::Finish); println!("Decompressed {} bytes", decoder.total_out()); ``` -------------------------------- ### Filters Source: https://context7.com/portable-network-archive/liblzma-rs/llms.txt Enables building custom filter chains for specialized compression scenarios, such as combining x86 BCJ with LZMA2. ```APIDOC ## Filters - Custom Filter Chain ### Description Build custom filter chains for specialized compression scenarios. ### Method `Filters::new`, `filters.x86()`, `filters.lzma2()` ### Parameters - `options` (LzmaOptions) - LZMA2 compression options. ### Request Example ```rust use liblzma::stream::{Filters, LzmaOptions, Stream, Check}; let mut filters = Filters::new(); let options = LzmaOptions::new_preset(6).unwrap(); filters.x86(); filters.lzma2(&options); let encoder = Stream::new_stream_encoder(&filters, Check::Crc64).unwrap(); ``` ### Response Example ```rust // Encoder with custom filter chain is created println!("Created encoder with x86 + LZMA2 filter chain"); ``` ``` -------------------------------- ### Stream::new_auto_decoder Source: https://context7.com/portable-network-archive/liblzma-rs/llms.txt Creates a decoder that automatically detects and handles both .xz and .lzma compressed data formats. ```APIDOC ## Stream::new_auto_decoder - Auto-Detect Format ### Description Creates a decoder that automatically detects .xz or .lzma format. ### Method `Stream::new_auto_decoder` ### Parameters - `memlimit` (u64) - Maximum memory limit for the decoder. - `flags` (u32) - Flags for the decoder, e.g., `CONCATENATED`. ### Request Example ```rust use liblzma::stream::{Stream, CONCATENATED}; let decoder = Stream::new_auto_decoder(u64::MAX, CONCATENATED).unwrap(); ``` ### Response Example ```rust // Auto-detecting decoder is created println!("Created auto-detecting decoder"); ``` ``` -------------------------------- ### Extreme Compression Preset Source: https://context7.com/portable-network-archive/liblzma-rs/llms.txt Utilizing the PRESET_EXTREME flag to achieve higher compression ratios at the expense of encoding speed. ```APIDOC ## PRESET_EXTREME - Maximum Compression ### Description Applies the extreme compression preset to the XzEncoder to maximize the compression ratio for data streams. ### Method N/A (Rust Library Function) ### Parameters #### Request Body - **preset** (integer) - Required - The base compression level (0-9) combined with the PRESET_EXTREME flag. ### Request Example ```rust let mut extreme_encoder = XzEncoder::new(&data[..], 6 | PRESET_EXTREME); ``` ### Response #### Success Response (200) - **compressed_data** (Vec) - The resulting byte stream after applying extreme compression. ``` -------------------------------- ### XzEncoder (Write-Based) Source: https://context7.com/portable-network-archive/liblzma-rs/llms.txt Compresses data written to the encoder and outputs it to a provided writer. ```APIDOC ## [STRUCT] liblzma::write::XzEncoder ### Description Accepts uncompressed data written to it and writes compressed data to an output stream. ### Method N/A (Rust Struct) ### Endpoint liblzma::write::XzEncoder::new ### Parameters #### Request Body - **writer** (W: Write) - Required - The output stream to write compressed data to. - **preset** (u32) - Required - The compression preset level (0-9). ### Request Example let mut encoder = XzEncoder::new(&mut output, 6); encoder.write_all(b"data").unwrap(); encoder.finish().unwrap(); ``` -------------------------------- ### Stream Compression to Writer (Rust) Source: https://context7.com/portable-network-archive/liblzma-rs/llms.txt Compresses data from a reader and writes the compressed output directly to a writer. This function is efficient for streaming large amounts of data without loading everything into memory. It takes a byte slice as the source and a mutable Vec as the destination. ```rust use liblzma::copy_encode; use std::io::Cursor; let input_data = b"Data to compress into a buffer"; let mut output_buffer = Vec::new(); // Compress data from input to output copy_encode(&input_data[..], &mut output_buffer, 6).unwrap(); println!("Compressed {} bytes to {} bytes", input_data.len(), output_buffer.len()); ``` -------------------------------- ### Encode Data to XZ Format (Rust) Source: https://context7.com/portable-network-archive/liblzma-rs/llms.txt Compresses all provided data into XZ format using a specified compression level (0-9). This function is a high-level convenience function that takes a byte slice as input and returns a Vec containing the compressed data. It's useful for single-shot compression tasks. ```rust use liblzma::{encode_all, decode_all}; // Compress data with compression level 6 let original_data = b"Hello, World! This is some data to compress."; let compressed = encode_all(&original_data[..], 6).unwrap(); // The compressed data is now in XZ format println!("Original size: {} bytes", original_data.len()); println!("Compressed size: {} bytes", compressed.len()); // Verify by decompressing let decompressed = decode_all(&compressed[..]).unwrap(); assert_eq!(original_data.to_vec(), decompressed); ``` -------------------------------- ### MtStreamBuilder Source: https://context7.com/portable-network-archive/liblzma-rs/llms.txt Configures multithreaded encoding or decoding streams. Requires the 'parallel' feature to be enabled. ```APIDOC ## MtStreamBuilder - Parallel Compression Configuration ### Description Configure multithreaded encoding/decoding streams. Requires the `parallel` feature. ### Method `MtStreamBuilder::new` and associated methods. ### Parameters - `threads` (usize) - Number of threads to use. - `preset` (u8) - Compression preset level. - `check` (Check) - Integrity check type. - `block_size` (u64) - Size of compression blocks. - `timeout_ms` (u32) - Timeout for non-blocking operations in milliseconds. - `memlimit_threading` (u64) - Memory limit specifically for threading operations. - `memlimit_stop` (u64) - Hard memory limit to stop compression. ### Request Example ```rust // Requires: liblzma = { version = "0.4", features = ["parallel"] } #[cfg(feature = "parallel")] use liblzma::stream::{MtStreamBuilder, Check}; #[cfg(feature = "parallel")] fn create_parallel_encoder() { let encoder = MtStreamBuilder::new() .threads(4) .preset(6) .check(Check::Crc64) .block_size(1 << 20) .timeout_ms(300) .memlimit_threading(1 << 30) .memlimit_stop(u64::MAX) .encoder() .unwrap(); } ``` ### Response Example ```rust // Memory usage estimate can be obtained println!("Memory usage estimate: {} bytes", MtStreamBuilder::new().memusage()); ``` ``` -------------------------------- ### XzDecoder::new_multi_decoder Source: https://context7.com/portable-network-archive/liblzma-rs/llms.txt Decodes multiple concatenated XZ streams from a single input source. ```APIDOC ## [FUNCTION] XzDecoder::new_multi_decoder ### Description Decodes multiple concatenated XZ streams from a single input source, allowing for seamless processing of combined compressed data. ### Method N/A (Rust Function) ### Endpoint liblzma::read::XzDecoder::new_multi_decoder ### Parameters #### Request Body - **input** (Read) - Required - The input source containing one or more concatenated XZ streams. ### Request Example let decoder = XzDecoder::new_multi_decoder(&combined_data[..]); ### Response #### Success Response (200) - **result** (Vec) - The fully decompressed content of all concatenated streams. ``` -------------------------------- ### Stream Decompression to Writer (Rust) Source: https://context7.com/portable-network-archive/liblzma-rs/llms.txt Decompresses data from a reader and writes the decompressed output directly to a writer. This function is ideal for processing compressed streams, reading from a source (like a file or network stream) and writing the uncompressed data to a destination. It takes a byte slice as the compressed source and a mutable Vec as the decompressed destination. ```rust use liblzma::{copy_encode, copy_decode}; let original = b"Original uncompressed data"; let mut compressed = Vec::new(); copy_encode(&original[..], &mut compressed, 6).unwrap(); // Decompress to a new buffer let mut decompressed = Vec::new(); copy_decode(&compressed[..], &mut decompressed).unwrap(); assert_eq!(original.to_vec(), decompressed); ``` -------------------------------- ### Stream::process_vec Source: https://context7.com/portable-network-archive/liblzma-rs/llms.txt Processes data using an encoder and appends the compressed output to a vector, automatically managing buffer growth. ```APIDOC ## Stream::process_vec - Process Data with Dynamic Output ### Description Process data and append output to a vector, automatically handling buffer management. ### Method `Stream::process_vec` ### Parameters - `input` (&[u8]) - The data to be compressed. - `output` (&mut Vec) - The vector to store the compressed output. - `action` (Action) - The action to perform, e.g., `Action::Finish`. ### Request Example ```rust use liblzma::stream::{Stream, Check, Action}; let mut encoder = Stream::new_easy_encoder(6, Check::Crc64).unwrap(); let input = b"Data to encode"; let mut output = Vec::new(); let status = encoder.process_vec(input, &mut output, Action::Finish).unwrap(); ``` ### Response Example ```rust // Output size and status are printed println!("Output size: {}, Status: {:?}", output.len(), status); ``` ``` -------------------------------- ### Stream::new_stream_decoder Source: https://context7.com/portable-network-archive/liblzma-rs/llms.txt Creates a low-level decoding stream with configurable memory limit and flags for XZ data. Supports concatenated streams. ```APIDOC ## Stream::new_stream_decoder - Create XZ Decoder Stream ### Description Creates a low-level decoding stream with configurable memory limit and flags. ### Method `Stream::new_stream_decoder` ### Parameters - `memlimit` (u64) - Maximum memory limit for the decoder. - `flags` (u32) - Flags for the decoder, e.g., `CONCATENATED`. ### Request Example ```rust use liblzma::stream::{Stream, CONCATENATED}; let mut decoder = Stream::new_stream_decoder(u64::MAX, CONCATENATED).unwrap(); ``` ### Response Example ```rust // Decoder stream is created successfully println!("Decoder created"); ``` ``` -------------------------------- ### Decode XZ Data (Rust) Source: https://context7.com/portable-network-archive/liblzma-rs/llms.txt Decompresses XZ-formatted data in a single call. This high-level function takes a byte slice containing XZ compressed data and returns a Vec with the original, decompressed bytes. It's suitable for scenarios where the entire compressed data is available in memory. ```rust use liblzma::{encode_all, decode_all}; // Given some XZ compressed data let original = vec![1u8, 2, 3, 4, 5, 6, 7, 8]; let compressed = encode_all(&original[..], 6).unwrap(); // Decompress it back let decompressed = decode_all(&compressed[..]).unwrap(); assert_eq!(original, decompressed); ``` === COMPLETE CONTENT === This response contains all available snippets from this library. No additional content exists. Do not make further requests.