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);
4

1 回答 1

3

检查官方Zlib 站点上的示例。在源文件中压缩到新文件的示例字节中,直到源上的 EOF。不使用源文件的文件大小。

您应该在示例中更改以使用自定义流的唯一内容是这一行:

strm.avail_in = fread(in, 1, CHUNK, source);

使用您的函数从流中获取字节以填充in缓冲区。

要在 Zlib 中处理 ZIP 存档,请使用contrib/minizip是关于如何使用它的讨论。

于 2016-08-22T16:13:42.453 回答