Solved

Bzip2 source

Posted on 2006-10-29
4
688 Views
Last Modified: 2012-08-14
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);
         
    }
0
Comment
Question by:musicc
  • 2
4 Comments
 
LVL 86

Expert Comment

by:CEHJ
ID: 17829193
>>but it does not work

Meaning?
0
 

Author Comment

by:musicc
ID: 17829334
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.
0
 
LVL 9

Accepted Solution

by:
shinobun earned 200 total points
ID: 17830261
You need to close the stream, or else it will not complete the bzip compression.

        try {
            bzip = new CBZip2OutputStream(baos);
            System.out.println(input.length);
            bzip.write(input, 0, input.length);
            bzip.close();  //  <-- here
        } catch (IOException ex) {
            ex.printStackTrace();
        }
0
 

Author Comment

by:musicc
ID: 17830791
Great!!! It's work. Thank you all
0

Featured Post

Free Tool: Site Down Detector

Helpful to verify reports of your own downtime, or to double check a downed website you are trying to access.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
Weekend adv creator 3 54
Should localization be done inside spring controller 5 32
jsp insert to database example 2 47
SequenceInputStream example 3 19
By the end of 1980s, object oriented programming using languages like C++, Simula69 and ObjectPascal gained momentum. It looked like programmers finally found the perfect language. C++ successfully combined the object oriented principles of Simula w…
Java functions are among the best things for programmers to work with as Java sites can be very easy to read and prepare. Java especially simplifies many processes in the coding industry as it helps integrate many forms of technology and different d…
Viewers learn about the scanner class in this video and are introduced to receiving user input for their programs. Additionally, objects, conditional statements, and loops are used to help reinforce the concepts. Introduce Scanner class: Importing…
The viewer will learn how to implement Singleton Design Pattern in Java.

830 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question