Solved

Bzip2 source

Posted on 2006-10-29
4
659 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
Comment Utility
>>but it does not work

Meaning?
0
 

Author Comment

by:musicc
Comment Utility
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
Comment Utility
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
Comment Utility
Great!!! It's work. Thank you all
0

Featured Post

What Security Threats Are You Missing?

Enhance your security with threat intelligence from the web. Get trending threat insights on hackers, exploits, and suspicious IP addresses delivered to your inbox with our free Cyber Daily.

Join & Write a Comment

After being asked a question last year, I went into one of my moods where I did some research and code just for the fun and learning of it all.  Subsequently, from this journey, I put together this article on "Range Searching Using Visual Basic.NET …
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 tutorial will introduce the viewer to VisualVM for the Java platform application. This video explains an example program and covers the Overview, Monitor, and Heap Dump tabs.

743 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

Need Help in Real-Time?

Connect with top rated Experts

16 Experts available now in Live!

Get 1:1 Help Now