81 lines
3.3 KiB
Rust
81 lines
3.3 KiB
Rust
use std::fs::{File, OpenOptions};
|
|
use std::io;
|
|
use std::io::{Read, Write};
|
|
use std::path::Path;
|
|
use flate2::Compression;
|
|
use flate2::read::DeflateDecoder;
|
|
use flate2::write::DeflateEncoder;
|
|
|
|
pub struct TestCompression {}
|
|
|
|
impl TestCompression {
|
|
pub fn compress_string(input: &str) -> Vec<u8> {
|
|
let mut encoder = DeflateEncoder::new(Vec::new(), Compression::default());
|
|
encoder
|
|
.write_all(input.as_bytes())
|
|
.expect("Failed to write data");
|
|
encoder.finish().expect("Failed to finish compression")
|
|
}
|
|
|
|
pub fn decompress_to_string(compressed_data: &[u8]) -> Result<String, std::io::Error> {
|
|
let mut decoder = DeflateDecoder::new(compressed_data);
|
|
let mut decompressed = String::new();
|
|
decoder.read_to_string(&mut decompressed)?;
|
|
Ok(decompressed)
|
|
}
|
|
|
|
/// Load a pre-compressed test result to a string for comparison in an assert macro
|
|
pub fn load_compressed_file_to_string(source: &Path) -> String {
|
|
let mut compressed_file = File::open(&source).expect(format!("Unable to open compressed file at [{}]", source.display()).as_str());
|
|
let mut compressed_data = Vec::new();
|
|
compressed_file.read_to_end(&mut compressed_data)
|
|
.expect(format!("Unable to read compressed data from [{}]", source.display()).as_str());
|
|
TestCompression::decompress_to_string(&compressed_data).unwrap()
|
|
}
|
|
|
|
/// Save a str to a specified file as compressed data
|
|
pub fn save_string_as_compressed_data(source: &str, target_file: &Path) {
|
|
let mut compressed_file = OpenOptions::new()
|
|
.write(true)
|
|
.truncate(true) // optional: clear existing contents
|
|
.create(true)
|
|
.open(target_file)
|
|
.expect(&format!("Unable to open target file [{}] for writing", target_file.display()));
|
|
|
|
let compressed_data = TestCompression::compress_string(source);
|
|
|
|
compressed_file
|
|
.write_all(&compressed_data)
|
|
.expect(&format!("Unable to write compressed data to [{}]", target_file.display()));
|
|
}
|
|
|
|
pub fn compress_file<P: AsRef<Path>>(source: P, destination: P) -> io::Result<()> {
|
|
// Read the source file
|
|
let mut read_buffer = String::new();
|
|
let mut read_file = File::open(&source)?;
|
|
read_file.read_to_string(&mut read_buffer)?;
|
|
|
|
// Compress the data
|
|
let compressed_data = TestCompression::compress_string(&read_buffer);
|
|
|
|
// Write the compressed data to the destination file
|
|
let mut write_file = File::create(&destination)?;
|
|
write_file.write_all(&compressed_data)?;
|
|
|
|
Ok(())
|
|
}
|
|
|
|
pub fn compress_string_to_file(text: &str, target: &Path) {
|
|
let compressed_text = TestCompression::compress_string(text);
|
|
let mut writer = File::create(target).expect("Unable to create target file.");
|
|
writer.write_all(&compressed_text).expect("Unable to write compressed data");
|
|
}
|
|
|
|
pub fn decompress_file_to_string(source: &Path) -> String {
|
|
let mut compressed_file = File::open(source).expect("Unable to open source to compress.");
|
|
let mut compressed_data = Vec::new();
|
|
compressed_file.read_to_end(&mut compressed_data).expect("Unable to read compressed data");
|
|
TestCompression::decompress_to_string(&compressed_data).unwrap()
|
|
}
|
|
}
|