简体   繁体   中英

zlib Compress without writing to disk

I am completely new to zLib and I am confused on how to use it. This is what I am trying to do using c++, but without writing to disk, so using a buffer or something:

Take a vector of integers stored in an std::vector print it all out in a text file with spaces in between each element. Compress it using zLib then save the size of that compressed file in bytes to an integer variable later used for computation and whatever.

Any help is appreciated, thanks :)

zlib has compress and uncompress functions that take pointers to input and output buffers (along with a maximum size for each).

In your case, I'd start with a std::stringstream to turn the integers into text form (with spaces in between). Then use that stream's .str() to retrieve the content as a string. Use the string's .c_str() to retrieve a pointer to the data it contains, which you can then pass to compress .

For the destination, it takes a pointer to the buffer and a pointer to a long int. It writes the compressed size to that long int, so that's the value you seem to want to save for later computation and such.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM