Zlib在java中解压缩不起作用

时间:2014-11-09 07:06:59

标签: java php android compression zlib

我正在使用

压缩PHP 5.4.4-14+deb7u7中的字符串
$cdat = gzcompress($dat, 9);

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

然后在android / 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);
}

1 个答案:

答案 0 :(得分: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;
 }