ZLib,GZip,Zip压缩与解压缩工具类

原创 2010年01月27日 11:32:00

package test;

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;

/**
 * 压缩工具包
 */
public class ZipUtil {

 // 输入数据的最大长度
 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 = "干哈捡垃圾改了倒是几啊该近哦怎么冷静斯洛克发生过年萨嘎丝巾官商勾结撒该赏菊管理局进苦力哈哈四欧洲女方家萨嘎嘎嘎怒日颇为勤能补拙个少普宁该萨嘎为此该萨嘎年经普年均可与飞洒你亚冠的丧尽和";
   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();
  }

 }
}

 

相关文章推荐

Java实现的ZIP解压缩工具类

[代码] [Java]代码 view source print? 001 package com.lanp; 002 003 import java.io.File; 004 import ja...

Android Zip解压缩工具类

package com.example.zip.commons.utils;import android.util.Log;import java.io.File; import java.io.Fi...

java解压缩工具类

啥也不说了,直接上代码,使用很方便,不过需要添加依赖jar包 ant ant 1.6.5 代码如下package com.jd.heads.common.utils; import java....

RDIFramework.NET ━ .NET快速信息化系统开发框架 V3.2 新增解压缩工具类ZipHelper

在项目对文件进行解压缩是非常常用的功能,对文件进行压缩存储或传输可以节省流量与空间。压缩文件的格式与方法都比较多,比较常用的国际标准是zip格式。压缩与解压缩的方法也很多,在.NET 2.0开始,在S...

ZLib 解压缩工具

官网:http://www.zlib.net/ 功能:压缩一段字节流,但是不包含任何文件信息。所以如果要编写压缩数据,还要自定义头部信息之类的,自己生成对应的文件结构 设计:CMake编译工具(用...

Java压缩、解压缩工具类(Ant)

import java.io.BufferedInputStream; import java.io.BufferedOutputStream; import java.io.File; imp...

ziputils java压缩/解压缩工具类

package com.eluotuo.common.util; import java.io.BufferedInputStream; import java.io.BufferedOutputS...

ZipUtil 历史上最好用的 压缩解压缩工具类 附代码

package com.example.a; import java.io.BufferedReader; import java.io.File; import java.io.FileInput...

解压缩工具类

  • 2017年10月26日 09:44
  • 5KB
  • 下载

工具(四):Java压缩解压缩工具类

package com.wdy.tools.utils.pressuitl; import java.io.BufferedInputStream; import java.io.BufferedO...
内容举报
返回顶部
收藏助手
不良信息举报
您举报文章:ZLib,GZip,Zip压缩与解压缩工具类
举报原因:
原因补充:

(最多只允许输入30个字)