?
Solved

Bzip2 source

Posted on 2006-10-29
4
Medium Priority
?
707 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 800 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

Hire Technology Freelancers with Gigs

Work with freelancers specializing in everything from database administration to programming, who have proven themselves as experts in their field. Hire the best, collaborate easily, pay securely, and get projects done right.

Question has a verified solution.

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

Introduction Java can be integrated with native programs using an interface called JNI(Java Native Interface). Native programs are programs which can directly run on the processor. JNI is simply a naming and calling convention so that the JVM (Java…
Go is an acronym of golang, is a programming language developed Google in 2007. Go is a new language that is mostly in the C family, with significant input from Pascal/Modula/Oberon family. Hence Go arisen as low-level language with fast compilation…
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…
This theoretical tutorial explains exceptions, reasons for exceptions, different categories of exception and exception hierarchy.
Suggested Courses

719 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