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

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 theString(byte[], int, int)constructors. You should always specify an encoding. The simplest way of reading the data would be to create anInputStreamReaderwrapping theGZIPInputStream, 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).