Link to home
Start Free TrialLog in
Avatar of musicc
musicc

asked on

Bzip2 source

Hello experts,

I have downloaded bzip2 source code from http://www.kohsuke.org/bzip2/ . Since there is no documentation with the source code, I do not know how to call the function for compress/uncompress. Could someone give me a sample code that uses this library?
P.S. I found this code in java.sun.com forum, but it does not work.

Thanks


//---compress-----/
    public byte [] bzip2Compress(byte [] input) {
        //*.bz2
        ByteArrayOutputStream baos = new ByteArrayOutputStream();
        CBZip2OutputStream bzip = null;
        try {
            bzip = new CBZip2OutputStream(baos);
            System.out.println(input.length);
            bzip.write(input, 0, input.length);
        } catch (IOException ex) {
            ex.printStackTrace();
        }
        System.out.println(baos.toString());
        output = baos.toByteArray();
        System.out.println("bzipcompress_output:\t" + output.length);
        return output;
    }

//----decompress---------------
    public byte [] bzip2Decompress(byte [] input, int actualDataSize) {
        //*.bz2
        ByteArrayInputStream bais = new ByteArrayInputStream(input);
        System.out.println("Test");
        CBZip2InputStream bzip = new CBZip2InputStream(bais);
        System.out.println("Test");
        ByteArrayOutputStream baos  = new ByteArrayOutputStream();
        output = new byte [actualDataSize];
        int numBytesRead = 0;
        //bzip =
        try {
            while((numBytesRead = bzip.read(output)) != -1) {
                baos.write(output, 0 , output.length);
            }
        } catch (IOException ex) {
            ex.printStackTrace();
        }
        output = baos.toByteArray();
        System.out.println("bzipdecompress_output" + output.length);
        return output;
    }

    public static void main(String [] args) {
        byte [] temp = null;
        byte [] temo = null;
        MainCompress cmp = new MainCompress();;
        String msg = "xcxcsfergergrtrthy";
        System.out.println(cmp.stream(msg.getBytes()));
        temp = cmp.bzip2Compress(msg.getBytes());
           
        try {
            System.out.write(temp);
        } catch (IOException ex) {
            ex.printStackTrace();
        }
        temo = cmp.bzip2Decompress(temp,5);
        System.out.println(temo.length);
         
    }
Avatar of CEHJ
CEHJ
Flag of United Kingdom of Great Britain and Northern Ireland image

>>but it does not work

Meaning?
Avatar of musicc
musicc

ASKER

It does not work because I get null pointer exception error when I run decompress method. I think there is something wrong with the compress function. It always returns 1 byte no matter what the bytestring size.
ASKER CERTIFIED SOLUTION
Avatar of shinobun
shinobun

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of musicc

ASKER

Great!!! It's work. Thank you all