I've been using Java's standard library for writing Zip Archive files and, in that case, I do not have to know the files that are going to be stored into a zip file beforehand: I would simply create a new ZipEntry and then write into the ZipFile stream.
On the other hand, LibArchive and ZLib for C++ have to set the to-be-archived file information beforehand: in my case the data that I want to compress comes from an extern stream, and hence I cannot query the size of the stream itself. Is there a library/way to use those libraries such that the initial file information is not required?
EDIT Moreover, the following C++ code with LibArchive produces a valid ZIP file containing a file with no data.
a = archive_write_new();
archive_write_add_filter_gzip(a);
archive_write_set_format_pax_restricted(a);
archive_write_open_filename(a, path.c_str());
entry = archive_entry_new(); // Note 2
archive_entry_set_pathname(entry, entryName.c_str());
archive_entry_set_perm(entry, 0666);
archive_write_header(a, entry);
size_t s = archive_write_data(a, buff, len);
archive_entry_free(entry);
archive_write_close(a);
archive_write_free(a);
Check the sample on the official Zlib site. In sample bytes from source file compressed to the new file until EOF on source. File size of source file is not used.
The only thing you should change in sample to use your custom stream is this line:
strm.avail_in = fread(in, 1, CHUNK, source);
Use your function to get bytes from stream to fill in
buffer.
To handle ZIP archive in Zlib use contrib/minizip. Here is a discussion on how to use it.