• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Compressing and Decompressing strings in Java

 
Chitra Raji
Greenhorn
Posts: 10
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 79
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 10
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 79
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 10
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
i tried that also. it gives the same exception. any other alternative?
 
Greg T Robertson
Ranch Hand
Posts: 91
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 7023
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
Susanta Chatterjee
Ranch Hand
Posts: 102
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 10
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 10
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Greg T Robertson

Thanx for the information. It worked.

Thanx and Regards
Chitra
 
Alexandr Erofeev
Greenhorn
Posts: 2
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Pie
Posts: 15150
31
Android IntelliJ IDE Java Scala Spring
  • 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
Alexandr Erofeev
Greenhorn
Posts: 2
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator


It's work
 
Winston Gutkowski
Bartender
Pie
Posts: 9470
49
Eclipse IDE Hibernate Ubuntu
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Alexandr Erofeev wrote:It's work

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

Winston
 
Matthieu Labas
Greenhorn
Posts: 1
Eclipse IDE Linux VI Editor
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic