-
Notifications
You must be signed in to change notification settings - Fork 162
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Corrupt result of flate2::read::GzEncoder #158
Comments
You probably want to use
But if you want to read into an array using
|
Thanks for answering in such a sort time. I used I think that your The problem is not reading less bytes because my test example has less than 100 bytes. I wonder if after reading all bytes is necessary to call a function to finish the gzip or something similar. Do you guess what I'm doing wrong? |
I have compared the results of using Using
Using
The difference between the results are the last bytes Maybe this finding can be helpful. |
For streaming, you should use |
Ok. I had started using Thanks. |
@vitalyd - How would you proceed to read out only the bytes written to the |
I solved this by switching to the |
What an interesting ride. I've been using a few other de/compression libs, and they always output the total bytes read into the buffer, so when I ran into this issue; when using a buffer with Maybe I was being too obtuse, (it is late), but Anyway, maybe this deserves a laugh? I got my desired output... 😅 Is there a better way? // compute checksum
let mut crc = Crc::new();
crc.update(&data);
// Encode
let mut encoder = GzEncoder::new(data, Compression::new(level));
let n_bytes = encoder.read(slice)?;
// insert checksum as bytes into output
let mut checksum_bytes = crc.sum().to_le_bytes();
slice[n_bytes..n_bytes + 4].swap_with_slice(&mut checksum_bytes);
// insert data len as bytes into output
let mut data_len_bytes = (data.len() as u32).to_le_bytes();
slice[n_bytes + 4..n_bytes + 8].swap_with_slice(&mut data_len_bytes);
// Ka-pow, total bytes affected output
Ok(n_bytes + checksum_bytes.len() + data_len_bytes.len()) |
I am using
flate2::read::GzEncoder
to gzip a stream. However, when I try to ungzip the result I get anErr value: Custom { kind: InvalidInput, error: StringError("corrupt gzip stream does not have a matching checksum") }
.I'm doing probably something wrong. This is the code that I'm using:
HELLO
has the following value:"Hello world!"
, whose bytes are:[72, 101, 108, 108, 111, 32, 119, 111, 114, 108, 100, 33]
.&response.body_bytes().unwrap()[..]
has the following value (these are the bytes of the corrupted gzip):[31, 139, 8, 0, 0, 0, 0, 0, 0, 255, 1, 12, 0, 243, 255, 72, 101, 108, 108, 111, 32, 119, 111, 114, 108, 100, 33, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0]
.I have tried to ungzipping a custom vector of bytes without the final zeroes and I get the same error.
Am I doing something wrong? Something is missing?
Thanks.
The text was updated successfully, but these errors were encountered: