File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes Java in General and the fly likes Compressing and Decompressing strings in Java Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Java in General
Bookmark "Compressing and Decompressing strings in Java" Watch "Compressing and Decompressing strings in Java" New topic
Author

Compressing and Decompressing strings in Java

Chitra Raji
Greenhorn

Joined: Dec 01, 2003
Posts: 10
Hi

I compressed a string using GZIPOutputStream. Now when i try to uncompress it using GZIPInputStream throws the following exception "java.util.zip.ZipException: invalid bit length repeat" while reading from the stream.

Anyone who has compressed and or uncompressed strings, please help.
Kalai Selvan
Ranch Hand

Joined: Jul 07, 2004
Posts: 79
Hi Chitra,
I just tried the following code and it works......



Please while sending such question also provide the code. So the problem will be easy to undestand.


Regards,
Kalai Selvan T.
Chitra Raji
Greenhorn

Joined: Dec 01, 2003
Posts: 10
Thanx.

But the problem is i dont want to use files. i want to compress/decompress a string. Following is my code.

private static String compress(String str)
{
String str1 = null;
ByteArrayOutputStream bos = null;
try
{
bos = new ByteArrayOutputStream();
BufferedOutputStream dest = null;

byte b[] = str.getBytes();
GZIPOutputStream gz = new GZIPOutputStream(bos,b.length);
gz.write(b,0,b.length);
bos.close();
gz.close();

}
catch(Exception e) {
System.out.println(e);
e.printStackTrace();
}
byte b1[] = bos.toByteArray();
return new String(b1);
}

private static String deCompress(String str)
{
String s1 = null;

try
{
byte b[] = str.getBytes();
InputStream bais = new ByteArrayInputStream(b);
GZIPInputStream gs = new GZIPInputStream(bais);
ByteArrayOutputStream baos = new ByteArrayOutputStream();
int numBytesRead = 0;
byte [] tempBytes = new byte[6000];
try
{
while ((numBytesRead = gs.read(tempBytes, 0, tempBytes.length)) != -1)
{
baos.write(tempBytes);
}
s1 = new String(baos.toByteArray());
s1= baos.toString();
}
catch(ZipException e)
{
e.printStackTrace();
}
}
catch(Exception e) {
e.printStackTrace();
}
return s1;
}
Kalai Selvan
Ranch Hand

Joined: Jul 07, 2004
Posts: 79
Hi Chitra,
U can refer java doc of java.util.zip.Deflater and ava.util.zip.Inflator,
some examples were given there.

I hope that will help you.

Regards,
Kalai Selvan T.
Chitra Raji
Greenhorn

Joined: Dec 01, 2003
Posts: 10
i tried that also. it gives the same exception. any other alternative?
Greg T Robertson
Ranch Hand

Joined: Nov 18, 2003
Posts: 91
You may need to base 64 encode the strings first depending upon what exactly is in them. There are severl Base 64 encoders out there (may be one in standard J2SE now not sure) but if not check apache's website.
Dirk Schreckmann
Sheriff

Joined: Dec 10, 2001
Posts: 7023
When posting code, please be sure to surround the code with the [code] and [/code] UBB Tags. This will help to preserve the formatting of the code, thus making it easier to read and understand.


[How To Ask Good Questions] [JavaRanch FAQ Wiki] [JavaRanch Radio]
Susanta Chatterjee
Ranch Hand

Joined: Aug 12, 2002
Posts: 102
Hi Chitra,

Change the line containing:
to
in deCompress method.

I tried that with your two methods and works perfectly.

What happening is: you are writing more bytes to your output stream, than you read in from inputstream and thereby corrupting the stream.

Also, in future, when you post, please follow the advices made to you already by others.

Susanta
[ September 23, 2004: Message edited by: Susanta Chatterjee ]
Chitra Raji
Greenhorn

Joined: Dec 01, 2003
Posts: 10
Hi susanta

i tried the code that u sent. it is not working. i am still getting the same exception.

thanx.
Chitra
Chitra Raji
Greenhorn

Joined: Dec 01, 2003
Posts: 10
Hi Greg T Robertson

Thanx for the information. It worked.

Thanx and Regards
Chitra
Alexandr Erofeev
Greenhorn

Joined: Jun 10, 2012
Posts: 2
Chitra Raji wrote:
But the problem is i dont want to use files. i want to compress/decompress a string. Following is my code.



Your code generates an error.

Сode:
Jesper de Jong
Java Cowboy
Saloon Keeper

Joined: Aug 16, 2005
Posts: 14114
    
  16

Hello Alexandr, welcome to the Ranch!

Thanks for you answer. Please note that the original question was asked in 2004. The person who asked the question is probably not still waiting for an answer eight years later.


Java Beginners FAQ - JavaRanch SCJP FAQ - The Java Tutorial - Java SE 7 API documentation
Scala Notes - My blog about Scala
Alexandr Erofeev
Greenhorn

Joined: Jun 10, 2012
Posts: 2


It's work
Winston Gutkowski
Bartender

Joined: Mar 17, 2011
Posts: 7699
    
  20

Alexandr Erofeev wrote:It's work

Did you read Jesper's post? It might work, but it's 8 years too late.

Winston


Isn't it funny how there's always time and money enough to do it WRONG?
Articles by Winston can be found here
Matthieu Labas
Greenhorn

Joined: Jul 18, 2013
Posts: 1

Hi, I registered to the site just to answer: it might be 8 years too late, but it still is interesting to some people who stumble upon that question (like me).
In the end, Alexandr's solution was the one that worked for me, and I'm happy he took time to write it, despite Jesper's comment!

Спасибо Саша!

Matthieu
 
wood burning stoves
 
subject: Compressing and Decompressing strings in Java