1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
#define BOOST_ALL_DYN_LINK

#include <fstream>
#include <iostream>
#include <cstring>
#include <boost/iostreams/filtering_streambuf.hpp>
#include <boost/iostreams/copy.hpp>
#include <boost/iostreams/filter/bzip2.hpp>

namespace io = boost::iostreams;

void bzip2_compresss(const char* src, const char* dst)
{
    std::ofstream dstFile(dst, std::ios_base::binary);
    std::ifstream srcFile(src, std::ios_base::binary);

    io::filtering_streambuf<io::output> out;
    out.push(io::bzip2_compressor());
    out.push(dstFile);
    io::copy(srcFile, out);
}

void bzip2_decompresss(const char* src, const char* dst)
{
    std::ifstream srcFile(src, std::ios_base::binary);
    std::ofstream dstFile(dst, std::ios_base::binary);

    io::filtering_streambuf<io::input> in;
    in.push(io::bzip2_decompressor());
    in.push(srcFile);
    io::copy(in, dstFile);
}

inline bool streq(const char* lhs, const char* rhs)
{
    return std::strcmp(lhs, rhs) == 0;
}

int main(int argc, char** argv)
{
    if (argc != 4 || (!streq(argv[1], "c") && !streq(argv[1], "d")))
    {
        std::cout <<
            "bz2test c input output_bzip2\n"
            "\t Compress mode\n"
            "\n"
            "bz2test d input_bzip2 output\n"
            "\t Decompress mode"
            << std::endl;
        return 1;
    }
    if (argv[1][0] == 'c')
    {
        bzip2_compresss(argv[2], argv[3]);
    }
    else
    {
        bzip2_decompresss(argv[2], argv[3]);
    }
}