Android Java压缩Zlib,Gzip,Zip支持J2ME
2010-11-05 00:54:54 来源:WEB开发网throw e;
}
}
/**
* GZip压缩数据
*
* @param object
* @return
* @throws IOException
*/
public static byte[] gZip(byte[] bContent) throws IOException
{
byte[] data = null;
try
{
ByteArrayOutputStream out = new ByteArrayOutputStream();
GZIPOutputStream gOut = new GZIPOutputStream(out, bContent.length); // 压缩级别,缺省为1级
DataOutputStream objOut = new DataOutputStream(gOut);
objOut.write(bContent);
objOut.flush();
gOut.close();
data = out.toByteArray();
out.close();
}
catch (IOException e)
{
e.printStackTrace();
throw e;
}
return data;
}
/**
* GZip解压数据
*
* @param object
* @return
* @throws IOException
*/
public static byte[] unGZip(byte[] bContent) throws IOException
{
byte[] data = new byte[MAXLENGTH];
try
{
ByteArrayInputStream in = new ByteArrayInputStream(bContent);
GZIPInputStream pIn = new GZIPInputStream(in);
DataInputStream objIn = new DataInputStream(pIn);
int len = 0;
int count = 0;
while ((count = objIn.read(data, len, len + BUFFERSIZE)) != -1)
{
len = len + count;
}
byte[] trueData = new byte[len];
System.arraycopy(data, 0, trueData, 0, len);
objIn.close();
pIn.close();
in.close();
return trueData;
}
catch (IOException e)
{
e.printStackTrace();
throw e;
}
}
/***
* 压缩Zip
赞助商链接