我对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++字符串?
发布于 2021-06-08 22:53:48
下面是STL-functor风格的包装器:
#ifndef ZSTD_UTILITY_H
#define ZSTD_UTILITY_H
#include <type_traits>
#include <string>
namespace zstd {
#include <zstd.h>
} // namespace zstd
static constexpr int kMinCLevel = 1;
static constexpr int kMaxCLevel = 22;
static constexpr int kDefaultCLevel = kMinCLevel;
template<typename ChType>
struct ZstdCompression final {
using char_type = typename std::char_traits<ChType>::char_type;
using ContainerType = std::basic_string<char_type>;
inline auto operator()(const char_type* input, const size_t inputSize, const int level = kDefaultCLevel) {
assert(0 < inputSize);
assert(kMinCLevel <= level);
assert(kMaxCLevel >= level);
const size_t required = zstd::ZSTD_compressBound(inputSize);
ContainerType block(required, 0x0);
const size_t actual = zstd::ZSTD_compress(block.data(), block.size(), input, inputSize, level);
assert(!zstd::ZSTD_isError(actual));
block.erase(block.begin() + actual, block.end());
return block;
}
template<typename Container, typename IteratorValueType = typename std::iterator_traits<typename Container::iterator>::value_type>
inline auto operator()(const Container& input, const int level = kDefaultCLevel) -> std::enable_if_t<std::is_same_v<char_type, IteratorValueType>, ContainerType> {
return this->operator()(input.data(), input.size(), level);
}
};
using ZstdCompressor = ZstdCompression<char>;
template<typename ChType>
struct ZstdDeCompression final {
using char_type = typename std::char_traits<ChType>::char_type;
using ContainerType = std::basic_string<char_type>;
inline auto operator()(const char_type* input, const size_t inputSize) {
assert(0 < inputSize);
const size_t size = zstd::ZSTD_getFrameContentSize(input, inputSize);
assert(ZSTD_CONTENTSIZE_ERROR != size);
assert(ZSTD_CONTENTSIZE_UNKNOWN != size);
ContainerType block(size, 0x0);
const size_t actual = zstd::ZSTD_decompress(block.data(), block.size(), input, inputSize);
assert(!zstd::ZSTD_isError(actual));
assert(actual == size);
return block;
}
template<typename Container, typename IteratorValueType = typename std::iterator_traits<typename Container::iterator>::value_type>
inline auto operator()(const Container& input) -> std::enable_if_t<std::is_same_v<char_type, IteratorValueType>, ContainerType> {
return this->operator()(input.data(), input.size());
}
};
using ZstdDeCompressor = ZstdDeCompression<char>;
#endif // ZSTD_UTILITY_H
https://stackoverflow.com/questions/57745122
复制相似问题