Click here to Skip to main content
11,438,636 members (77,771 online)
Rate this: bad
good
Please Sign up or sign in to vote.
See more: C++
How to compress text files using bzlib library in bzip2. I am not getting it, anyone to assist with the coding in C++. .i read through bzip2 manual bzip.org/1.0.5/bzip2-manual-1.0.5.html#libprog but cant figure it out. pls assist

What am trying to do in the main project is: I read a rexr file and get the size in bytes, then i compress the same text file using bzip2 library and get the compressed size of the file.

for example : having test.txt as input and it must give me text.txt.bz2

here is what i have so far:

#include "stdafx.h"
#include <iostream>
#include <fstream>
#include <string>
#include "bzlib.h"

using namespace std;
 
using std::cout;
using std::cin;
 

int _tmain(int argc, _TCHAR* argv[])
 
{
  
   fstream file;
   ifstream::pos_type size;
   ifstream::pos_type size2;
   char * memblock;
 
  ifstream file ("C:\\test.txt", ios::in|ios::binary|ios::ate);
  if (file.is_open())
  {
    size = file.tellg();
    memblock = new char [size];
    file.seekg (0, ios::beg);
    file.read (memblock, size);
    file.close();
    cout<<size; // size of the text file  
  
    }
  
     // compress with bzip2 
      
    
      int BZ2_bzCompress (bz_stream *stream, int action);
    
      
    
    // get the size of the compressed text file
    
     
}
Posted 9-Apr-12 0:32am
Edited 9-Apr-12 1:08am
v3
Comments
Nelek at 9-Apr-12 5:39am
   
Can you post the relevant code you tried? Or give us a bit more information about the errors you get and so on.
Chandrasekharan P at 9-Apr-12 6:09am
   
Added Pretags.

1 solution

Rate this: bad
good
Please Sign up or sign in to vote.

Solution 1

The sequence of steps is pretty well set out in the manual. Start at http://www.bzip.org/1.0.5/bzip2-manual-1.0.5.html#bzcompress-init and work your way down the page! As you haven't got any code in there to do with compression just give it a stab. Sling in the three calls you need to use the low level interface and go from there. If you check the return codes it gives you some pretty hefty hints about what's going wrong.

Read the bits about what to do with the bz_stream structure VERY carefully. Remember it's not a C++ object so you can't rely on a constructor, you have to zero out all the fields by hand or use something like std::memset. My guidelines would be:

- allocate a bz_stream object
- clear it with std::memset
- allocate an output buffer, you've already got an input buffer. Make it the same size as the input buffer at first, compressed data probably isn't going to be bigger than the input data
- call BZ2_bzCompressInit on the structure
- set all the pointers and sizes you need to in the bz_stream structure
- call BZ2_bzCompress. As you've already read your complete file you can do it in one call
- write the compressed data to a file
- clean up by calling BZ2_bzCompressEnd and, if necessary freeing the bz_stream object

Anyway, bash out some code, give it a run and if it fails in a way you don't understand what's going wrong we'll try and help. BUT try and write the code first.

Cheers,

Ash

Edited as I didn't check my spellung.
  Permalink  
v2

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



Advertise | Privacy | Mobile
Web01 | 2.8.150506.1 | Last Updated 14 Apr 2012
Copyright © CodeProject, 1999-2015
All Rights Reserved. Terms of Service
Layout: fixed | fluid

CodeProject, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100