Compressing with GZip

158 views
Skip to first unread message

Mario Barón

unread,
Nov 7, 2013, 9:42:47 AM11/7/13
to codenameone...@googlegroups.com
Hey guys, 

I'm sending a json through a web service using the multipart Request but I want to reduce the size of data using CN1's GZip support but I don't really understand how this works can you please help/guide me on how to do this? 


Thanks a million.

Shai Almog

unread,
Nov 7, 2013, 1:58:24 PM11/7/13
to codenameone...@googlegroups.com
Hi,
haven't tried it but using GZipOutputStream to compress your data should probably work.

Mario Barón

unread,
Nov 8, 2013, 12:35:59 PM11/8/13
to codenameone...@googlegroups.com
Hey Shai, 

I've changed the way I was doing  things and simply went ahead and zipped the image using the GZipOutputstream and encoded it using Base64. What I really need to do now is reverse this, meaning that I need to take that Base64 string, decode it and unzip it so I can have the byte[] image once again. Can you please help me with this? How would I do it? 

thanks. 

Shai Almog

unread,
Nov 8, 2013, 3:35:15 PM11/8/13
to codenameone...@googlegroups.com
Hi Mario,
With multipart request you don't need the base64 since we already do that for you.

The gzip input stream would do the reverse e.g. http://docs.oracle.com/javase/6/docs/api/java/util/zip/GZIPInputStream.html

Mario Barón

unread,
Nov 8, 2013, 3:57:14 PM11/8/13
to codenameone...@googlegroups.com
Hey Shai, 

thanks for the quick answer. Sorry I didn't make my self 100% clear. I'm not using the multipart request anymore, I decided to instead compress the image and then encode it using the Base64 and sent it using a normal ConnectionRequest. But I'm a little stuck with the uncompressing of the image since I'm getting an "Incorrect header check" IOException when I apply the following method:

public static byte[] decodificarBase64YDescomprimirImagen(String imagenBase64) {
    byte[] imagenEnBytes = null;
    byte[] imagenDecodificada= Base64.decode(imagenBase64.getBytes());
    InputStream is= new ByteArrayInputStream(imagenDecodificada);
    GZIPInputStream gzipInput=null;
    try {
      gzipInput= new GZIPInputStream(is);
      imagenEnBytes= new byte[1028];
      gzipInput.read(imagenEnBytes,0,1028);
        
    } catch (IOException e) {
      // TODO Auto-generated catch block
      e.printStackTrace();
    }
   
    return imagenEnBytes;
  }

I'm not really sure how to correct this. Thanks.



On Thursday, November 7, 2013 9:42:47 AM UTC-5, Mario Barón wrote:

Shai Almog

unread,
Nov 9, 2013, 3:28:45 AM11/9/13
to codenameone...@googlegroups.com
Are the images jpgs/png, if so they won't compress anyway so there is no need for gzip.
If you are trying to upload large amounts of data without multipart this is very likely to fail, you need multipart for large uploads.

I have no idea why you get that exception and debugging these things is always a pain of digging into the wire protocol data.
Reply all
Reply to author
Forward
0 new messages