0
votes

Hi I am using GZIP for compression and decompression of strings.

Getting Some Exceptions ! Please Help me out !

protected byte[] CompressInputString(String input_string2)
        throws IOException {
    ByteArrayOutputStream os = new ByteArrayOutputStream(
            input_string2.length());
    System.out.println("Byte Array OS : " + os);
    GZIPOutputStream gos = new GZIPOutputStream(os);
    System.out.println("GZIPOutputStream : " + gos);
    gos.write(input_string2.getBytes());
    System.out.println("GZIPOutputStream get bytes: "
            + input_string2.getBytes());
    gos.close();
    byte[] compressed = os.toByteArray();
    os.close();
    System.out.println("Compressed : " + compressed);
    return compressed;

}

protected String DecompressInputString(byte[] input_to_decode_from_function)
        throws IOException {
    final int BUFFER_SIZE = 32;
    ByteArrayInputStream is = new ByteArrayInputStream(input_to_decode_from_function);
    GZIPInputStream gis = new GZIPInputStream(is, BUFFER_SIZE);
    StringBuilder string = new StringBuilder();
    byte[] data = new byte[BUFFER_SIZE];
    int bytesRead;
    while ((bytesRead = gis.read(data)) != -1) {
        string.append(new String(data, 0, bytesRead));
    }
    gis.close();
    is.close();
    return string.toString();
}

My input is : abcdefghijklmnop

The output is

GZIPOutputStream : java.util.zip.GZIPOutputStream@f38798
GZIPOutputStream get bytes: [B@4b222f
Compressed : [B@b169f8
Compressed File : [B@b169f8

To uncompress, what input do I give?

If I put [B@b169f8 as string input and convert it to byte array using input.getBytes() and pass it to my decompress function, an exception is raised

NOT IN GZIP FORMAL EXCEPTION LOG

3
First suggestion: never use String.getBytes() without specifying an encoding... - Jon Skeet
so what is the second option sir , i need to get this code rolling asap ! - rolling.stones
That wasn't an "option" - it was just part of what's wrong. See my answer for more. - Jon Skeet

3 Answers

2
votes

IF I PUT [B@b169f8 as string input and convert it to byte array using INPUT.getBytes() and pass it to my decompress function ... A EXCEPTION IS RAISED

Well yes - because "[B@b169f8" isn't actually the content of the byte array. It's just the result of calling toString() on it; it's basically indicating the type of the object and its hash.

If you want to convert a byte array into a string in a lossless way, you need to use something like base64. There are lots of libraries which perform base64 encoding, including this public domain one.

Additionally, I would strongly advise against using either String.getBytes() or the String(byte[], int, int) constructors. You should always specify an encoding. The simplest way of reading the data would be to create an InputStreamReader wrapping the GZIPInputStream, and read text data directly from that. I'd suggest using UTF-8 as an encoding for the initial text-to-binary conversion (and on the decompression side too, of course).

0
votes

First, you're turning your byte array into a string using .toString() on the byte array, which returns a representation of its memory address, not its contents.

You'll want a reasonable way to represent your byte array as a string, if you really do need it as a string in between. Base64 is a reasonable way to do this.

Then, you'll need to turn it back into a byte array before feeding it to the inflator.

Or, you could skip the above two steps and just keep the byte array as a byte array, and feed it directly to the inflator.

0
votes

Works fine to me, I've tried calling your methods like so:

public static void main(String[] args) throws Throwable {
    Test tf = new Test();
    byte[] compressed = tf.CompressInputString("Jimmy");
    System.out.println(Arrays.toString(compressed));
    String s = tf.DecompressInputString(compressed);
    System.out.println(s);//Prints "Jimmy"

}

Can you add an example of how you call the methods, and also a complete stack trace of any exception?