So I have data compressed in php in this way:
$compressed = gzcompress($pairs, 9);
When I try to decompress obtained data with rust in this way:
let mut d = flate2::read::GzDecoder::new(compressed_pairs.as_bytes());
let mut s = String::new();
d.read_to_string(&mut s).unwrap();
println!("{}", s);
I get this error - thread 'main' panicked at 'called `Result::unwrap()` on an `Err` value: Custom { kind: InvalidInput, error: "invalid gzip header" }',
As I read here gzcompress is similar to gzencode, but with different header.
Is it possible to decode it in Rust?
I found out here a C library and I thought that I can connect it to my Rust code, but may be there are simpler solution?
My Answer:
To decompress a string that was compressed using PHP's `gzcompress` function, you can use the following Rust code:
rust
use flate2::read::GzDecoder;
use std::io::prelude::*;
use std::io::Cursor;
fn decompress_php_compressed_string(compressed_string: &[u8]) -> String {
let mut decoder = GzDecoder::new(Cursor::new(compressed_string));
let mut decompressed_string = String::new();
decoder.read_to_string(&mut decompressed_string).unwrap();
decompressed_string
}
fn main() {
let compressed_string = vec![/* compressed string bytes here */];
let decompressed_string = decompress_php_compressed_string(&compressed_string);
println!("{}", decompressed_string);
}
Make sure to replace `/* compressed string bytes here */` with the actual bytes of the compressed string that you want to decompress. This code uses the `flate2` crate to decompress the string.
Rate this post
3 of 5 based on 8559 votesComments