Solved

Bzip2 source

Posted on 2006-10-29
4
703 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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

PeopleSoft Has Never Been Easier

PeopleSoft Adoption Made Smooth & Simple!

On-The-Job Training Is made Intuitive & Easy With WalkMe's On-Screen Guidance Tool.  Claim Your Free WalkMe Account Now

Question has a verified solution.

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

For beginner Java programmers or at least those new to the Eclipse IDE, the following tutorial will show some (four) ways in which you can import your Java projects to your Eclipse workbench. Introduction While learning Java can be done with…
Introduction This article is the second of three articles that explain why and how the Experts Exchange QA Team does test automation for our web site. This article covers the basic installation and configuration of the test automation tools used by…
Viewers learn about the “for” loop and how it works in Java. By comparing it to the while loop learned before, viewers can make the transition easily. You will learn about the formatting of the for loop as we write a program that prints even numbers…
This theoretical tutorial explains exceptions, reasons for exceptions, different categories of exception and exception hierarchy.

627 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