データの圧縮と展開
Posted feedbacks - C++
Boost.IostreamsのBZip2関係のライブラリを使いました。ほかにはzlibのものもBoostにあります。ここでは簡単にするため引数にファイル名を取っていますが、実際にはC++のストリームのやり取りで済みます。
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]);
}
}
|


mattsan
#8262()
Rating1/5=0.20
データを圧縮するcompress、展開するdecompressという関数やメソッドなどを書いてください。データはバイト列でもストリームでもそれ以外の形式でもOKです。
圧縮形式は問いませんが、できるだけ一般的なフォーマット(zip,lzhなど)でお願いします。
また、標準以外のライブラリを使う場合には出典の記載をお願いします。
「○○でも実用的な圧縮/展開プログラムがかけるんだぞ!」というのを、ぜひ示してください。
[ reply ]