aboutsummaryrefslogtreecommitdiff
path: root/src/utils.rs
blob: 925669cd8af3d59935f95581acab230dd38e0c5a (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
use flate2::write::GzEncoder;
use flate2::Compression;
use std::fs::File;
use std::io::{self, Read, Write};

pub fn encode_gzip_string(input: &str) -> std::io::Result<Vec<u8>> {
    // Create a buffer to hold the compressed data
    let mut encoder = GzEncoder::new(Vec::new(), Compression::default());

    // Write the input string into the encoder
    encoder.write_all(input.as_bytes())?;

    // Finish the encoding process and get the compressed data
    let compressed_data = encoder.finish()?;

    Ok(compressed_data)
}

pub fn save_bytes_to_file(bytes: &[u8], file_path: &str) -> io::Result<()> {
    let mut file = File::create(file_path)?;
    file.write_all(bytes)?;
    Ok(())
}

pub fn read_file_as_bytes(path: &str) -> io::Result<Vec<u8>> {
    // Open the file in read-only mode
    let mut file = File::open(path)?;

    // Create a buffer to hold the file contents
    let mut buffer = Vec::new();

    // Read the file contents into the buffer
    file.read_to_end(&mut buffer)?;

    // Return the buffer
    Ok(buffer)
}