Android Java压缩Zlib,Gzip,Zip支持J2ME
2010-11-05 00:54:54 来源:WEB开发网// /** Default compression */
// public static final int COMPRESSION_DEFAULT =
// JZlib.Z_DEFAULT_COMPRESSION;
/**
* ZLib压缩数据
*
* @param object
* @return
* @throws IOException
*/
public static byte[] zLib(byte[] bContent) throws IOException
{
byte[] data = null;
try
{
ByteArrayOutputStream out = new ByteArrayOutputStream();
ZOutputStream zOut = new ZOutputStream(out,
JZlib.Z_BEST_COMPRESSION); // 压缩级别,缺省为1级
DataOutputStream objOut = new DataOutputStream(zOut);
objOut.write(bContent);
objOut.flush();
zOut.close();
data = out.toByteArray();
out.close();
}
catch (IOException e)
{
e.printStackTrace();
throw e;
}
return data;
}
/**
* ZLib解压数据
*
* @param object
* @return
* @throws IOException
*/
public static byte[] unZLib(byte[] bContent) throws IOException
{
byte[] data = new byte[MAXLENGTH];
try
{
ByteArrayInputStream in = new ByteArrayInputStream(bContent);
ZInputStream zIn = new ZInputStream(in);
DataInputStream objIn = new DataInputStream(zIn);
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();
zIn.close();
in.close();
return trueData;
}
catch (IOException e)
{
e.printStackTrace();
更多精彩
赞助商链接