Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Example of Boost IOStream to create a zip file?

Tags:

c++

boost

I've been looking for a good, portable way to create a zip file in C++ and been coming up short. I've read in various places that its' possible to use the Boost IOstream library, but I can't find any source code or even documentation on it in the reference:

http://www.boost.org/doc/libs/1_48_0/libs/iostreams/doc/index.html

Does anybody have a good reference? I've done a whole lot of Googling and not come up with much.

like image 300
Darkenor Avatar asked Feb 08 '12 19:02

Darkenor


3 Answers

I do not think boost::iostreams can open a zip file. See Unziping a zip file with boost and Visual C++ 2005.

boost::iostreams can be used to compress streams or single files using zlib, gzip or bzip2. You may find some examples here:

  • zlib
  • gzip
  • bzip2

However, it can not read the directory information inside a zip file.

On the other hand, you need to compile boost using third party libraries: zlib and bzip2. See the installation information.

like image 68
J. Calleja Avatar answered Sep 19 '22 12:09

J. Calleja


I wrote a simple zip file maker that allows use with iostreams. It's included in the partio library https://github.com/wdas/partio/blob/master/src/lib/io/ZIP.h https://github.com/wdas/partio/blob/master/src/lib/io/ZIP.cpp

For example you can create a zip file with two files by doing

ZipFileWriter zip("foo.zip");
std::ostream* o = zip.Add_File("test.txt");
*o << "look a zip file" << std::endl;
delete o;
std::ostream* o2 = zip.Add_File("test2.txt");
*o2 << "look another file" << std::endl;
delete o2;

And then could read a file by doing

ZipFileReader zip("foo.zip");
std::istream* i = zip.Get_File("test.txt");
std::string foo;
*i >> foo;
std::cout << foo << std::endl;
delete i;
like image 22
aselle Avatar answered Sep 16 '22 12:09

aselle


Maybe it's not a zip file, but rather a compressed file, if it otherwise can help with your intention. Actually, I tested this:

#include <fstream>
#include <sstream>
#include <iostream>
#include <boost/iostreams/filtering_streambuf.hpp>
#include <boost/iostreams/copy.hpp>
#include <boost/iostreams/filter/zlib.hpp>

TEST(MyTests, SaveZipFile)
{
    using namespace std;

    ofstream file("a.z", ios_base::out | ios_base::binary);
    boost::iostreams::filtering_streambuf<boost::iostreams::output> out;
    out.push(boost::iostreams::zlib_compressor());
    out.push(file);

    stringstream sstr{"nihao"};


    boost::iostreams::copy(sstr, out);
}

TEST(MyTests, OpenZipFile)
{
    using namespace std;
    ifstream file("a.z", ios_base::in | ios_base::binary);
    boost::iostreams::filtering_streambuf<boost::iostreams::input> in;
    in.push(boost::iostreams::zlib_decompressor());
    in.push(file);

    stringstream sstr;
    boost::iostreams::copy(in, sstr);
    cout << sstr.str() << endl;
    ASSERT_EQ(sstr.str(), string("nihao"));
}
like image 45
Michaelzh Avatar answered Sep 19 '22 12:09

Michaelzh