rust-lang / flate2-rs

DEFLATE, gzip, and zlib bindings for Rust
https://docs.rs/flate2
Apache License 2.0
929 stars 163 forks source link

Provide Seek / offset pointer in GzDecoder #310

Open gwbres opened 2 years ago

gwbres commented 2 years ago

Hello,

I would like to enhance the GzDecoder with the std::io::Seek trait and std::io::rewind() capacities, to rewind the file although we might already have uncompressed some data. Is that even doable ?

My first basic attempt here, I implement the Seek trait. Let's say I uncompressed a first line, then call Seek(pos=0) to rewind, the first line is forever lost and cannot be uncompressed a second time.

My second attempt here I try to implement something that would rewind right after the .gz header (that we would never read again) and try to reset the FSM at the beginning of the file Body. Same results, it's like none of the things I do have an impact. I guess I'm missing some important attributes to reset, but I'm having a hard time understanding the code.

Any advice ?

noonchen commented 2 years ago

I'd like to request this feature as well.

zlib.h already contains related functions:

https://github.com/madler/zlib/blob/21767c654d31d2dccdde4330529775c6c5fd5389/zlib.h#L1849-L1863

milesj commented 1 year ago

Would also like to see this.

hderms commented 1 year ago

I would also like to see this! Would be a game changer

IriaSomobu commented 11 months ago

To anyone looking for a solution: in case you really need Seek and you dont mind RAM overhead -- you can use SeekableReader. It is Read + Seek wrapper around Read that caches read data within internal buffer.