java中的Zlib解压缩不起作用
我在java中的Zlib解压缩不起作用,java,php,android,compression,zlib,Java,Php,Android,Compression,Zlib,我在PHP5.4.4-14+deb7u7中使用 $cdat = gzcompress($dat, 9); 然后在android/java中,我想从这里解压它: 我正在使用: public static String unzipString(String zippedText) { String unzipped = null; try { byte[] zbytes = zippedText.getBytes("ISO-8859-1"); /
PHP5.4.4-14+deb7u7
中使用
$cdat = gzcompress($dat, 9);
然后在android/java中,我想从这里解压它:
我正在使用:
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);
}
公共静态对GetHTTPResponse(字符串url,列表url参数){
字符串responseVal=null;
int-responseCode=0;
试一试{
HttpParams httpParameters=新的BasicHttpParams();
int timeoutConnection=TIMEOUT_SECONDS*1000;
HttpConnectionParams.setConnectionTimeout(httpParameters,timeoutConnection);
int timeoutSocket=超时时间×秒*1000;
HttpConnectionParams.setSoTimeout(httpParameters,timeoutSocket);
HttpClient=新的默认HttpClient(httpParameters);
HttpPost HttpPost=新的HttpPost(url);
setEntity(新的UrlEncodedFormEntity(urlparameters));
HttpResponse response=client.execute(httppost);
responseCode=response.getStatusLine().getStatusCode();
BufferedReader rd=新的BufferedReader(新的InputStreamReader(response.getEntity().getContent());
responseVal=Common.GetStringFromBufferedReader(rd);
Log.d(“服务器”,responseVal);
}
捕获(例外e){
响应代码=0;
}
if(responseVal!=null){
responseVal=Common.unzipString(responseVal);
}
返回新对(responseVal、responseCode);
}
您不能使用
BufferedReader rd =
new BufferedReader(new InputStreamReader(
response.getEntity().getContent()));
responseVal = Common.GetStringFromBufferedReader(rd);
正如Javadoc所指出的
InputStreamReader
是从字节流到字符流的桥梁:它读取字节并使用指定的字符集将其解码为字符
相反,你可以使用
然后,您可以直接将内容以字节[]
的形式传递给函数,并且永远不要默默地吞下异常
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;
}
它是二进制的,不是字符串
。那么我如何解决这个问题呢;当您将其转换为字符串时
将其从二进制数据更改为字符数据。好的,我添加了下面的函数,它将下载并调用解压函数。
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;
}