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.l ength);
bzip.write(input, 0, input.length);
} catch (IOException ex) {
ex.printStackTrace();
}
System.out.println(baos.to String());
output = baos.toByteArray();
System.out.println("bzipco mpress_out put:\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("bzipde compress_o utput" + 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.str eam(msg.ge tBytes())) ;
temp = cmp.bzip2Compress(msg.getB ytes());
try {
System.out.write(temp);
} catch (IOException ex) {
ex.printStackTrace();
}
temo = cmp.bzip2Decompress(temp,5 );
System.out.println(temo.le ngth);
}
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.l
bzip.write(input, 0, input.length);
} catch (IOException ex) {
ex.printStackTrace();
}
System.out.println(baos.to
output = baos.toByteArray();
System.out.println("bzipco
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("bzipde
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.str
temp = cmp.bzip2Compress(msg.getB
try {
System.out.write(temp);
} catch (IOException ex) {
ex.printStackTrace();
}
temo = cmp.bzip2Decompress(temp,5
System.out.println(temo.le
}
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
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER
Great!!! It's work. Thank you all
Meaning?