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

这一生的挚爱 提交于 2019-11-30 16:37:28

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());
}
Yoni Samlan

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

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!