java - Android:解压缩用 PHP gzcompress() 压缩的字符串

标签 java android gzip gzipinputstream

我如何解压缩由 PHP gzcompress() 函数压缩的字符串?

有完整的例子吗?

谢谢

我现在这样试过:

public static String unzipString(String zippedText) throws Exception
{
    ByteArrayInputStream bais = new ByteArrayInputStream(zippedText.getBytes("UTF-8"));
    GZIPInputStream gzis = new GZIPInputStream(bais);
    InputStreamReader reader = new InputStreamReader(gzis);
    BufferedReader in = new BufferedReader(reader);

    String unzipped = "";
    while ((unzipped = in.readLine()) != null) 
        unzipped+=unzipped;

    return unzipped;
}

但如果我尝试解压缩 PHP gzcompress (-ed) 字符串,它就无法正常工作。

最佳答案

PHP 的 gzcompress 使用 Zlib 而不是 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());
}

关于java - Android:解压缩用 PHP gzcompress() 压缩的字符串,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/4327517/

相关文章:

java - 如何使用 JSON 映射 Java 嵌入对象

java - 区分等效数据类型

java - 如何在java上编写伪代码来解决这个问题?

java - 带有jlist的keylistener

apache-spark - 如何在 Spark 中读取没有扩展名的压缩 (gzip) 文件

python - IOError: CRC 检查失败 0xffca51ff != 0x3679ba0L

android - 相机拍摄图像时如何设置图像垂直?

android - 无法从 Android 菜单中隐藏应用程序图标?

java - 如何暂停 Android 应用程序的自动滚动?

http - 服务器如何知道返回压缩数据?