0

I have a Java application and I would like to compress content of certain String variable (as it's a huge JSON). Now, i saw a lot of examples where i can compress String into byte array, but as my application has to (optionally!) send that compressed data as a REST response (i'm using Java Spark) i would like to compress that String into another (smaller) string.

Not sure if that's really possible, that's why i'm here :)

Why i don't wanna send byte array over network? Because my response has two parts - metadata and actual data returned from the DB. I would like to keep metadata readable and only actual data compressed.

Is there a way to achieve this?

guest86
  • 2,894
  • 8
  • 49
  • 72
  • How big is the string? Is it consistently a large string or does it fluctuate? – Shane Dec 12 '17 at 15:42
  • 1
    You might be interested in [this from the Spark docs](http://sparkjava.com/documentation#gzip). This uses the HTTP standard for compressing: [the `Content-Encoding` response header](https://developer.mozilla.org/en-US/docs/Web/HTTP/Headers/Content-Encoding). – Brian Dec 12 '17 at 15:49
  • Size fluctuates. Sometimes it's like 100kb, sometimes it's 1MB. Hence the "optional" part :) – guest86 Dec 12 '17 at 15:50
  • Then i would recommend gzip compression on the byte array before sending it and uncompressing it on the other end. – Shane Dec 12 '17 at 15:53
  • Ok, but how can i send only part of the response as byte array? – guest86 Dec 12 '17 at 16:09
  • ZIP -> base64 = Compressed `String` - [as a conceptual example](https://stackoverflow.com/questions/7849067/how-can-i-convert-a-string-into-a-gzip-base64-string) – MadProgrammer Dec 12 '17 at 19:39

0 Answers0