Gzip字符串无法连接到字符串

时间:2019-05-06 13:20:33

标签: html c++ linux http gzip

我是C ++的新手,我一直在尝试寻找该问题的答案,但几乎找不到答案。我试图在gzip压缩中使用zlib压缩html字符串体,压缩已成功完成但是当我尝试将其串联到另一个字符串时,它根本不会串联,从而使主体变为空白,这里是压缩代码:

void compress_message(){
            z_stream zs;
            char outbuffer[32768];
            long strlen = content.size();
            int ret;
            string outstring;

            memset(&zs, 0, sizeof(zs));

            if (deflateInit2(&zs, 
                     Z_BEST_COMPRESSION,
                     Z_DEFLATED,
                     MOD_GZIP_ZLIB_WINDOWSIZE + 16, 
                     MOD_GZIP_ZLIB_CFACTOR,
                     Z_DEFAULT_STRATEGY) != Z_OK
            ) {
                throw(std::runtime_error("deflateInit2 failed while compressing."));
            }

            content = "";

            zs.next_in = (Bytef*) content.data();
            zs.avail_in = strlen;

            do {
                //cout << "compressing..." << endl;
                zs.next_out = reinterpret_cast<Bytef*>(outbuffer);
                zs.avail_out = sizeof(outbuffer);

                ret = deflate(&zs, Z_FINISH);

                if (content.size() < zs.total_out) {
                    // append the block to the output string
                    content.append(outbuffer,
                                     zs.total_out - content.size());
                }
            } while (ret == Z_OK);

            deflateEnd(&zs);
            //cout << "original: " << content << endl;
            if (ret != Z_STREAM_END) {          // an error occurred that was not EOF
                ostringstream oss;
                oss << "Exception during zlib compression: (" << ret << ") " << zs.msg;
                throw(std::runtime_error(oss.str()));
            }
        }

base64转换功能:

string tobase64(string data) {
    string code64,
        output = "";
    int i = 0,
        j = 0;
    unsigned int slen = data.size();
    const char* bytes_to_encode;
    code64 = "ABCDEFGHIJKLMNOPQRSTUVWXYZ"
             "abcdefghijklmnopqrstuvwxyz"
             "0123456789+/";
    unsigned char char_array_3[3],
        char_array_4[4];

    bytes_to_encode = data.c_str();

    while(slen--){
        char_array_3[i++] = bytes_to_encode[j++];

        if(3 == i){
            char_array_4[0] = (char_array_3[0] & 0xfc) >> 2;
            char_array_4[1] = ((char_array_3[0] & 0x03) << 4) + ((char_array_3[1] & 0xf0) >> 4);
            char_array_4[2] = ((char_array_3[1] & 0x0f) << 2) + ((char_array_3[2] & 0xc0) >> 6);
            char_array_4[3] = char_array_3[2] & 0x3f;

            for(i = 0; i < 4; i++){
                output += code64[char_array_4[i]];
            }

            i = 0;
        }
    }

    if (i)
    {
        for(j = i; j < 3; j++)
            char_array_3[j] = '\0';

        char_array_4[0] = ( char_array_3[0] & 0xfc) >> 2;
        char_array_4[1] = ((char_array_3[0] & 0x03) << 4) + ((char_array_3[1] & 0xf0) >> 4);
        char_array_4[2] = ((char_array_3[1] & 0x0f) << 2) + ((char_array_3[2] & 0xc0) >> 6);

        for (j = 0; (j < i + 1); j++)
          output += code64[char_array_4[j]];

        while((i++ < 3))
          output += '=';
    }

    return output;
}

该字符串已成功压缩,但是当我将其连接到字符串响应时,例如:

string final_content = ("HTTP/1.1 200 OK\r\n"+headers+"\r\n\r\n");
content = tobase64(content);
final_content += content;

我也尝试过

final_content.append(content);

它们都具有相同的结果,当仅显示以下内容时,主体永远不会串联final_content:

HTTP/1.1 200 OK
...
Content-type: text/html

并且从来没有包含压缩字符串,因此一直在尝试在线查找答案,但找不到答案,请帮忙。我正在ubuntu 18.04上工作

0 个答案:

没有答案