//! This module contains functionality for compression.
usecrate::alloc::vec; usecrate::alloc::vec::Vec;
mod buffer; pubmod core; pubmod stream; useself::core::*;
/// How much processing the compressor should do to compress the data. /// `NoCompression` and `Bestspeed` have special meanings, the other levels determine the number /// of checks for matches in the hash chains and whether to use lazy or greedy parsing. #[repr(i32)] #[derive(Debug, Copy, Clone, PartialEq, Eq, Hash)] pubenum CompressionLevel { /// Don't do any compression, only output uncompressed blocks.
NoCompression = 0, /// Fast compression. Uses a special compression routine that is optimized for speed.
BestSpeed = 1, /// Slow/high compression. Do a lot of checks to try to find good matches.
BestCompression = 9, /// Even more checks, can be very slow.
UberCompression = 10, /// Default compromise between speed and compression.
DefaultLevel = 6, /// Use the default compression level.
DefaultCompression = -1,
}
/// Compress the input data to a vector, using the specified compression level (0-10). pubfn compress_to_vec(input: &[u8], level: u8) -> Vec<u8> {
compress_to_vec_inner(input, level, 0, 0)
}
/// Compress the input data to a vector, using the specified compression level (0-10), and with a /// zlib wrapper. pubfn compress_to_vec_zlib(input: &[u8], level: u8) -> Vec<u8> {
compress_to_vec_inner(input, level, 1, 0)
}
/// Simple function to compress data to a vec. fn compress_to_vec_inner(input: &[u8], level: u8, window_bits: i32, strategy: i32) -> Vec<u8> { // The comp flags function sets the zlib flag if the window_bits parameter is > 0. let flags = create_comp_flags_from_zip_params(level.into(), window_bits, strategy); letmut compressor = CompressorOxide::new(flags); letmut output = vec![0; ::core::cmp::max(input.len() / 2, 2)];
match status {
TDEFLStatus::Done => {
output.truncate(out_pos); break;
}
TDEFLStatus::Okay => { // We need more space, so resize the vector. if output.len().saturating_sub(out_pos) < 30 {
output.resize(output.len() * 2, 0)
}
} // Not supposed to happen unless there is a bug.
_ => panic!("Bug! Unexpectedly failed to compress!"),
}
}
output
}
#[cfg(test)] mod test { usesuper::{compress_to_vec, compress_to_vec_inner, CompressionStrategy}; usecrate::inflate::decompress_to_vec; use alloc::vec;
let res = compress_to_vec(test_data, 1);
assert_eq!(&check[..], res.as_slice());
let res = compress_to_vec(test_data, 9);
assert_eq!(&check[..], res.as_slice());
}
#[test] fn compress_huff_only() { let test_data = b"Deflate late";
let res = compress_to_vec_inner(test_data, 1, 0, CompressionStrategy::HuffmanOnly as i32); let d = decompress_to_vec(res.as_slice()).expect("Failed to decompress!");
assert_eq!(test_data, d.as_slice());
}
/// Test that a raw block compresses fine. #[test] fn compress_raw() { let text = b"Hello, zlib!"; let encoded = { let len = text.len(); let notlen = !len; letmut encoded = vec![ 1,
len as u8,
(len >> 8) as u8,
notlen as u8,
(notlen >> 8) as u8,
];
encoded.extend_from_slice(&text[..]);
encoded
};
let res = compress_to_vec(text, 0);
assert_eq!(encoded, res.as_slice());
}
#[test] fn short() { let test_data = [10, 10, 10, 10, 10, 55]; let c = compress_to_vec(&test_data, 9);
let d = decompress_to_vec(c.as_slice()).expect("Failed to decompress!");
assert_eq!(&test_data, d.as_slice()); // Check that a static block is used here, rather than a raw block // , so the data is actually compressed. // (The optimal compressed length would be 5, but neither miniz nor zlib manages that either // as neither checks matches against the byte at index 0.)
assert!(c.len() <= 6);
}
}
Messung V0.5 in Prozent
¤ Dauer der Verarbeitung: 0.11 Sekunden
(vorverarbeitet am 2026-06-18)
¤
Die Informationen auf dieser Webseite wurden
nach bestem Wissen sorgfältig zusammengestellt. Es wird jedoch weder Vollständigkeit, noch Richtigkeit,
noch Qualität der bereit gestellten Informationen zugesichert.
Bemerkung:
Die farbliche Syntaxdarstellung und die Messung sind noch experimentell.