使用bzlib.h在C ++中进行BZ2压缩 [英] BZ2 compression in C++ with bzlib.h

查看:597
本文介绍了使用bzlib.h在C ++中进行BZ2压缩的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我目前需要一些帮助,以学习如何使用bzlib.h标头。我想知道是否有人会帮助我使用任何Boost库在C ++ 中找到compressToBZ2()函数?

I currently need some help learning how to use bzlib.h header. I was wondering if anyone would be as so kind to help me figure out a compressToBZ2() function in C++ without using any Boost libraries?

void compressBZ2(std::string file)
{
std::ifstream infile;
int fileDestination = infile.open(file.c_str());

char bz2Filename[] = "file.bz2";
FILE *bz2File = fopen(bz2Filename, "wb");
int bzError;
const int BLOCK_MULTIPLIER = 7;
BZFILE *myBZ = BZ2_bzWriteOpen(&bzError, bz2File, BLOCK_MULTIPLIER, 0, 0);

const int BUF_SIZE = 10000;
char* buf = new char[BUF_SIZE];
ssize_t bytesRead;

while ((bytesRead = read(fileDestination, buf, BUF_SIZE)) > 0)
{
    BZ2_bzWrite(&bzError, myBZ, buf, bytesRead);
}

BZ2_bzWriteClose(&bzError, myBZ, 0, NULL, NULL);

delete[] buf;
}

我一直在尝试使用的是这样的东西,但是我我没有运气。我正在尝试获取.bz2文件 .tar.bz2

What I've been trying to do is use something like this but I've had no luck. I am trying to get a .bz2 file not .tar.bz2

有帮助吗?

推荐答案

这两行是错误的:

int fileDestination = infile.open(file.c_str());

// ...

while ((bytesRead = read(fileDestination, buf, BUF_SIZE)) > 0)

std :: ifstream 有效。例如,如果您查看 std :: ifstream ::打开 ,它不会返回任何内容。似乎您在将旧的系统调用 open / read 与C ++流概念混合在一起。

This isn't how std::ifstream works. For example, if you look at std::ifstream::open it doesn't return anything. It seems you are mixing up the old system calls open/read with the C++ stream concept.

只需这样做:

infile.open(file.c_str());

// ...

while (infile.read(buf, BUF_SIZE))

建议您阅读有关使用流的更多信息。

I recommend you read up more on using streams.

这篇关于使用bzlib.h在C ++中进行BZ2压缩的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆