BZ2 compression in C++ with bzlib.h

∥☆過路亽.° 提交于 2019-12-05 14:54:50

These two lines are wrong:

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

// ...

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

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.

Just do:

infile.open(file.c_str());

// ...

while (infile.read(buf, BUF_SIZE))

I recommend you read up more on using streams.

Try with libbzip2.

It's available in C.

http://bzip.org/

For a code sample see: dlltest.c

I modified the loop and it's working.

int BUF_SIZE = 1024 * 10;    
char* buf = new char[BUF_SIZE];
while(infile.tellg() >= 0) {
    infile.read(buf, BUF_SIZE);
    BZ2_bzWrite(&bzError, myBZ, buf, infile.gcount());
}    
BZ2_bzWriteClose(&bzError, myBZ, 0, NULL, NULL);
标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!