Android:解压用PHP gzcompress()压缩的字符串。[英] Android: decompress string that was compressed with PHP gzcompress()

本文是小编为大家收集整理的关于Android:解压用PHP gzcompress()压缩的字符串。的处理/解决方法,可以参考本文帮助大家快速定位并解决问题,中文翻译不准确的可切换到English标签页查看源文。

问题描述

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

有任何完整示例?

thx

我现在像这样尝试了:

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

其他推荐答案

尝试GZIPINPUTSTREAM.请参阅 example .com/Question/3627401/gzipinputStream-to-string/3627442#3627442">这个问题.

其他推荐答案

util/zip/offaterinputstream.html

由于Deflate算法为GZIP.

本文地址:https://www.itbaoku.cn/post/102730.html

问题描述

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

Any full examples?

thx

I tried it now like this:

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;
}

but it's not working if i i'm trying to unzip a PHP gzcompress (-ed) string.

推荐答案

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

其他推荐答案

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

其他推荐答案

See

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

since the DEFLATE algorithm is gzip.