Android Java壓縮Zlib,Gzip,Zip支援J2ME

l_serein發表於2012-07-12

Java程式碼

/*

* 檔名: ZipUtil.java

* 版權: xxxxxxxx.com. Copyright 1999-2010, All rights reserved

* 描述: 是壓縮工具類,此類根據com.jcraft.jzlib地三方提供的核心類進行.壓縮和解壓縮。

* 修改人:

* 修改時間: 2010-09-13

* 跟蹤單號:

* 修改單號:

* 修改內容: 新增

可以到google是去下載jzlib4me20100516.rar 也就是jzlib4me的google專案為第三方支援包.

這個ZipUtil.java的zlib支援J2ME.也就是將zlib的壓縮和解壓縮的兩個方法可以放到J2ME專案中.但也需要jzlib4me20100516.rar包.

*/

package com.temobi.ms.util;

import java.io.ByteArrayInputStream;

import java.io.ByteArrayOutputStream;

import java.io.DataInputStream;

import java.io.DataOutputStream;

import java.io.IOException;

import java.util.zip.GZIPInputStream;

import java.util.zip.GZIPOutputStream;

import java.util.zip.ZipEntry;

import java.util.zip.ZipInputStream;

import java.util.zip.ZipOutputStream;

import com.jcraft.jzlib.JZlib;

import com.jcraft.jzlib.ZInputStream;

import com.jcraft.jzlib.ZOutputStream;

import com.temobi.ms.resource.ConfigRes;

import com.temobi.ms.resource.Const;

/**

* 壓縮工具包

*/

public class ZipUtil

{

public static byte[] deflate(byte[] bContent)

{

MapServerLog.beforeMethod();

try

{

String sys_compress = ConfigRes.getInstance().get(Const.SYS_COMPRESS);

byte[] temp = null;

if("LZIP".equals(sys_compress))

{

temp = ZipUtil.zLib(bContent);

}

else

if("GZIP".equals(sys_compress))

{

temp = ZipUtil.gZip(bContent);

}

else

if("ZIP".equals(sys_compress))

{

temp = ZipUtil.zip(bContent);

}

MapServerLog.afterMethod();

return temp;

}

catch (IOException e)

{

MapServerLog.exceptionMethod(e);

e.printStackTrace();

}

MapServerLog.afterMethod();

return null;

}

public static byte[] inflate(byte[] bContent)

{

MapServerLog.beforeMethod();

try

{

String sys_compress = ConfigRes.getInstance().get(Const.SYS_COMPRESS);

byte[] temp = null;

if("LZIP".equals(sys_compress))

{

temp = ZipUtil.unZLib(bContent);

}

else

if("GZIP".equals(sys_compress))

{

temp = ZipUtil.unGZip(bContent);

}

else

if("ZIP".equals(sys_compress))

{

temp = ZipUtil.unZip(bContent);

}

MapServerLog.afterMethod();

return temp;

}

catch (IOException e)

{

MapServerLog.exceptionMethod(e);

e.printStackTrace();

}

MapServerLog.afterMethod();

return null;

}

// 輸入資料的最大長度

private static final int MAXLENGTH = 102400;

// 設定快取大小

private static final int BUFFERSIZE = 1024;

// 壓縮選擇方式:

//

// /** Try o get the best possible compression */

// public static final int COMPRESSION_MAX = JZlib.Z_BEST_COMPRESSION;

//

// /** Favor speed over compression ratio */

// public static final int COMPRESSION_MIN = JZlib.Z_BEST_SPEED;

//

// /** No compression */

// public static final int COMPRESSION_NONE = JZlib.Z_NO_COMPRESSION;

//

// /** 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();

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

*

* @param data

* @return

* @throws IOException

*/

public static byte[] zip(byte[] bContent) throws IOException

{

byte[] b = null;

try

{

ByteArrayOutputStream bos = new ByteArrayOutputStream();

ZipOutputStream zip = new ZipOutputStream(bos);

ZipEntry entry = new ZipEntry("zip");

entry.setSize(bContent.length);

zip.putNextEntry(entry);

zip.write(bContent);

zip.closeEntry();

zip.close();

b = bos.toByteArray();

bos.close();

}

catch (Exception ex)

{

ex.printStackTrace();

}

return b;

}

/***

* 解壓Zip

*

* @param data

* @return

* @throws IOException

*/

public static byte[] unZip(byte[] bContent) throws IOException

{

byte[] b = null;

try

{

ByteArrayInputStream bis = new ByteArrayInputStream(bContent);

ZipInputStream zip = new ZipInputStream(bis);

while (zip.getNextEntry() != null)

{

byte[] buf = new byte[1024];

int num = -1;

ByteArrayOutputStream baos = new ByteArrayOutputStream();

while ((num = zip.read(buf, 0, buf.length)) != -1)

{

baos.write(buf, 0, num);

}

b = baos.toByteArray();

baos.flush();

baos.close();

}

zip.close();

bis.close();

}

catch (Exception ex)

{

ex.printStackTrace();

}

return b;

}

public static void main(String[] args)

{

String newContent = "";

try

{

String content =

"水電費his大家fks打飛機速度快放假了速度快放假速度發生的飛機上的考慮防靜電速度開飛機上開啟了房間速度快讓他檔案";

System.out.println(content);

byte[] origin = content.getBytes();

System.out.println("原始長度 length is : " + origin.length);

// ZLib 壓縮

byte[] zLibCnt = zLib(origin);

System.out.println("zLib壓縮後長度 : " + zLibCnt.length);

byte[] unzLibCnt = unZLib(zLibCnt);

System.out.println("zLib解壓後長度 : " + unzLibCnt.length);

newContent = new String(unzLibCnt);

System.out.println(newContent);

// GZip 壓縮

byte[] gZipCnt = gZip(origin);

System.out.println("GZip壓縮後長度 : " + gZipCnt.length);

byte[] ungZipCnt = unGZip(gZipCnt);

System.out.println("GZip解壓後長度 : " + ungZipCnt.length);

newContent = new String(ungZipCnt);

System.out.println(newContent);

// Zip 壓縮

byte[] zipCnt = zip(origin);

System.out.println("Zip壓縮後長度 : " + zipCnt.length);

byte[] unZipCnt = unZip(zipCnt);

System.out.println("Zip解壓後長度 : " + unZipCnt.length);

newContent = new String(unZipCnt);

System.out.println(newContent);

}

catch (Exception e)

{

e.printStackTrace();

}

}

}

相關文章