Java decompressing array of bytes

前端 未结 2 565
悲&欢浪女
悲&欢浪女 2021-01-22 12:10

On server (C++), binary data is compressed using ZLib function:

compress2()

and it\'s sent over to client (Java). On client side (

相关标签:
2条回答
  • 2021-01-22 12:30

    I think the problem is not with unpack method but in packedBuffer content. Unpack works fine

    public static byte[] pack(String s) throws IOException {
        ByteArrayOutputStream out = new ByteArrayOutputStream();
        DeflaterOutputStream dout = new DeflaterOutputStream(out);
        dout.write(s.getBytes());
        dout.close();
        return out.toByteArray();
    }
    
    public static void main(String[] args) throws Exception {
        byte[] a = pack("123");
        String s = unpack(a);   // calls your unpack
        System.out.println(s);
    }
    

    output

    123
    
    0 讨论(0)
  • 2021-01-22 12:44

    InflaterInputStream is expecting raw deflate data (RFC 1951), whereas compress2() is producing zlib-wrapped deflate data (RFC 1950 around RFC 1951).

    Inflater on the other hand does process zlib-wrapped data (unless you give it the nowrap option). Go figure.

    0 讨论(0)
提交回复
热议问题