亚洲激情专区-91九色丨porny丨老师-久久久久久久女国产乱让韩-国产精品午夜小视频观看

溫馨提示×

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
永年县| 株洲市| 沂南县| 石首市| 丽水市| 西吉县| 卫辉市| 扬中市| 九龙县| 油尖旺区| 信阳市| 阜康市| 乐陵市| 中西区| 唐河县| 茌平县| 庆城县| 浪卡子县| 通河县| 岚皋县| 盈江县| 汾阳市| 肃北| 美姑县| 竹溪县| 汾西县| 安新县| 勐海县| 怀化市| 金门县| 芦山县| 桂平市| 满洲里市| 新疆| 巴林右旗| 开阳县| 洪洞县| 洪湖市| 阳朔县| 昌都县| 岫岩|