Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

compression and decompression of string data in java

Tags:

java

gzip

I am using the following code to compress and decompress string data, but the problem which I am facing is, it is easily getting compressed without error, but decompress method throws following error.

Exception in thread "main" java.io.IOException: Not in GZIP format

public static void main(String[] args) throws Exception {         String string = "I am what I am hhhhhhhhhhhhhhhhhhhhhhhhhhhhh"                 + "bjggujhhhhhhhhh"                 + "rggggggggggggggggggggggggg"                 + "esfffffffffffffffffffffffffffffff"                 + "esffffffffffffffffffffffffffffffff"                 + "esfekfgy enter code here`etd`enter code here wdd"                 + "heljwidgutwdbwdq8d"                 + "skdfgysrdsdnjsvfyekbdsgcu"                 +"jbujsbjvugsduddbdj";         System.out.println("after compress:");         String compressed = compress(string);         System.out.println(compressed);         System.out.println("after decompress:");         String decomp = decompress(compressed);         System.out.println(decomp);     }        public static String compress(String str) throws Exception {         if (str == null || str.length() == 0) {             return str;         }         System.out.println("String length : " + str.length());         ByteArrayOutputStream obj=new ByteArrayOutputStream();         GZIPOutputStream gzip = new GZIPOutputStream(obj);         gzip.write(str.getBytes("UTF-8"));         gzip.close();         String outStr = obj.toString("UTF-8");         System.out.println("Output String length : " + outStr.length());         return outStr;      }        public static String decompress(String str) throws Exception {         if (str == null || str.length() == 0) {             return str;         }         System.out.println("Input String length : " + str.length());         GZIPInputStream gis = new GZIPInputStream(new ByteArrayInputStream(str.getBytes("UTF-8")));         BufferedReader bf = new BufferedReader(new InputStreamReader(gis, "UTF-8"));         String outStr = "";         String line;         while ((line=bf.readLine())!=null) {           outStr += line;         }         System.out.println("Output String lenght : " + outStr.length());         return outStr;      } 

Still couldn't figure out how to fix this issue!!!

like image 900
rampuriyaaa Avatar asked May 03 '13 04:05

rampuriyaaa


People also ask

How do I compress a string in Java?

Deflator is one of most used class for string compression in java. It uses the popular ZLIB compression library. It provides the function called deflator() to compress string. The function first takes the input string data, performs the compression and then fills the given buffer with the compressed data.

What is data compression in Java?

This class implements an output stream filter for compressing data in the “deflate” compression format. It is also used as the basis for other types of compression filters, such as GZIPOutputStream. Important Methods: void close() : Writes remaining compressed data to the output stream and closes the underlying stream.

What is compressing a string?

"String Compression Algorithm” or “Run Length Encoding” happens when you compress a string, and the consecutive duplicates of each string are replaced with the character, followed by the consecutive, repeated character count. For example: After string compression, the string “aaaabbcddddd” would return “a4b2c1d5”.


2 Answers

This is because of

String outStr = obj.toString("UTF-8"); 

Send the byte[] which you can get from your ByteArrayOutputStream and use it as such in your ByteArrayInputStream to construct your GZIPInputStream. Following are the changes which need to be done in your code.

byte[] compressed = compress(string); //In the main method  public static byte[] compress(String str) throws Exception {     ...     ...     return obj.toByteArray(); }  public static String decompress(byte[] bytes) throws Exception {     ...     GZIPInputStream gis = new GZIPInputStream(new ByteArrayInputStream(bytes));     ... } 
like image 59
Rahul Avatar answered Nov 04 '22 10:11

Rahul


The above Answer solves our problem but in addition to that. if we are trying to decompress a uncompressed("not a zip format") byte[] . we will get "Not in GZIP format" exception message.

For solving that we can add addition code in our Class.

public static boolean isCompressed(final byte[] compressed) {     return (compressed[0] == (byte) (GZIPInputStream.GZIP_MAGIC)) && (compressed[1] == (byte) (GZIPInputStream.GZIP_MAGIC >> 8)); } 

My Complete Compression Class with compress/decompress would look like:

import java.io.BufferedReader; import java.io.ByteArrayInputStream; import java.io.ByteArrayOutputStream; import java.io.IOException; import java.io.InputStreamReader; import java.util.zip.GZIPInputStream; import java.util.zip.GZIPOutputStream;  public class GZIPCompression {   public static byte[] compress(final String str) throws IOException {     if ((str == null) || (str.length() == 0)) {       return null;     }     ByteArrayOutputStream obj = new ByteArrayOutputStream();     GZIPOutputStream gzip = new GZIPOutputStream(obj);     gzip.write(str.getBytes("UTF-8"));     gzip.flush();     gzip.close();     return obj.toByteArray();   }    public static String decompress(final byte[] compressed) throws IOException {     final StringBuilder outStr = new StringBuilder();     if ((compressed == null) || (compressed.length == 0)) {       return "";     }     if (isCompressed(compressed)) {       final GZIPInputStream gis = new GZIPInputStream(new ByteArrayInputStream(compressed));       final BufferedReader bufferedReader = new BufferedReader(new InputStreamReader(gis, "UTF-8"));        String line;       while ((line = bufferedReader.readLine()) != null) {         outStr.append(line);       }     } else {       outStr.append(compressed);     }     return outStr.toString();   }    public static boolean isCompressed(final byte[] compressed) {     return (compressed[0] == (byte) (GZIPInputStream.GZIP_MAGIC)) && (compressed[1] == (byte) (GZIPInputStream.GZIP_MAGIC >> 8));   } } 
like image 23
Arun Pratap Singh Avatar answered Nov 04 '22 09:11

Arun Pratap Singh