Java压缩Zlib,Gzip,Zip支持J2ME
来源:互联网 发布:军工软件 编辑:程序博客网 时间:2024/06/06 10:47
-
-
-
-
-
-
-
-
-
-
-
-
- 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;
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
- 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);
- 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;
- }
-
-
-
-
-
-
-
-
- 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;
- }
- }
-
-
-
-
-
-
-
-
- public static byte[] gZip(byte[] bContent) throws IOException
- {
-
- byte[] data = null;
- try
- {
- ByteArrayOutputStream out = new ByteArrayOutputStream();
-
- GZIPOutputStream gOut = new GZIPOutputStream(out, bContent.length);
- 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;
- }
-
-
-
-
-
-
-
-
- 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;
- }
- }
-
-
-
-
-
-
-
-
- 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;
- }
-
-
-
-
-
-
-
-
- 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);
-
-
- 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);
-
-
- 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);
-
-
- 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();
- }
-
- }
- }
- Java压缩Zlib,Gzip,Zip支持J2ME
- Android Java压缩Zlib,Gzip,Zip支持J2ME
- J2ME下的Zlib/Gzip/Zip压缩相关
- atitit.压缩算法 ZLib ,gzip ,zip 最佳实践 java .net php
- Java 字符串压缩ZIP GZIP
- gzip zip 和zlib
- ZLib,GZip,Zip压缩与解压缩工具类
- zlib,gzip,zip,7z压缩的区别
- Java zip/gzip文件压缩和解压缩
- Java zip/gzip文件压缩和解压缩
- 利用Java实现压缩与解压缩(zip、gzip)支持中文路径
- 利用Java实现压缩与解压缩(zip、gzip)支持中文路径
- Android 利用Java实现压缩与解压缩(zip、gzip)支持中文路径
- 利用Java实现压缩与解压缩(zip、gzip)支持中文路径
- Android 利用Java实现压缩与解压缩(zip、gzip)支持中文路径
- Android 利用Java实现压缩与解压缩(zip、gzip)支持中文路径
- Android 利用Java实现压缩与解压缩(zip、gzip)支持中文路径
- 利用Java实现压缩与解压缩(zip、gzip)支持中文路径
- 六一 孩童时代
- linux下查看内存使用情况
- JFileChooser如何保存特定的文件类型
- VC自动调整窗体控件大小(再也不用一个个去调整控件)
- flex与js交互(转)
- Java压缩Zlib,Gzip,Zip支持J2ME
- GDI内存泄露问题
- mysqsl优化
- J2ME -->常见笔试题(收集共享)
- 美国之行(四)
- Swing记忆之全屏模式
- Linux Context , Interrupts 和 Context Switching 说明
- u-boot及bootlload知识学习
- Top K algorithm