0


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 ?

3
  • What if the string has more information than can be stored in 50 chars? Commented May 9, 2013 at 5:29
  • Really? There is no such pattern. If you compress a string s into a compressed version t, what makes you think that t is not a legal uncompressed string? Are you restricting the set of valid uncompressed strings? Commented May 9, 2013 at 5:32
  • @diev That would be cool to see the text of War and Peace compressed to 50 characters. I take it the OP has in mind a particular, very restricted set of possible strings. Or maybe the compression is allowed to be lossy? If that is the case, just drop all but the first 47 characters and add .... Commented May 9, 2013 at 5:35

3 Answers 3

1

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.

Sign up to request clarification or add additional context in comments.

1 Comment

... based on the Pigeonhole principle.
0

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

Comments

0

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

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.