`
thb143
  • 浏览: 8443 次
文章分类
社区版块
存档分类
最新评论

java对byte数组解压缩(zip,gzip,bzip2,jzlib)

 
阅读更多
//导入bzip2.jar和jzlib相关java文件

import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
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 org.apache.tools.bzip2.CBZip2InputStream;
import org.apache.tools.bzip2.CBZip2OutputStream;

import com.jcraft.jzlib.JZlib;
import com.jcraft.jzlib.ZInputStream;
import com.jcraft.jzlib.ZOutputStream;

public class Test {

	/***
	 * 压缩GZip
	 * 
	 * @param data
	 * @return
	 */
	public static byte[] gZip(byte[] data) {
		byte[] b = null;
		try {
			ByteArrayOutputStream bos = new ByteArrayOutputStream();
			GZIPOutputStream gzip = new GZIPOutputStream(bos);
			gzip.write(data);
			gzip.finish();
			gzip.close();
			b = bos.toByteArray();
			bos.close();
		} catch (Exception ex) {
			ex.printStackTrace();
		}
		return b;
	}

	/***
	 * 解压GZip
	 * 
	 * @param data
	 * @return
	 */
	public static byte[] unGZip(byte[] data) {
		byte[] b = null;
		try {
			ByteArrayInputStream bis = new ByteArrayInputStream(data);
			GZIPInputStream gzip = new GZIPInputStream(bis);
			byte[] buf = new byte[1024];
			int num = -1;
			ByteArrayOutputStream baos = new ByteArrayOutputStream();
			while ((num = gzip.read(buf, 0, buf.length)) != -1) {
				baos.write(buf, 0, num);
			}
			b = baos.toByteArray();
			baos.flush();
			baos.close();
			gzip.close();
			bis.close();
		} catch (Exception ex) {
			ex.printStackTrace();
		}
		return b;
	}

	/***
	 * 压缩Zip
	 * 
	 * @param data
	 * @return
	 */
	public static byte[] zip(byte[] data) {
		byte[] b = null;
		try {
			ByteArrayOutputStream bos = new ByteArrayOutputStream();
			ZipOutputStream zip = new ZipOutputStream(bos);
			ZipEntry entry = new ZipEntry("zip");
			entry.setSize(data.length);
			zip.putNextEntry(entry);
			zip.write(data);
			zip.closeEntry();
			zip.close();
			b = bos.toByteArray();
			bos.close();
		} catch (Exception ex) {
			ex.printStackTrace();
		}
		return b;
	}

	/***
	 * 解压Zip
	 * 
	 * @param data
	 * @return
	 */
	public static byte[] unZip(byte[] data) {
		byte[] b = null;
		try {
			ByteArrayInputStream bis = new ByteArrayInputStream(data);
			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;
	}

	/***
	 * 压缩BZip2
	 * 
	 * @param data
	 * @return
	 */
	public static byte[] bZip2(byte[] data) {
		byte[] b = null;
		try {
			ByteArrayOutputStream bos = new ByteArrayOutputStream();
			CBZip2OutputStream bzip2 = new CBZip2OutputStream(bos);
			bzip2.write(data);
			bzip2.flush();
			bzip2.close();
			b = bos.toByteArray();
			bos.close();
		} catch (Exception ex) {
			ex.printStackTrace();
		}
		return b;
	}

	/***
	 * 解压BZip2
	 * 
	 * @param data
	 * @return
	 */
	public static byte[] unBZip2(byte[] data) {
		byte[] b = null;
		try {
			ByteArrayInputStream bis = new ByteArrayInputStream(data);
			CBZip2InputStream bzip2 = new CBZip2InputStream(bis);
			byte[] buf = new byte[1024];
			int num = -1;
			ByteArrayOutputStream baos = new ByteArrayOutputStream();
			while ((num = bzip2.read(buf, 0, buf.length)) != -1) {
				baos.write(buf, 0, num);
			}
			b = baos.toByteArray();
			baos.flush();
			baos.close();
			bzip2.close();
			bis.close();
		} catch (Exception ex) {
			ex.printStackTrace();
		}
		return b;
	}

	/**
	 * 把字节数组转换成16进制字符串
	 * 
	 * @param bArray
	 * @return
	 */
	public static String bytesToHexString(byte[] bArray) {
		StringBuffer sb = new StringBuffer(bArray.length);
		String sTemp;
		for (int i = 0; i < bArray.length; i++) {
			sTemp = Integer.toHexString(0xFF & bArray[i]);
			if (sTemp.length() < 2)
				sb.append(0);
			sb.append(sTemp.toUpperCase());
		}
		return sb.toString();
	}

	/**
	 * 压缩数据
	 * 
	 * @param object
	 * @return
	 * @throws IOException
	 */
	public static byte[] jzlib(byte[] object) {

		byte[] data = null;
		try {
			ByteArrayOutputStream out = new ByteArrayOutputStream();
			ZOutputStream zOut = new ZOutputStream(out,
					JZlib.Z_DEFAULT_COMPRESSION);
			DataOutputStream objOut = new DataOutputStream(zOut);
			objOut.write(object);
			objOut.flush();
			zOut.close();
			data = out.toByteArray();
			out.close();

		} catch (IOException e) {
			e.printStackTrace();
		}
		return data;
	}

	/**
	 * 解压被压缩的数据
	 * 
	 * @param object
	 * @return
	 * @throws IOException
	 */
	public static byte[] unjzlib(byte[] object) {

		byte[] data = null;
		try {
			ByteArrayInputStream in = new ByteArrayInputStream(object);
			ZInputStream zIn = new ZInputStream(in);
			byte[] buf = new byte[1024];
			int num = -1;
			ByteArrayOutputStream baos = new ByteArrayOutputStream();
			while ((num = zIn.read(buf, 0, buf.length)) != -1) {
				baos.write(buf, 0, num);
			}
			data = baos.toByteArray();
			baos.flush();
			baos.close();
			zIn.close();
			in.close();

		} catch (IOException e) {
			e.printStackTrace();
		}
		return data;
	}

	public static void main(String[] args) {
		String s = "this is a test";

		byte[] b1 = zip(s.getBytes());
		System.out.println("zip:" + bytesToHexString(b1));
		byte[] b2 = unZip(b1);
		System.out.println("unZip:" + new String(b2));

		byte[] b3 = bZip2(s.getBytes());
		System.out.println("bZip2:" + bytesToHexString(b3));
		byte[] b4 = unBZip2(b3);
		System.out.println("unBZip2:" + new String(b4));

		byte[] b5 = gZip(s.getBytes());
		System.out.println("bZip2:" + bytesToHexString(b5));
		byte[] b6 = unGZip(b5);
		System.out.println("unBZip2:" + new String(b6));

		byte[] b7 = jzlib(s.getBytes());
		System.out.println("jzlib:" + bytesToHexString(b7));
		byte[] b8 = unjzlib(b7);
		System.out.println("unjzlib:" + new String(b8));
	}
}
分享到:
评论

相关推荐

    Java 解压缩 bzip2.jar

    Java 解压缩 bzip2.jar

    Keka-1.1.26.dmg keka: 1.1.26 可以创建以下格式的压缩文件:7Z ZIP TAR GZIP BZIP2 XZ LZIP DMG ISO

    7Z ZIP TAR GZIP BZIP2 XZ LZIP DMG ISO 并支持解压这些格式: 7Z ZIP ZIPX RAR TAR GZIP BZIP2 XZ LZIP DMG ISO LZMA EXE CAB WIM PAX JAR WAR IPA APK APPX XPI CPGZ 便捷且强大 您在使用时甚至无需打开 ...

    压缩解压缩工具Bzip2源码

    当你需要从包含了一些错误数据的备份磁盘上修复或者获得含有错误数据的压缩文件中的数据时,bzip2仍能完美的解压出没有被破坏的部分。  4、和gzip的用法类似,最简单的打包压缩命令就是“bzip2 【要打包压缩的文件名...

    GZip,BZip2,Zip对DataTable,DataSet压缩转换XML

    使用GZip,BZip2,Zip对DataTable,DataSet压缩转换为XML C#自己做的

    C# 压缩与解压缩完整源码,支持ZIP,RAR,7Z,WinZip,BZip2等

    C# 压缩与解压缩完整源码,支持ZIP,RAR,7Z,WinZip,BZip2等,可以支持压缩批量文件,大文件,文件夹。支持解压单个文件,批量文件,文件夹等。完整源码,本项目包含三个子项目,分别是压缩与解压缩应用项目,压缩源码...

    FiveStarZip 压缩解压神器 v5.4.11.21248 x64 完全自主开发的自由软件

    FiveStarZip 压缩解压神器 v5.4.11.21248 x64 完全自发布者主开发的自由的...支持7z zip Gzip Bzip2 tar XZ文件的压缩和 rar zip 7z Gzip Bzip2 tar XZ文件的解压缩。本版本修改了界面主题。软件完全免费,请放心使用。

    bzip2-1.0.5-bin.zip

    bzip2库下载:bzlib.h;bzip2.lib;libbz2.dll.a;bzip2-bcc.lib

    含有zip.lzw.gzip等多种压缩算法的程序

    含有zip.lzw.gzip等多种压缩算法的程序 含有zip.lzw.gzip等多种压缩算法的程序 含有zip.lzw.gzip等多种压缩算法的程序

    压缩及解压缩软件7-Zip9.03

     压缩及解压缩:7z、ZIP、GZIP、BZIP2 和 TAR (目前不支持RAR格式,是因为RAR是一种商业格式)  仅解压缩:RAR、CAB、ISO、ARJ、LZH、CHM、WIM、Z、CPIO、RPM、DEB 和 NSIS  对于 ZIP 及 GZIP 格式,7-Zip 能提供...

    bzip2.jar及java源码

    bzip2.rar中包括bzip2.jar和java源代码文件

    bzip2.zip_RPM安装包

    Bzip2工具的RPM安装包,包含 bzip2-1.0.6-13.el7.x86_64 bzip2-devel-1.0.6-13.el7.x86_64 bzip2-libs-1.0.6-13.el7.x86_64

    7-Zip 现今最高解压缩比压缩软件

    支持格式: 压缩及解压缩:7z、ZIP、GZIP、BZIP2 和 TAR 仅解压缩:RAR、CAB、ISO、ARJ、LZH、CHM、Z、CPIO、RPM、DEB 和 NSIS - 对于 ZIP 及 GZIP 格式,7-Zip 能提供比使用 PKZip 及 WinZip 高 2-10% 的...

    ZArchiver解压缩工具0.8.2

    &gt; 解压缩格式为: 7z (7zip), zip, rar, bzip2, gzip, XZ, iso, tar, arj, cab, LZH, LZMA, xar, tgz, tbz, Z, deb, rpm的压缩文件; &gt; 查看格式为:7z (7zip), zip, rar, bzip2, gzip, XZ, iso, tar, arj, cab, LZH...

    压缩比例最高的解压缩软件7-Zip 4.46 Beta1简体中文版

    &lt;br&gt;主要特征: 更新了算法来加大 7z 格式 的压缩比 7-Zip 是基于 GNU LGPL 之下发布的软件 支持格式: 压缩及解压缩:7z、ZIP、GZIP、BZIP2 和 TAR 仅解压缩:RAR、CAB、ISO、ARJ、LZH、CHM...

    Android 解压缩工具 ZArchiver Pro 0.9.5.8 中文多语免费版.zip

    解压缩以下存档类型:7z(7zip),zip,rar,rar5,bzip2,gzip,XZ,iso,tar,arj,cab,lzh,lha,lzma,xar,tgz,tbz,Z,deb,rpm,zipx ,mtz,chm,dmg,cpio,cramfs,img(fat,ntfs,ubf),wim,ecm,...

    linux解压缩命令-Linux命令转发记录

    tar 是Linux中最常用的打包工具,它本身不具有压缩功能,但可以调用其他压缩工具(如gzip、bzip2等)来实现压缩和解压。 解压命令格式:tar -xvf 压缩文件名。其中,-x 表示解压,-v 表示显示过程,-f 指定压缩...

    bzip2-1.0.5升级bzip2-1.0.6

    针对 报错 bzip2 版本低 进行修复升级 checking if bzip2 version &gt;= 1.0.6... no checking whether bzip2 support suffices... configure: error: bzip2 library and headers are required

    bzip2命令 bz2文件的压缩程序

    Linux系统中bzip2命令的英文是“bunzip2”,即.bz2文件格式的压缩程序; bzip2命令系统默认是...bzip2在压缩或解压缩时,若输出文件与现有文件同名,预设不会覆盖现有文件。若要覆盖,请使用此参数 -k bzip2在压缩

    7-Zip.zip强大的压缩解压工具

     支持对7z, XZ, BZIP2, GZIP, TAR, ZIP and WIM等格式的压缩/解压缩!  仅解压缩:ARJ, CAB, CHM, CPIO, CramFS, DEB, DMG, FAT, HFS, ISO, LZH, LZMA, MBR, MSI, NSIS, NTFS, RAR, RPM, SquashFS, UDF, VHD, WIM,...

Global site tag (gtag.js) - Google Analytics