2010-12-01 153 views
1

如何解压缩由PHP gzcompress()函数压缩的字符串?Android:使用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)字符串它不工作。

回答

7

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

当我通过Characterset RFC 1951时,它会引发错误java.io.UnsupportedEncodingException:RFC 1951 – 2014-03-22 11:50:12

相关问题