|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
#include <iostream> |
|
#include <fstream> |
|
#include <sstream> |
|
#include <string> |
|
#include <cstdlib> |
|
#include <dlib/compress_stream.h> |
|
#include <dlib/base64.h> |
|
|
|
|
|
using namespace std; |
|
using namespace dlib; |
|
|
|
int main(int argc, char** argv) |
|
{ |
|
if (argc != 2) |
|
{ |
|
cout << "You must give a file name as the argument to this program.\n" << endl; |
|
cout << "This program reads in a file from the disk and compresses\n" |
|
<< "it in an in memory buffer and then converts that buffer \n" |
|
<< "into base64 text. The final step is to output to the screen\n" |
|
<< "some C++ code that contains this base64 encoded text and can\n" |
|
<< "decompress it back into its original form.\n" << endl; |
|
|
|
return EXIT_FAILURE; |
|
} |
|
|
|
|
|
ifstream fin(argv[1], ios::binary); |
|
if (!fin) { |
|
cout << "can't open file " << argv[1] << endl; |
|
return EXIT_FAILURE; |
|
} |
|
|
|
ostringstream sout; |
|
istringstream sin; |
|
|
|
|
|
base64 base64_coder; |
|
|
|
compress_stream::kernel_1ea compressor; |
|
|
|
|
|
compressor.compress(fin,sout); |
|
sin.str(sout.str()); |
|
sout.clear(); |
|
sout.str(""); |
|
|
|
|
|
base64_coder.encode(sin,sout); |
|
|
|
sin.clear(); |
|
sin.str(sout.str()); |
|
sout.str(""); |
|
|
|
|
|
|
|
|
|
sout << "#include <sstream>\n"; |
|
sout << "#include <dlib/compress_stream.h>\n"; |
|
sout << "#include <dlib/base64.h>\n"; |
|
sout << "\n"; |
|
sout << "// This function returns the contents of the file '" << argv[1] << "'\n"; |
|
sout << "const std::string get_decoded_string()\n"; |
|
sout << "{\n"; |
|
sout << " dlib::base64 base64_coder;\n"; |
|
sout << " dlib::compress_stream::kernel_1ea compressor;\n"; |
|
sout << " std::ostringstream sout;\n"; |
|
sout << " std::istringstream sin;\n\n"; |
|
|
|
|
|
sout << " // The base64 encoded data from the file '" << argv[1] << "' we want to decode and return.\n"; |
|
string temp; |
|
getline(sin,temp); |
|
while (sin && temp.size() > 0) |
|
{ |
|
sout << " sout << \"" << temp << "\";\n"; |
|
getline(sin,temp); |
|
} |
|
|
|
sout << "\n"; |
|
sout << " // Put the data into the istream sin\n"; |
|
sout << " sin.str(sout.str());\n"; |
|
sout << " sout.str(\"\");\n\n"; |
|
sout << " // Decode the base64 text into its compressed binary form\n"; |
|
sout << " base64_coder.decode(sin,sout);\n"; |
|
sout << " sin.clear();\n"; |
|
sout << " sin.str(sout.str());\n"; |
|
sout << " sout.str(\"\");\n\n"; |
|
sout << " // Decompress the data into its original form\n"; |
|
sout << " compressor.decompress(sin,sout);\n\n"; |
|
sout << " // Return the decoded and decompressed data\n"; |
|
sout << " return sout.str();\n"; |
|
sout << "}\n"; |
|
|
|
|
|
|
|
cout << sout.str() << endl; |
|
} |
|
|
|
|