0

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);
jackb
  • 695
  • 8
  • 26
  • Questions seeking recommendations quickly go south. Please consult the [help](http://stackoverflow.com/help) section on dos and don'ts here on SO. And that mention of JAVA and it's libs is of no significance to the c++ related question. –  Aug 22 '16 at 15:59
  • This is a typical problem of this very stackexchange community… – jackb Aug 22 '16 at 16:01
  • Not really. We all went through that. Happened to me too when I was a beginner user. Once you learn the SO ways it's a breeze. –  Aug 22 '16 at 16:02
  • Actually, I'm stating that my intended operation is possible. Often people say "Why, if the library doesn't do it, then you cannot do it". So telling that in Java I can do it, it has its sense, saying that this is a possible thing to do. – jackb Aug 22 '16 at 16:02
  • You are wrong, see "Size, file type, and pathname are all required attributes here. " https://github.com/libarchive/libarchive/wiki/Examples Please provide a reply only if you know something about it and provide counterexamples. – jackb Aug 22 '16 at 16:03
  • Zlib does have a stream interface. https://github.com/madler/zlib/blob/master/zlib.h search for stream. – West Aug 22 '16 at 16:16
  • Ok, If I get that correctly, you suggest a similar solution as Nikita. Is it possible to do the same things even for archive zip files? – jackb Aug 22 '16 at 16:20
  • If you want to reply to or ask someone in particular, tag them like this: @jackb, otherwise they won't ever know you are trying to reach them. You may want to look [here](https://github.com/libarchive/libarchive/wiki/FormatZip#Zip_and_Streaming_Writes) or google "libarchive streaming compression", in particular [this post](https://groups.google.com/d/msg/libarchive-discuss/41t7U5dSIe4/kBnfrKDeK-cJ). Good luck. – n. m. could be an AI Aug 22 '16 at 16:26
  • Unfortunately *archive_write_open_file* is a deprecated function and, by doing so, the ZIP file is not valid. I tried to do something similar (I edit the code above), but after extracting the zip file I have no information as a result. – jackb Aug 22 '16 at 16:33
  • No, archive_write_open_file doesn't by itself invalidate the file, the reason is different. [Here](http://pastebin.com/UhuHgtv3)'s a tested working example. Anyway, libarchive doesn't look like a particularly good library, or one that fits your purpose well. It's an archiving library with compression capabilities, not a compression library. All usual compression libraries, including libz, liblzma, libbz2, have streaming. – n. m. could be an AI Aug 22 '16 at 19:29

1 Answers1

3

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.

Community
  • 1
  • 1
Nikita
  • 6,270
  • 2
  • 24
  • 37
  • Thanks. By the way, in this way I am compressing only one file per time, and not doing a ZIP archive. Is it possible to do the same thing for ZIP archives? – jackb Aug 22 '16 at 16:17
  • 1
    @jackb Zlib itself cannot handle ZIP archives, but there is an extension in [contrib\minizip](https://github.com/madler/zlib/tree/master/contrib/minizip). – Nikita Aug 22 '16 at 16:24
  • Thanks, I'll try to check that. I edit my question clarifying that I'm referring to zip archives. – jackb Aug 22 '16 at 16:26
  • @jackb I've updated my answer. Check the linked SO discussion on minizip usage. – Nikita Aug 22 '16 at 16:37