• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

How to compress String?

 
Chetan Parekh
Ranch Hand
Posts: 3640
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I have a String variable that contains some data and I want to compres it.

How can I achieve the same?

I don't want to store in file, I want to compress it in varible only.
 
Stuart Ash
Ranch Hand
Posts: 637
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by Chetan Parekh:
I have a String variable that contains some data and I want to compres it.

How can I achieve the same?

I don't want to store in file, I want to compress it in varible only.


Does the String contain some unutterable utterances by yourself?

I guess, creating a StringWriter wrapped into a Zip Writer or something will do? Something like create a ZipOutputStream wrapped into an OutputStream that can give you a String, say a ByteOutputStream. Do the compress, and then do a toString and store the result in a variable.
 
Chetan Parekh
Ranch Hand
Posts: 3640
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by Stuart Ash:
Does the String contain some unutterable utterances by yourself?

Please simplify what you want to convey in above sentence.
[ December 26, 2005: Message edited by: Chetan Parekh ]
 
Stan James
(instanceof Sidekick)
Ranch Hand
Posts: 8791
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Remember String is unicode, so it's using 2 bytes of memory for each character. You could just use char[] and save half. At least until you want to turn them back to string for output.

There are some simple techniques that work with strings with certain characteristics. For example I used to work with mainframe records with lots of repeated spaces and zeros and such. I replaced 5-to-99 repeated characters with an escape, a counter and the character so x999999x becomes x!069x. It got dramatic compression with very low overhead but only because the data had extensive repetition.

I worked in one language that had six-bit characters. If you can get by with only 64 characters you can compress your char[] to a non-standard byte[] and compress 25%. I think I'm only kidding. Unless it sounds useful to you.
 
Scott Selikoff
author
Saloon Keeper
Posts: 4028
18
Eclipse IDE Flex Google Web Toolkit
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Whats the purpose of the compression? Size of the string or encoding it securely? What is the range of elements in the string, such as the character set?
 
u johansson
Ranch Hand
Posts: 47
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
The String contains some data.

It's so BASIC'ish.

Split it up. Put the data where they belong. Introduce types (classes and interfaces).
 
u johansson
Ranch Hand
Posts: 47
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Any compression requires data with similarities. If your data hasn't got any - you cannot compress it.
 
Ilja Preuss
author
Sheriff
Posts: 14112
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by u johansson:
Any compression requires data with similarities. If your data hasn't got any - you cannot compress it.


That is not true. Huffman coding, for example, doesn't need any similarities, but compresses texts by encoding more often used characters with shorter bit sequences.
 
Ilja Preuss
author
Sheriff
Posts: 14112
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by Stan James:
Remember String is unicode, so it's using 2 bytes of memory for each character. You could just use char[] and save half.


Isn't a char two bytes in Java?
 
u johansson
Ranch Hand
Posts: 47
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Sorry, Ilja Preuss, Huffman coding is based on similarites.
 
Rick O'Shay
Ranch Hand
Posts: 531
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by Chetan Parekh:
I have a String variable that contains some data and I want to compres it.

How can I achieve the same?

I don't want to store in file, I want to compress it in varible only.


More generally, how does one compress data. Let's assume you want to recover the information at some later date. This rules out compresssion schemes that can squeeze an infinite amounts of data in zero bytes. Let's assume you need full recovery. Use tools in the java.util.zip package.
 
Ilja Preuss
author
Sheriff
Posts: 14112
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by u johansson:
Sorry, Ilja Preuss, Huffman coding is based on similarites.


Well, then your definition of "similarities" must be quite wide - wide enough that almost any data contains some. Not sure how your argument is going to help, then...
 
Chetan Parekh
Ranch Hand
Posts: 3640
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Reason to compress data is:

I have an Applet that get CSV value as String from Servlet and I need to compress it.

Is there any other way to pass data in a compressed format between Applet and Servlet?
 
Stan James
(instanceof Sidekick)
Ranch Hand
Posts: 8791
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
By "compress" we've all meant to reduce a string in size. That doesn't sound like a solid part of your requirements and from that last post I'm not entirely sure that's what you meant.

Compression is done some times to reduce the number of bytes sent over the network. I wouldn't do it until somebody can prove I'm sending too many bytes over the network ... stressing some part of the network or experiencing very long response time. I mentioned the simple compression scheme I did for mainframe data. It gave no performance gain and was not put into production. It was great fun, but only proved that data size was NOT the problem.

What does your data look like? Could you show a sample? How big is the string you're dealing with?
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic