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

前端 未结 3 1181
别跟我提以往
别跟我提以往 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());
    }
    
    0 讨论(0)
  • 2021-01-03 15:34

    Try a GZIPInputStream. See this example and this SO question.

    0 讨论(0)
  • 2021-01-03 15:43

    See

    http://developer.android.com/reference/java/util/zip/InflaterInputStream.html

    since the DEFLATE algorithm is gzip.

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