Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/android/207.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java Android:解压缩用PHP gzcompress()压缩的字符串_Java_Android_Gzip_Gzipinputstream - Fatal编程技术网

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

Java Android:解压缩用PHP gzcompress()压缩的字符串,java,android,gzip,gzipinputstream,Java,Android,Gzip,Gzipinputstream,如何解压缩PHP gzcompress()函数压缩的字符串 有完整的例子吗 thx 我现在是这样试的: public static String unzipString(String zippedText) throws Exception { ByteArrayInputStream bais = new ByteArrayInputStream(zippedText.getBytes("UTF-8")); GZIPInputStream gzis = new GZIPInpu

如何解压缩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)字符串,它就不起作用了

因为DEFLATE算法是gzip。

请参见


因为DEFLATE算法是gzip。

请尝试gzip输入流。请参阅和。

尝试GZIPInputStream。请参阅和。

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

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

小修正:gzip使用deflate算法,但它也添加了一些头信息(比如被压缩的文件名、文件权限),因为deflate只压缩数据。Deflate被其他工具使用,比如PNG库。次要更正:gzip使用Deflate算法,但它也添加了一些头信息(比如被压缩的文件名、文件权限),因为Deflate只压缩数据。Deflate由其他工具使用,如PNG库。当我传递Characterset RFC 1951时,它抛出错误java.io.UnsupportedEncodingException:RFC 1951当我传递Characterset RFC 1951时,它抛出错误java.io.UnsupportedEncodingException:RFC 1951