Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Deflate in Java - Inflate in Javascript

I'm sending compressed data from a java app via nodejs to a webpage. The data is compressed with the java deflater and base64 encoded. On the webpage I'm trying to inflate the data with https://github.com/dankogai/js-deflate, but it does not work (empty result). Am I missing something?

Java side:

private String compress(String s) {
    DeflaterOutputStream def = null;
    String compressed = null;
    try {
        ByteArrayOutputStream out = new ByteArrayOutputStream();
        // create deflater without header
        def = new DeflaterOutputStream(out, new Deflater(Deflater.DEFAULT_COMPRESSION, true));
        def.write(s.getBytes());
        def.close();
        compressed = Base64.encodeBase64String(out.toByteArray());
        System.out.println(compressed);
    } catch(Exception e) {
        Log.c(TAG, "could not compress data: " + e);
    }
    return compressed;
}

Javascript side:

var data = RawDeflate.inflate(Base64.fromBase64(compressed));
like image 342
dbrettschneider Avatar asked Jun 27 '13 10:06

dbrettschneider


1 Answers

Try this:

public static String compressAndEncodeString(String str) {
    DeflaterOutputStream def = null;
    String compressed = null;
    try {
        ByteArrayOutputStream out = new ByteArrayOutputStream();
        // create deflater without header
        def = new DeflaterOutputStream(out, new Deflater(Deflater.BEST_COMPRESSION, true));
        def.write(str.getBytes());
        def.close();
        compressed = Base64.encodeToString(out.toByteArray(), Base64.DEFAULT);
    } catch(Exception e) {
        Log.e(TAG, "could not compress data: " + e);
    }
    return compressed;
}
like image 138
ejavice Avatar answered Oct 16 '22 02:10

ejavice