rust-lang / flate2-rs

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

why GzDecoder can't read stream correct #399

Closed jokemanfire closed 6 months ago

jokemanfire commented 6 months ago

When I use GzDecoder unzip file ,but the file hash is not correct!. I try to implement two method like this 1、

 let mut dst_file = OpenOptions::new().create(true).append(true).open(dst)?;
    let src_file = File::open(src)?;
    let mut decompress_stream = GzDecoder::new(src_file);

     let mut data = Vec::new();
    decompress_stream.read_to_end(&mut data).unwrap();
    dst_file.write_all(&data)?;

2、

let mut dst_file = OpenOptions::new().create(true).append(true).open(dst)?;
    let src_file = File::open(src)?;
    let mut decompress_stream = GzDecoder::new(src_file);

    let mut buf = vec![0; 1024];
    loop {
        let n = decompress_stream.read(&mut buf).unwrap() ;
        if n == 0 {
            break;
        }
        dst_file.write_all(&buf)?;

    }

but the method 2 is not correct , sha256 is not same with method1 . Is some problem in method 2 ? if the file is too large ,the memory is not enough , so the method1 will panic, that's whay I want to use method2 .

jokemanfire commented 6 months ago

When I use GzDecoder unzip file ,but the file hash is not correct!. I try to implement two method like this 1、

 let mut dst_file = OpenOptions::new().create(true).append(true).open(dst)?;
    let src_file = File::open(src)?;
    let mut decompress_stream = GzDecoder::new(src_file);

     let mut data = Vec::new();
    decompress_stream.read_to_end(&mut data).unwrap();
    dst_file.write_all(&data)?;

2、

let mut dst_file = OpenOptions::new().create(true).append(true).open(dst)?;
    let src_file = File::open(src)?;
    let mut decompress_stream = GzDecoder::new(src_file);

    let mut buf = vec![0; 1024];
    loop {
        let n = decompress_stream.read(&mut buf).unwrap() ;
        if n == 0 {
            break;
        }
        dst_file.write_all(&buf)?;

    }

but the method 2 is not correct , sha256 is not same with method1 . Is some problem in method 2 ? if the file is too large ,the memory is not enough , so the method1 will panic, that's whay I want to use method2 .

I find the data may be not full 1024bytes, I modify like this , and it can pass


let mut buf = vec![0u8; 1024];
    loop {

        let n = decompress_stream.read(&mut buf).unwrap() ;
        if n == 0 {
            break;
        }
        // println!("buf size : {}  {}",n,buf.len() );
        dst_file.write_all(&buf[0..n])?;

    }