IT_Programming/Android_Java

Android(Java) httpClient + zlib(zip)

JJun ™ 2011. 11. 27. 06:58

------------------------------------------------------------------------------------------------

 출처: http://hako.04p.kr/?cat=3

------------------------------------------------------------------------------------------------

 

안드로이드가 서버에서 zlib(zip)으로 압축된 데이터를 받아 올 때
압축된 “파일”이 아니라 압축된 “문자열”이기 때문에 ZipInputStream을 사용하는 게 아니라,
    Inflater를 사용한다 !!

  

 

HttpPost httpPost = new HttpPost("http://hako.04p.kr/");
List<NameValuePair> nameValuePairs = new ArrayList<NameValuePair>(2);
nameValuePairs.add(new BasicNameValuePair("parameter1", "11"));
nameValuePairs.add(new BasicNameValuePair("parameter2", "22"));
httpPost.setEntity(new UrlEncodedFormEntity(nameValuePairs));
HttpResponse httpResponse = httpClient.execute(httpPost);
HttpEntity httpEntity = httpResponse.getEntity();
InputStream inputStream = httpEntity.getContent();
ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream();
while (true) {
	byte[] temp = new byte[8 * 1024];
	int readLength = inputStream.read(temp);
	if (readLength < 0) break;
	byteArrayOutputStream.write(temp, 0, readLength);
}
inputStream.close();
Inflater decompressor = new Inflater();
decompressor.setInput(byteArrayOutputStream.toByteArray());
byte[] buffer = new byte[1024 * 1024];
while(!decompressor.finished()) {
	decompressor.inflate(buffer);
}
String decompressedString = new String(buffer);