简体   繁体   中英

Append to compressed file using zlib

Looking around I have found the question being asked, but not great answers. If this is a stackoverflow duplicate (sorry!)

My goal is to have a zlib compressed file that I append to using C/C++ at different intervals (such as a log file). Due to buffer size constraints I was hoping to avoid having to keep the entire file in memory for appending new items.

It sounds like you are trying to keep something like a compressed log, appending small amounts of data each time. For that you can look at gzlog.h and gzlog.c for an example of how to do this.

You can also look at gzappend , which appends data to a gzip file.

These are all easily adaptable to a zlib stream.

Mark Adler's answer was very close to what I needed, but due to already being entrenched in the zlib library and on an embedded device with limited resources I was/am stuck.

I ended up simply appending a delimiter to each section of data (ex: @@delimiter@@) and once ready to read the finished file, (different application) it seeks these sections and creates an array object of the compressed sections that are then individually decompressed.

I am still marking Adler's answer as correct, as it was useful info that will be of more help to other programmers.

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