I'm following the bzip2 programming with libbzip2 instructions to make a script for compression/decompression, but I have run into issues with the reading step. Here's my code:
int decode = (argv[4][0]=='d');
FILE* f = fopen( argv[2], "rb" ); if( f==0 ) return 2;
FILE* g = fopen( argv[3], "wb" ); if( g==0 ) return 2;
int bzError;
int nBuf;
int f_len = flen(f);
byte* inp = new byte[f_len*4+1024*1024]; if( inp==0 ) return 3;
f_len = fread( inp, 1,f_len, f );
if( decode==0 ) {
int BLOCK_MULTIPLIER = atoi( argv[5] );
BZFILE *myBZ = BZ2_bzWriteOpen(&bzError, g, BLOCK_MULTIPLIER, 0, 0);
BZ2_bzWrite(&bzError, myBZ, inp, f_len);
BZ2_bzWriteClose(&bzError, myBZ, 0, NULL, NULL);
} else {
byte buf[4096];
BZFILE *myBZ = BZ2_bzReadOpen(&bzError, f, 0, 0, NULL, 0);
if (bzError != BZ_OK) {
fprintf(stderr, "E: BZ2_bzReadOpen: %d\n", bzError);
return -1;
}
while (bzError == BZ_OK) {
int nread = BZ2_bzRead(&bzError, myBZ, buf, sizeof buf);
cout<<"nread= "<<nread<<"\n";
if (bzError == BZ_OK || bzError == BZ_STREAM_END) {
size_t nwritten = fwrite(buf, 1, nread, stdout);
if (nwritten != (size_t) nread) {
fprintf(stderr, "E: short write\n");
return -1;
}
}
}
if (bzError != BZ_STREAM_END) {
fprintf(stderr, "E: bzip error after read: %d\n", bzError);
return -1;
}
BZ2_bzReadClose(&bzError, myBZ);
return 0;
}
The compression mode works fine, but if it is in decompression mode, the bzRead step fails and I get the following output from my error messages/statements:
nread = 0 E: bzip error after read: -7
Why would nread be 0? Also, the -7 represents an unexpected EOF, but how is that possible? I have tried running this on files compressed with the built in linux bzip2 as well, and gotten the same output.