c++ - 如何使用 Zstd 压缩 C++ 字符串?

标签 c++ string compression zstd

我是 C++ 的新手,我想通过 Zstd 压缩库压缩一个 std:string 对象,但到目前为止我找不到 C++ 示例通过谷歌搜索为此目的编写代码。我找到了示例 C 代码,但它们似乎使用 C 样式字符数组而不是 std:string 对象。

示例 C 代码: https://github.com/facebook/zstd/blob/dev/examples/simple_compression.c

/*
 * Copyright (c) 2016-present, Yann Collet, Facebook, Inc.
 * All rights reserved.
 *
 * This source code is licensed under both the BSD-style license (found in the
 * LICENSE file in the root directory of this source tree) and the GPLv2 (found
 * in the COPYING file in the root directory of this source tree).
 * You may select, at your option, one of the above-listed licenses.
 */

#include <stdio.h>     // printf
#include <stdlib.h>    // free
#include <string.h>    // strlen, strcat, memset
#include <zstd.h>      // presumes zstd library is installed
#include "common.h"    // Helper functions, CHECK(), and CHECK_ZSTD()

static void compress_orDie(const char* fname, const char* oname)
{
    size_t fSize;
    void* const fBuff = mallocAndLoadFile_orDie(fname, &fSize);
    size_t const cBuffSize = ZSTD_compressBound(fSize);
    void* const cBuff = malloc_orDie(cBuffSize);

    /* Compress.
     * If you are doing many compressions, you may want to reuse the context.
     * See the multiple_simple_compression.c example.
     */
    size_t const cSize = ZSTD_compress(cBuff, cBuffSize, fBuff, fSize, 1);
    CHECK_ZSTD(cSize);

    saveFile_orDie(oname, cBuff, cSize);

    /* success */
    printf("%25s : %6u -> %7u - %s \n", fname, (unsigned)fSize, (unsigned)cSize, oname);

    free(fBuff);
    free(cBuff);
}

static char* createOutFilename_orDie(const char* filename)
{
    size_t const inL = strlen(filename);
    size_t const outL = inL + 5;
    void* const outSpace = malloc_orDie(outL);
    memset(outSpace, 0, outL);
    strcat(outSpace, filename);
    strcat(outSpace, ".zst");
    return (char*)outSpace;
}

int main(int argc, const char** argv)
{
    const char* const exeName = argv[0];

    if (argc!=2) {
        printf("wrong arguments\n");
        printf("usage:\n");
        printf("%s FILE\n", exeName);
        return 1;
    }

    const char* const inFilename = argv[1];

    char* const outFilename = createOutFilename_orDie(inFilename);
    compress_orDie(inFilename, outFilename);
    free(outFilename);
    return 0;
}

我的问题是,是否有人可以指导我查看显示如何使用 Zstd 压缩 C++ 字符串的示例代码/片段?

最佳答案

在我看来,最新版本的Boost library (version 1.70.0)通过 Zstd 添加了支持压缩到它的iostreams子模块。我可以管理以下 C++代码片段,但似乎 Boost 的旧版本不支持Zstd压缩(我在 Boost 1.67.0 上使用 Debian 10,它没有 Zstd 支持。)

我现在可以设法组装的代码是这样的(它基于来自 here 的代码):

    #include <iostream>
    #include <algorithm>
    #include <string>

    #include <sstream>
    #include <boost/iostreams/filtering_streambuf.hpp>
    #include <boost/iostreams/copy.hpp>
    #include <boost/iostreams/filter/zstd.hpp>


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

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

            bio::filtering_streambuf<bio::input> out;
            out.push(bio::zstd_compressor(bio::zstd_params(bio::zstd::default_compression)));

            out.push(origin);
            bio::copy(out, compressed);

            return compressed.str();
    }

关于c++ - 如何使用 Zstd 压缩 C++ 字符串?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/57745122/

相关文章:

c++ - 用于分配/释放 I/O 缓冲区的现代 C++ 习惯用法

c++ - 类多态性和相等运算符

string - 查找给定句子中出现的单词

hadoop - Hadoop区 block 压缩

c++ - 尝试实时编码和传输原始视频时,建议使用哪种视频/图像编码格式?

c++ - 依赖型自动扣除

c++ - 如何使用 std::ifstream 读取具有宽字符串路径的二进制文件

Python str.format 字符串以固定间距开始

java - 读取文本文件时出现问题

C# - 减小图像大小的最佳方法是什么(用于通过 tcp 进行流式传输)