问题
i'm having trouble packing a bunch of files into one archive. the boost docs are very limited on this topic and i've searched the web for several hours now, but i can't find a solution.
What i have so far:
boost::filesystem::ofstream ofsArchive("some.zip");
boost::iostreams::filtering_ostreambuf outFilter;
boost::iostreams::zlib_params zparam(boost::iostreams::zlib::default_compression);
try
{
// set up the filter
outFilter.strict_sync();
outFilter.push(boost::iostreams::zlib_compressor(zparam));
outFilter.push(ofsArchive);
for(each object of some kind)
{
// create a binary serialized file
boost::filesystem::ofstream ofs(filename, std::ios_base::binary);
boost::archive::binary_oarchive bin_oa( ofs );
bin_oa << crazyObject;
// here's where i'm stuck. how to add multiple files to the "some.zip"?
boost::iostreams::copy(ofs, outputArchive);
}
}
catch(boost::iostreams::zlib_error& e){...}
how do i add the files to the zip archive? the method provided obviously doesn't work, i just can't find anything on the subject in the docs or header files
回答1:
zlib does not implement the Zip file format, it just implements the stream compression used within Zip (see the zlib FAQ). To my knowledge (which I should warn you is by no means total), Boost does not include functionality to read or write Zip archives. There are libraries that do provide that functionality of course, for example, zziplib (note: the site appears to be down at the moment).
Edit: Apparently, zziplib actually can't write Zip files, it can only read them. Still, I'm sure a little googling would turn up a library capable of writing the format.
回答2:
I know the post is a bit old, but for people like me who see it years after.
There are a bunch of free librairies to write and read zip files in C (usable in C++ of course) :
- infoZip (last version seems to be really complete and safe, though it dates back in 2008);
- libzip, much more recent, last version being only two months old.
来源:https://stackoverflow.com/questions/4961155/boostiostream-zlib-compressing-multiple-files-into-one-archive