Home > Back-end >  How can I convert a ZipWriter to Bytes in Rust?
How can I convert a ZipWriter to Bytes in Rust?

Time:07-16

I want to create a zip from a certain file, then i convert this zip (A buffer, not a file in the disk) to bytes, then to send it by a post body to an api.

Thats the code (simplifyed) just with the essencial, and from now to the next step, how can i convert it into bytes?

pub fn to_zip() {
    let buf: &mut [u8] = &mut [0u8; 65536];
    let w = std::io::Cursor::new(buf);
    let mut zip = zip::ZipWriter::new(w);

    // * Convert the buffer to bytes

    zip.finish().unwrap();
}

Sorry for a maybe confuse code first time with Rust beeing loving it so far!

CodePudding user response:

zip.finish().unwrap() gives you the Cursor that you used to create the ZipWriter. You can then use into_inner to go back to a &mut [u8].

  • Related