码迷,mamicode.com
首页 > 编程语言 > 详细

Java解压缩技术(二)GZIP压缩-解压缩(

时间:2015-04-14 16:44:11      阅读:198      评论:0      收藏:0      [点我收藏+]

标签:bzip2   gzip   java压缩   zip   数据压缩   


Java解压缩技术的实现 GZIP ZIP BZIP2


没啥好说的,都是些文件IO操作


package com.ljh.gzip;

import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.util.zip.GZIPInputStream;
import java.util.zip.GZIPOutputStream;

/**
 * @Desc: GZIP 压缩工具(数据压缩建议使用,支持单文件压缩)
 * 要压缩文件夹请参考ZipUtils类
 * Tips:GZIP也可以采用Apache开源的Commons-Compress.jar的实现
 * @author ljh
 * @date 2015-4-14 上午9:39:14
 */
public class GZipUtils {
	private static final int BUFFER = 1024;
	public static final String EXT = ".gz";

	/**
	 * @Description: GZIP 数据压缩
	 * @author (ljh) @date 2015-4-13 下午6:00:52
	 * @param data
	 * @return
	 * @throws IOException
	 * @return byte[]
	 */
	public static byte[] compress(byte[] data) throws IOException {
		ByteArrayInputStream bais = new ByteArrayInputStream(data);
		ByteArrayOutputStream baos = new ByteArrayOutputStream();
		compress(bais, baos);// 输入流压缩到输出流
		baos.flush();
		baos.close();
		bais.close();
		return baos.toByteArray();
	}

	/**
	 * @Description: GZIP 数据压缩
	 * @author (ljh) @date 2015-4-14 上午9:26:30
	 * @param is
	 * @param os
	 * @throws IOException
	 * @return void
	 */
	public static void compress(InputStream is, OutputStream os) throws IOException {
		GZIPOutputStream gzipOS = new GZIPOutputStream(os, 1024);
		int count;
		byte data[] = new byte[BUFFER];
		while ((count = is.read(data, 0, data.length)) != -1) {
			gzipOS.write(data, 0, count);
		}
		gzipOS.finish();
		gzipOS.flush();
		gzipOS.close();
	}

	/**
	 * @Description: GZIP 数据解压缩
	 * @author (ljh) @date 2015-4-13 下午6:00:42
	 * @param data
	 * @return
	 * @throws IOException
	 * @return byte[]
	 */
	public static byte[] uncompress(byte[] data) throws IOException {
		ByteArrayInputStream bais = new ByteArrayInputStream(data);
		ByteArrayOutputStream baos = new ByteArrayOutputStream(BUFFER);
		uncompress(bais, baos);
		baos.flush();
		baos.close();
		bais.close();
		return baos.toByteArray();
	}

	/**
	 * @Description: GZIP 数据解压缩
	 * @author (ljh) @date 2015-4-14 上午9:26:51
	 * @param is
	 * @param os
	 * @throws IOException
	 * @return void
	 */
	private static void uncompress(InputStream is, OutputStream os) throws IOException {
		GZIPInputStream gzipIS = new GZIPInputStream(is);
		int count;
		byte data[] = new byte[BUFFER];
		while ((count = gzipIS.read(data, 0, data.length)) != -1) {
			os.write(data, 0, count);
		}
		gzipIS.close();
	}

	/**
	 * @Description: 文件压缩
	 * @author (ljh) @date 2015-4-14 上午9:28:46
	 * @param file
	 * @throws Exception
	 * @return void
	 */
	public static void compress(File file) throws IOException {
		compress(file, true);
	}

	/**
	 * @Description: 文件压缩
	 * @author (ljh) @date 2015-4-14 上午9:29:21
	 * @param file
	 * @param delete
	 *            是否删除源文件
	 * @throws Exception
	 * @return void
	 * @throws IOException
	 */
	public static void compress(File file, boolean delete) throws IOException {
		if (!file.exists()) {
			// 文件不存在
		}
		FileInputStream fis = new FileInputStream(file);
		FileOutputStream fos = new FileOutputStream(file.getPath() + EXT);
		compress(fis, fos);
		fis.close();
		fos.flush();
		fos.close();
		if (delete) {
			file.delete();
		}
	}

	/**
	 * @Description: 文件压缩
	 * @author (ljh) @date 2015-4-14 上午9:32:52
	 * @param path
	 *            源文件路径
	 * @throws Exception
	 * @return void
	 */
	public static void compress(String path) throws IOException {
		compress(path, true);
	}

	/**
	 * @Description: 文件压缩
	 * @author (ljh) @date 2015-4-14 上午9:33:18
	 * @param path
	 *            源文件路径
	 * @param delete
	 *            是否删除源文件
	 * @throws Exception
	 * @return void
	 * @throws IOException
	 */
	public static void compress(String path, boolean delete) throws IOException {
		File file = new File(path);
		compress(file, delete);
	}

	/**
	 * @Description: 文件解压缩
	 * @author (ljh) @date 2015-4-14 上午9:35:03
	 * @param file
	 * @throws Exception
	 * @return void
	 */
	public static void uncompress(File file) throws IOException {
		uncompress(file, true);
	}

	/**
	 * @Description: 文件解压缩
	 * @author (ljh) @date 2015-4-14 上午9:35:44
	 * @param file
	 * @param delete
	 *            是否删除源文件
	 * @throws Exception
	 * @return void
	 * @throws IOException
	 */
	public static void uncompress(File file, boolean delete) throws IOException {
		if (!file.exists()) {
			// 文件不存在
		}
		FileInputStream fis = new FileInputStream(file);
		FileOutputStream fos = new FileOutputStream(file.getPath().replace(EXT, ""));
		uncompress(fis, fos);
		fis.close();
		fos.flush();
		fos.close();
		if (delete) {
			file.delete();
		}
	}

	/**
	 * @Description: 文件解压缩
	 * @author (ljh) @date 2015-4-14 上午9:37:48
	 * @param path
	 * @throws Exception
	 * @return void
	 */
	public static void uncompress(String path) throws IOException {
		uncompress(path, true);
	}

	/**
	 * @Description: 文件解压缩
	 * @author (ljh) @date 2015-4-14 上午9:38:03
	 * @param path
	 * @param delete
	 *            是否删除源文件
	 * @throws Exception
	 * @return void
	 */
	public static void uncompress(String path, boolean delete) throws IOException {
		File file = new File(path);
		uncompress(file, delete);
	}
}


测试代码:


	/**
	 * @Description: 中文150字节压缩比较明显,英文50字节压缩比较明显
	 * @author (ljh) @date 2015-4-14 上午11:37:37 
	 * @throws IOException 
	 * @return void
	 */
//	@org.junit.Test
	public void testGZIP() throws IOException {
		byte[] bytes = "阿莱克斯大家噶拉卡死机的个啊了的卡死机格拉卡死机的了感觉打开上岙地沟阿萨德gas的歌撒的歌第三个滴答滴答滴答滴答滴答滴答滴答".getBytes();
		// 对bytes压缩
		// 验证一下压缩后的效果对比
		System.out.println("压缩前:");
		System.out.println(bytes.length);
		for (byte b : bytes) {
			System.out.print(b + " ");
		}
		System.out.println();
		System.out.println("压缩后:");
		byte[] bytes2 = GZipUtils.compress(bytes);
		System.out.println(bytes2.length);
		for (byte b : bytes2) {
			System.out.print(b + " ");
		}
		
		System.out.println();
		System.out.println("解压缩后:");
        byte[] byte22 = GZipUtils.uncompress(bytes2);
        System.out.println(byte22.length);
        for (byte b : byte22) {
        	System.out.print(b+" ");
		}
		
        //GZIP工具
//        GZipUtils.compress("F:\\Activity_win9.bmp", false);//被压缩文件是一个12M的bmp文件,压缩后大小为1.2M
//        GZipUtils.uncompress("F:\\Activity_win9.bmp.gz", false);//解压缩后与源文件相同
        
	}


点我下载相关源码



Java解压缩技术(二)GZIP压缩-解压缩(

标签:bzip2   gzip   java压缩   zip   数据压缩   

原文地址:http://blog.csdn.net/lijunhuayc/article/details/45042859

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!