#include "stdafx.h"
#include <string>
#include "lzma.h"
#pragma comment(lib, "lzma")
std::string CompressWithLzma(const std::string& in, int level)
{
std::string result;
result.resize(in.size() + (in.size() >> 2) + 128);
size_t out_pos = 0;
if (LZMA_OK != lzma_easy_buffer_encode(
level, LZMA_CHECK_CRC32, NULL,
reinterpret_cast<uint8_t*>(const_cast<char*>(in.data())), in.size(),
reinterpret_cast<uint8_t*>(&result[0]), &out_pos, result.size()))
abort();
result.resize(out_pos);
return result;
}
std::string DecompressWithLzma(const std::string& in)
{
static const size_t kMemLimit = 1 << 30; // 1 GB.
lzma_stream strm = LZMA_STREAM_INIT;
std::string result;
result.resize(8192);
size_t result_used = 0;
lzma_ret ret;
ret = lzma_stream_decoder(&strm, kMemLimit, LZMA_CONCATENATED);
if (ret != LZMA_OK)
abort();
size_t avail0 = result.size();
strm.next_in = reinterpret_cast<const uint8_t*>(in.data());
strm.avail_in = in.size();
strm.next_out = reinterpret_cast<uint8_t*>(&result[0]);
strm.avail_out = avail0;
while (true) {
ret = lzma_code(&strm, strm.avail_in == 0 ? LZMA_FINISH : LZMA_RUN);
if (ret == LZMA_STREAM_END) {
result_used += avail0 - strm.avail_out;
if (0 != strm.avail_in) // Guaranteed by lzma_stream_decoder().
abort();
result.resize(result_used);
lzma_end(&strm);
return result;
}
if (ret != LZMA_OK)
abort();
if (strm.avail_out == 0) {
result_used += avail0 - strm.avail_out;
result.resize(result.size() << 1);
strm.next_out = reinterpret_cast<uint8_t*>(&result[0] + result_used);
strm.avail_out = avail0 = result.size() - result_used;
}
}
}
int _tmain(int argc, _TCHAR* argv[])
{
const std::string szSampleString = "This is a Test String!!";
std::string szCompressed = CompressWithLzma(szSampleString, 0);
std::string szDeCompressed = DecompressWithLzma(szCompressed);
return 0;
}
lzma 测试
最后编辑于 :
©著作权归作者所有,转载或内容合作请联系作者
- 文/潘晓璐 我一进店门,熙熙楼的掌柜王于贵愁眉苦脸地迎上来,“玉大人,你说我怎么就摊上这事。” “怎么了?”我有些...
- 文/花漫 我一把揭开白布。 她就那样静静地躺着,像睡着了一般。 火红的嫁衣衬着肌肤如雪。 梳的纹丝不乱的头发上,一...
- 文/苍兰香墨 我猛地睁开眼,长吁一口气:“原来是场噩梦啊……” “哼!你这毒妇竟也来了?” 一声冷哼从身侧响起,我...
推荐阅读更多精彩内容
- 性能测试 负载测试、容量测试、压力测试、强度测试都属于性能测试, 性能测试是指在给定条件基准的前提下能达到的运行程...