Karthikeyan
Karthikeyan

Reputation: 526

Compressing string to specified max chars in Java


I want to compress any string, if it has more than 50chars in it. If char size if less than 50, then let it be untouched, else it has to be compressed to required limit (50).

I will be inserting this compressed/uncompressed output in DB. So, while fetching back the data from DB, i want the compressed string to be easily differentiated from uncompressed (some common pattern for compressed string).

Please suggest some best compressing lib/algorithm ?

Upvotes: 0

Views: 414

Answers (3)

Evgeniy Dorofeev
Evgeniy Dorofeev

Reputation: 136162

I would use Inflater / Deflater from java.util.zip package. They use native ZLIB compression library and are very efficient. It's easy to differentiate compressed text from uncompressed - try to decompress and you will get an exception if it's a plain text

Upvotes: 0

vishal_aim
vishal_aim

Reputation: 7854

Please suggest some best compressing lib/algorithm ?

there is no such algorithm or library so that any string can be compressed to a given max characters (e.g. 50) without any loss of information.

Upvotes: 1

Ray Toal
Ray Toal

Reputation: 88488

If your compression is allowed to be lossy, you can use the abbreviate function from Apache Commons lang

Upvotes: 0

Related Questions