GZIP java 压缩技术
GZIP常常用在linxu环境下,是一种非常简单的压缩算法。在Java实现API中,它仅仅包含两个实现类:GZIPInputStream和GZIPOutputStream。
GZIPOutputStream类用于压缩
GZIPInputStream类用于解压缩
先说压缩实现,GZIPOutputStream只有一个方法用于压缩,就是带定长的write方法。简单调用如下文所示:
/** * 数据压缩 * * @param is * @param os * @throws Exception */ public static void compress(InputStream is, OutputStream os) throws Exception { GZIPOutputStream gos = new GZIPOutputStream(os); int count; byte data[] = new byte[BUFFER]; while ((count = 域名(data, 0, BUFFER)) != -1) { 域名e(data, 0, count); } 域名sh(); 域名h(); 域名e(); }
记得完成操作后,调用finish方法和flush方法!
核心的压缩实现就这么多!
对于解压缩,GZIPInputStream也对应GZIPOutputStream提供了一个带定长的read方法。简单调用如下文所示:
/** * 数据解压缩 * * @param is * @param os * @throws Exception */ public static void decompress(InputStream is, OutputStream os) throws Exception { GZIPInputStream gis = new GZIPInputStream(is); int count; byte data[] = new byte[BUFFER]; while ((count = 域名(data, 0, BUFFER)) != -1) { 域名e(data, 0, count); } 域名e(); }
就这么简单! 核心内容完毕!
顺便补充一下,在liunx下操作gzip命令
gzip file用于压缩,如gzip 域名将得到文件域名,同时删除文件域名!。
gzip -d 域名用于解压缩,如gzip -d 域名将得到文件域名,同时删除文件域名!。
根据这些特性,我补充了相应的文件操作实现,详见下文!
/** * 2010-4-13 */ package 域名域名; import 域名ArrayInputStream; import 域名ArrayOutputStream; import 域名; import 域名InputStream; import 域名OutputStream; import 域名tStream; import 域名utStream; import 域名.GZIPInputStream; import 域名.GZIPOutputStream; /** * GZIP工具 * * @author <a href="mailto:域名liang@域名">梁栋</a> * @since 1.0 */ public abstract class GZipUtils { public static final int BUFFER = 1024; public static final String EXT = ".gz"; /** * 数据压缩 * * @param data * @return * @throws Exception */ public static byte[] compress(byte[] data) throws Exception { ByteArrayInputStream bais = new ByteArrayInputStream(data); ByteArrayOutputStream baos = new ByteArrayOutputStream(); // 压缩 compress(bais, baos); byte[] output = 域名teArray(); 域名h(); 域名e(); 域名e(); return output; } /** * 文件压缩 * * @param file * @throws Exception */ public static void compress(File file) throws Exception { compress(file, true); } /** * 文件压缩 * * @param file * @param delete * 是否删除原始文件 * @throws Exception */ public static void compress(File file, boolean delete) throws Exception { FileInputStream fis = new FileInputStream(file); FileOutputStream fos = new FileOutputStream(域名ath() + EXT); compress(fis, fos); 域名e(); 域名h(); 域名e(); if (delete) { 域名te(); } } /** * 数据压缩 * * @param is * @param os * @throws Exception */ public static void compress(InputStream is, OutputStream os) throws Exception { GZIPOutputStream gos = new GZIPOutputStream(os); int count; byte data[] = new byte[BUFFER]; while ((count = 域名(data, 0, BUFFER)) != -1) { 域名e(data, 0, count); } 域名sh(); 域名h(); 域名e(); } /** * 文件压缩 * * @param path * @throws Exception */ public static void compress(String path) throws Exception { compress(path, true); } /** * 文件压缩 * * @param path * @param delete * 是否删除原始文件 * @throws Exception */ public static void compress(String path, boolean delete) throws Exception { File file = new File(path); compress(file, delete); } /** * 数据解压缩 * * @param data * @return * @throws Exception */ public static byte[] decompress(byte[] data) throws Exception { ByteArrayInputStream bais = new ByteArrayInputStream(data); ByteArrayOutputStream baos = new ByteArrayOutputStream(); // 解压缩 decompress(bais, baos); data = 域名teArray(); 域名h(); 域名e(); 域名e(); return data; } /** * 文件解压缩 * * @param file * @throws Exception */ public static void decompress(File file) throws Exception { decompress(file, true); } /** * 文件解压缩 * * @param file * @param delete * 是否删除原始文件 * @throws Exception */ public static void decompress(File file, boolean delete) throws Exception { FileInputStream fis = new FileInputStream(file); FileOutputStream fos = new FileOutputStream(域名ath().replace(EXT, "")); decompress(fis, fos); 域名e(); 域名h(); 域名e(); if (delete) { 域名te(); } } /** * 数据解压缩 * * @param is * @param os * @throws Exception */ public static void decompress(InputStream is, OutputStream os) throws Exception { GZIPInputStream gis = new GZIPInputStream(is); int count; byte data[] = new byte[BUFFER]; while ((count = 域名(data, 0, BUFFER)) != -1) { 域名e(data, 0, count); } 域名e(); } /** * 文件解压缩 * * @param path * @throws Exception */ public static void decompress(String path) throws Exception { decompress(path, true); } /** * 文件解压缩 * * @param path * @param delete * 是否删除原始文件 * @throws Exception */ public static void decompress(String path, boolean delete) throws Exception { File file = new File(path); decompress(file, delete); } }
罗嗦了半天,到底行不行?
来个测试用例,测试用例如下所示:
/** * 2010-4-13 */ package 域名域名ress; import static 域名域名rtEquals; import 域名InputStream; import 域名; import 域名InputStream; import 域名OutputStream; import 域名; /** * @author <a href="mailto:域名liang@域名">梁栋</a> * @since 1.0 */ public class GZipUtilsTest { private String inputStr = "zlex@域名,snowolf@域名,域名olf@域名"; @Test public final void testDataCompress() throws Exception { 域名tln("原文:\t" + inputStr); byte[] input = 域名ytes(); 域名tln("长度:\t" + 域名th); byte[] data = 域名ress(input); 域名tln("压缩后:\t"); 域名tln("长度:\t" + 域名th); byte[] output = 域名mpress(data); String outputStr = new String(output); 域名tln("解压缩后:\t" + outputStr); 域名tln("长度:\t" + 域名th); assertEquals(inputStr, outputStr); } @Test public final void testFileCompress() throws Exception { FileOutputStream fos = new FileOutputStream("d:/域名"); 域名e(域名ytes()); 域名h(); 域名e(); 域名ress("d:/域名", false); 域名mpress("d:/域名", false); File file = new File("d:/域名"); FileInputStream fis = new FileInputStream(file); DataInputStream dis = new DataInputStream(fis); byte[] data = new byte[(int) 域名th()]; 域名Fully(data); 域名e(); String outputStr = new String(data); assertEquals(inputStr, outputStr); } }
结果如何?
先看testDataCompress()方法控制台输出结果。
控制台输出如下:
原文: zlex@域名,snowolf@域名,域名olf@域名 长度: 52 压缩后: 长度: 45 解压缩后: zlex@域名,snowolf@域名,域名olf@域名 长度: 52
这里使用英文字符做测试,当输入字符串的字节数大于50左右时,压缩效果明显;如果这里使用中文压缩,可能当压缩上千字节时方能体现出压缩效果!
对于文件操作,朋友们可以自行实验,我代码里的实现是按照gzip命令来的!
举例来说:
压缩时,将文件域名压缩为域名,同时删除文件域名。
解压缩时,将文件域名解压缩为域名,同时删除文件域名。
注意执行testFileCompress方法,查看产生的文件! 你大可以放到linux上去做验证!
commons也提供了GZIP算法的实现,甚至更多种压缩算法(tar、bzip2等)的实现,有机会我将继续整理!
来自 大神 http://域名/blog/643010
好多的压缩技术。。