| 1 |
|
|---|
| 2 |
|
|---|
| 3 | #include <iostream>
|
|---|
| 4 | #include <sstream>
|
|---|
| 5 | #include <fstream>
|
|---|
| 6 | #include <boost/iostreams/filtering_streambuf.hpp>
|
|---|
| 7 | #include <boost/iostreams/copy.hpp>
|
|---|
| 8 | #include <boost/iostreams/filter/zlib.hpp>
|
|---|
| 9 | #include <boost/lexical_cast.hpp>
|
|---|
| 10 |
|
|---|
| 11 | using namespace std;
|
|---|
| 12 | using namespace boost;
|
|---|
| 13 | using namespace boost::iostreams;
|
|---|
| 14 |
|
|---|
| 15 | bool compress(const std::string& source, std::string& dest, size_t destOffset)
|
|---|
| 16 | {
|
|---|
| 17 | ostringstream _sink;
|
|---|
| 18 | istringstream _source(source);
|
|---|
| 19 |
|
|---|
| 20 | filtering_istreambuf in;
|
|---|
| 21 | zlib_compressor compressor;
|
|---|
| 22 | in.push(compressor);
|
|---|
| 23 | in.push(_source);
|
|---|
| 24 | copy(in, _sink);
|
|---|
| 25 |
|
|---|
| 26 | dest.insert(destOffset, _sink.str());
|
|---|
| 27 |
|
|---|
| 28 | return compressor.unconsumed_input().empty();
|
|---|
| 29 | }
|
|---|
| 30 |
|
|---|
| 31 | bool uncompress(const string& source, string& dest, size_t destOffset)
|
|---|
| 32 | {
|
|---|
| 33 | ostringstream _sink;
|
|---|
| 34 | istringstream _source(source);
|
|---|
| 35 |
|
|---|
| 36 | filtering_istreambuf in;
|
|---|
| 37 | zlib_decompressor decompressor;
|
|---|
| 38 | in.push(decompressor);
|
|---|
| 39 | in.push(_source);
|
|---|
| 40 | copy(in, _sink);
|
|---|
| 41 |
|
|---|
| 42 | dest.insert(destOffset, _sink.str());
|
|---|
| 43 |
|
|---|
| 44 | return decompressor.unconsumed_input().empty();
|
|---|
| 45 | }
|
|---|
| 46 |
|
|---|
| 47 | void SimpleTestCompression(const std::string& OriginalData)
|
|---|
| 48 | {
|
|---|
| 49 |
|
|---|
| 50 | std::string CompressionDest;
|
|---|
| 51 | compress(OriginalData,CompressionDest,0);
|
|---|
| 52 | std::string DecompressionDest;
|
|---|
| 53 | uncompress(CompressionDest,DecompressionDest,0);
|
|---|
| 54 | }
|
|---|
| 55 |
|
|---|
| 56 | std::string GetBadString()
|
|---|
| 57 | {
|
|---|
| 58 | std::string filename("C:\\Users\\gta\\Desktop\\short_magic.txt");
|
|---|
| 59 |
|
|---|
| 60 | std::ifstream in(filename.c_str());
|
|---|
| 61 | std::string tmp;
|
|---|
| 62 | std::string buffer="";
|
|---|
| 63 |
|
|---|
| 64 | while (!in.eof( )) {
|
|---|
| 65 | getline(in, tmp, '\n');
|
|---|
| 66 | char c=(char)boost::lexical_cast<int>(tmp);
|
|---|
| 67 | buffer+=c;
|
|---|
| 68 | tmp.clear();
|
|---|
| 69 | }
|
|---|
| 70 |
|
|---|
| 71 | return buffer;
|
|---|
| 72 | }
|
|---|
| 73 |
|
|---|
| 74 | int main(int argc, char* argv[])
|
|---|
| 75 | {
|
|---|
| 76 | SimpleTestCompression("Hello123");
|
|---|
| 77 | try {
|
|---|
| 78 | SimpleTestCompression(GetBadString());
|
|---|
| 79 | }
|
|---|
| 80 | catch (const boost::iostreams::zlib_error& e)
|
|---|
| 81 | {
|
|---|
| 82 | std::cout << "What: " << e.what() << " Error: " << e.error();
|
|---|
| 83 | }
|
|---|
| 84 | return 0;
|
|---|
| 85 | }
|
|---|
| 86 |
|
|---|