httpclientでWebページをキャプチャする際のgzip、deflateの解凍
2155 ワード
httpclientでWebページをキャプチャする場合、自動的にデータを解凍することはなく、自分で解凍する必要があります.
// ,content_encoding = "deflate" :
protected byte[] defalteUnCompress(byte[] src)throws Exception{
ByteArrayInputStream byteIn = new ByteArrayInputStream(src);
ByteArrayOutputStream byteOut = new ByteArrayOutputStream();
InflaterInputStream gzipIn = null;
byte[] content = new byte[1024];
int readLen = 0;
try{
Inflater inf = new Inflater(true);
// , new InflaterInputStream(in)
gzipIn = new InflaterInputStream(byteIn,inf);
while ((readLen = gzipIn.read(content)) != -1) {
byteOut.write(content, 0, readLen);
}
return byteOut.toByteArray();
}catch(Exception e){
throw e;
}finally{
try{
if(byteIn != null){
byteIn.close();
byteIn = null;
}
if(byteOut != null){
byteOut.close();
byteOut = null;
}
if(gzipIn != null){
gzipIn.close();
gzipIn = null;
}
}catch(Exception e){}
}
}
// ,content_encoding = "gzip,deflate" :
protected byte[] gzipUnCompress(byte[] src)throws Exception{
ByteArrayInputStream byteIn = new ByteArrayInputStream(src);
ByteArrayOutputStream byteOut = new ByteArrayOutputStream();
GZIPInputStream gzipIn = null;
byte[] content = new byte[1024];
int readLen = 0;
try{
gzipIn = new GZIPInputStream(byteIn);
while ((readLen = gzipIn.read(content)) != -1) {
byteOut.write(content, 0, readLen);
}
return byteOut.toByteArray();
}catch(Exception e){
throw e;
}finally{
try{
if(byteIn != null){
byteIn.close();
byteIn = null;
}
if(byteOut != null){
byteOut.close();
byteOut = null;
}
if(gzipIn != null){
gzipIn.close();
gzipIn = null;
}
}catch(Exception e){}
}
}