2014-11-09 111 views
1

我壓縮串PHP 5.4.4-14+deb7u7使用的zlib壓縮在java中不工作

$cdat = gzcompress($dat, 9); 

http://php.net/manual/en/function.gzcompress.php

然後在安卓/ JAVA我想解壓,從這裏開始: Android: decompress string that was compressed with PHP gzcompress()

我正在使用:

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 e) { 
    } 
    return unzipped; 
} 

但是當我嘗試它時,它解壓縮成一個空字符串,當在android下載的壓縮字符串非常長。

下載的串像

x�͜{o�8�a`�= �!�����[��K!(6c�E�$��]�)�HF��F\!����ə���L�LNnH]Lj٬T��M���f�'�u#�*_�7'�S^�w��*kڼn�Yޚ�I��e$.1C��~�ݟ��F�A�_Mv_�R͋��ܴ�Z^L���sU?A���?�׮�ZVmֽ6��>�B��C�M�*����^�sٸ�j����������?�"_�j�ܣY�E���h0�g��w[=&�D �oht=>�l�?��Po";`.�e�E�E��[���������sq��0���i]��������zUL�O{П��ժ�k��b�.&7��-d1_��ۣ�獮�y���=F��K!�rC�{�$����c�&9ޣH���n�x� 

有誰知道問題是什麼?

謝謝。

public static Pair<String,Integer> GetHTTPResponse(String url, List<NameValuePair> urlparameters) { 
    String responseVal = null; 
    int responseCode = 0; 

    try { 
     HttpParams httpParameters = new BasicHttpParams(); 
     int timeoutConnection = TIMEOUT_SECONDS * 1000; 
     HttpConnectionParams.setConnectionTimeout(httpParameters, timeoutConnection); 
     int timeoutSocket = TIMEOUT_SECONDS * 1000; 
     HttpConnectionParams.setSoTimeout(httpParameters, timeoutSocket); 

     HttpClient client = new DefaultHttpClient(httpParameters); 
     HttpPost httppost = new HttpPost(url); 

     httppost.setEntity(new UrlEncodedFormEntity(urlparameters)); 
     HttpResponse response = client.execute(httppost); 
     responseCode = response.getStatusLine().getStatusCode();  

     BufferedReader rd = new BufferedReader(new InputStreamReader(response.getEntity().getContent())); 

     responseVal = Common.GetStringFromBufferedReader(rd); 

     Log.d("SERVER", responseVal); 
    } 
    catch (Exception e) { 
     responseCode = 0; 
    } 

    if (responseVal != null) { 
     responseVal = Common.unzipString(responseVal); 
    } 

    return new Pair<String, Integer>(responseVal, responseCode); 
} 
+0

它是二進制的而不是'String'。 – 2014-11-09 07:10:35

+0

那麼我該如何解決這個問題? – omega 2014-11-09 07:11:45

+0

發佈您如何從PHP獲得響應;當你將它轉換爲String時,你可以將它從二進制文件改爲字符數據。 – 2014-11-09 07:13:28

回答

1

不能使用

BufferedReader rd = 
    new BufferedReader(new InputStreamReader(
     response.getEntity().getContent())); 
responseVal = Common.GetStringFromBufferedReader(rd); 

由於InputStreamReader的Javadoc中指出,

一個InputStreamReader是字節流通向字符流的橋樑:它讀取字節,對其進行解碼轉換爲使用指定的charset的字符。

相反,你可以使用HttpEntity.writeTo(OutputStream)ByteArrayOutputStream

ByteArrayOutputStream baos = new ByteArrayOutputStream(); 
response.getEntity().writeTo(baos); 
byte[] content = baos.toByteArray(); 

然後你就可以將內容直接傳遞到你的函數在byte[]從未默默吞下Exception

public static String unzipString(byte[] zbytes) { 
    String charsetName = "ISO-8859-1"; 
    String unzipped = null; 
    try { 
     // 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(charsetName); 
    } catch (IOException e) { 
     e.printStackTrace(); 
    } 
    return unzipped; 
} 
+0

這很好。謝謝。 – omega 2014-11-09 07:35:47