91超碰碰碰碰久久久久久综合_超碰av人澡人澡人澡人澡人掠_国产黄大片在线观看画质优化_txt小说免费全本

溫馨提示×

C++中gzip壓縮的流操作有哪些

c++
小樊
81
2024-08-11 11:47:38
欄目: 編程語言

在C++中,可以使用zlib庫來進行gzip壓縮和解壓縮的操作。以下是使用zlib庫進行gzip壓縮和解壓縮的流操作:

  1. 壓縮數據流:
#include <zlib.h>

std::string compressData(const std::string& data)
{
    z_stream strm;
    strm.zalloc = Z_NULL;
    strm.zfree = Z_NULL;
    strm.opaque = Z_NULL;
    strm.next_in = (Bytef*)data.data();
    strm.avail_in = data.size();

    if (deflateInit2(&strm, Z_DEFAULT_COMPRESSION, Z_DEFLATED, 15 | 16, 8, Z_DEFAULT_STRATEGY) != Z_OK)
    {
        return "";
    }

    std::string compressedData;
    char out[1024];

    do
    {
        strm.next_out = (Bytef*)out;
        strm.avail_out = sizeof(out);

        if (deflate(&strm, Z_FINISH) == Z_STREAM_ERROR)
        {
            deflateEnd(&strm);
            return "";
        }

        compressedData.append(out, sizeof(out) - strm.avail_out);
    } while (strm.avail_out == 0);

    deflateEnd(&strm);

    return compressedData;
}

  1. 解壓縮數據流:
#include <zlib.h>

std::string decompressData(const std::string& compressedData)
{
    z_stream strm;
    strm.zalloc = Z_NULL;
    strm.zfree = Z_NULL;
    strm.opaque = Z_NULL;
    strm.next_in = (Bytef*)compressedData.data();
    strm.avail_in = compressedData.size();

    if (inflateInit2(&strm, 15 | 16) != Z_OK)
    {
        return "";
    }

    std::string decompressedData;
    char out[1024];

    do
    {
        strm.next_out = (Bytef*)out;
        strm.avail_out = sizeof(out);

        if (inflate(&strm, Z_NO_FLUSH) == Z_STREAM_ERROR)
        {
            inflateEnd(&strm);
            return "";
        }

        decompressedData.append(out, sizeof(out) - strm.avail_out);
    } while (strm.avail_out == 0);

    inflateEnd(&strm);

    return decompressedData;
}

0
乌恰县| 辽阳市| 瑞安市| 娄底市| 准格尔旗| 孝义市| 彝良县| 顺昌县| 本溪| 咸丰县| 舟山市| 延长县| 鹤山市| 原平市| 游戏| 崇义县| 茂名市| 遵义市| 太谷县| 嘉荫县| 秦安县| 吉木乃县| 苏尼特左旗| 娄烦县| 孝昌县| 南城县| 永康市| 东辽县| 河间市| 托里县| 高阳县| 贡嘎县| 阿瓦提县| 耿马| 舞阳县| 河北区| 阳信县| 宁南县| 平果县| 贵溪市| 大关县|