Android: decompress string that was compressed with PHP gzcompress()

前端 未结 3 1308
别跟我提以往
别跟我提以往 2021-01-03 15:30

How can i decompress a String that was zipped by PHP gzcompress() function?

Any full examples?

thx

I tried it now like this:

public s         


        
3条回答
  •  予麋鹿
    予麋鹿 (楼主)
    2021-01-03 15:34

    PHP's gzcompress uses Zlib NOT GZIP

    public static String unzipString(String zippedText) {
        String unzipped = null;
        try {
            byte[] zbytes = zippedText.getBytes("ISO-8859-1");
            // Add extra byte to array when Inflater is set to true
            byte[] input = new byte[zbytes.length + 1];
            System.arraycopy(zbytes, 0, input, 0, zbytes.length);
            input[zbytes.length] = 0;
            ByteArrayInputStream bin = new ByteArrayInputStream(input);
            InflaterInputStream in = new InflaterInputStream(bin);
            ByteArrayOutputStream bout = new ByteArrayOutputStream(512);
            int b;
            while ((b = in.read()) != -1) {
                bout.write(b); }
            bout.close();
            unzipped = bout.toString();
        }
        catch (IOException io) { printIoError(io); }
        return unzipped;
     }
    private static void printIoError(IOException io)
    {
        System.out.println("IO Exception: " + io.getMessage());
    }
    

提交回复
热议问题