当前位置:网站首页>C language compressed string is saved to binary file, and the compressed string is read from binary file and decompressed.

C language compressed string is saved to binary file, and the compressed string is read from binary file and decompressed.

2022-06-13 02:03:00 Xeon_ CC

  1. stay vcpkg Under the environment of , Confirm that... Is installed boost library

  2. stay Debug,x64 Additional dependencies in the environment :
     Insert picture description here
     Insert picture description here

  3. Switch to Release,x64 Environmental Science , And attach dependencies :
     Insert picture description here
    After attaching the dependency, click apply , The program will not fail to compile when it runs .

  4. Write code :

#include <iostream>
#include <sstream>
#include <boost/iostreams/filtering_streambuf.hpp>
#include <boost/iostreams/copy.hpp>
#include <boost/iostreams/filter/gzip.hpp>
#include <stdio.h>
#include <string>
using namespace std;

std::string compress(const std::string& data) {
    
    namespace bio = boost::iostreams;

    std::stringstream compressed;
    std::stringstream origin(data);

    bio::filtering_streambuf<bio::input> out;
    out.push(bio::gzip_compressor(bio::gzip_params(bio::gzip::best_speed)));
    out.push(origin);
    bio::copy(out, compressed);

    return compressed.str();
}

std::string decompress(const std::string& data)
{
    
    namespace bio = boost::iostreams;

    std::stringstream compressed(data);
    std::stringstream decompressed;

    bio::filtering_streambuf<bio::input> out;
    out.push(bio::gzip_decompressor());
    out.push(compressed);
    bio::copy(out, decompressed);

    return decompressed.str();
}

int main() {
    
    //  First in a.bin File manually enter some strings 
   // Read in binary form a.bin file 
    FILE* raf = fopen("../static/a.bin", "rb");
    char bufa;
    string astr = "";
    while (true) {
    
        bufa = fgetc(raf);
        if (feof(raf) != 1) {
    
            astr += bufa;
        }
        else {
    
            break;
        }
    }
    fclose(raf);

    // Write... In binary form b.bin file , And will be taken from a.bin The string read from the file is compressed and written to b.bin file 
    FILE* wbf = fopen("../static/b.bin", "wb");
    string bstr = compress(astr);
    for (int i = 0; i < bstr.size(); i++) {
    
        fputc(bstr[i], wbf);
    }
    fclose(wbf);

    // Read in binary form b.bin file 
    FILE* rbf = fopen("../static/b.bin", "rb");
    string readbstr = "";
    char bufb;
    while (true) {
    
        bufb = fgetc(rbf);
        if (feof(rbf) != 1) {
    
            readbstr += bufb;
        }
        else {
    
            break;
        }
    }
    fclose(rbf);


    // Check from bin File read string and use compress Whether the string compressed by the function is the same .
    // If binary reading is not used , If you read directly , The two may be different .
    if (bstr == readbstr) {
    
        cout << "same....." << endl;
    }

    // Read the compressed binary file and extract it .
    cout << " Read compressed string ::" << readbstr << endl;
    cout << "decompressing..." << endl;
    string decmpstr = decompress(readbstr);
    cout << " Decompressed string ::" << decmpstr << endl;
    return 0;
}
  1. Running results
     Insert picture description here
    see a.bin file , This is what I casually input from the keyboard :
     Insert picture description here
    see b.bin file , This is the compressed content , The output of the terminal is garbled
     Insert picture description here
原网站

版权声明
本文为[Xeon_ CC]所创,转载请带上原文链接,感谢
https://yzsam.com/2022/164/202206130151294254.html