GZIP常常用在linxu环境下,是一种非常简单的压缩算法。在Java实现API中,它仅仅包含两个实现类:GZIPInputStream和GZIPOutputStream。
GZIPOutputStream类用于压缩
GZIPInputStream类用于解压缩
先说压缩实现,GZIPOutputStream只有一个方法用于压缩,就是带定长的write方法。简单调用如下文所示:
记得完成操作后,调用finish方法和flush方法!
核心的压缩实现就这么多!
对于解压缩,GZIPInputStream也对应GZIPOutputStream提供了一个带定长的read方法。简单调用如下文所示:
就这么简单! 核心内容完毕!
顺便补充一下,在liunx下操作gzip命令
gzip file 用于压缩,如 gzip a.txt 将得到文件 a.txt.gz , 同时删除文件a.txt!。
gzip -d file.gz 用于解压缩,如 gzip -d a.txt.gz 将得到文件 a.txt , 同时删除文件a.txt.gz!。
根据这些特性,我补充了相应的文件操作实现,详见下文!
完整实现:
罗嗦了半天,到底行不行?
来个测试用例,测试用例如下所示:
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 = is.read(data, 0, BUFFER)) != -1) {
- gos.write(data, 0, count);
- }
- gos.finish();
- gos.flush();
- gos.close();
- }
记得完成操作后,调用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 = gis.read(data, 0, BUFFER)) != -1) {
- os.write(data, 0, count);
- }
- gis.close();
- }
就这么简单! 核心内容完毕!
顺便补充一下,在liunx下操作gzip命令
gzip file 用于压缩,如 gzip a.txt 将得到文件 a.txt.gz , 同时删除文件a.txt!。
gzip -d file.gz 用于解压缩,如 gzip -d a.txt.gz 将得到文件 a.txt , 同时删除文件a.txt.gz!。
根据这些特性,我补充了相应的文件操作实现,详见下文!
完整实现:
- /**
- * 2010-4-13
- */
- package org.zlex.commons.io;
- import java.io.ByteArrayInputStream;
- import java.io.ByteArrayOutputStream;
- import java.io.File;
- import java.io.FileInputStream;
- import java.io.FileOutputStream;
- import java.io.InputStream;
- import java.io.OutputStream;
- import java.util.zip.GZIPInputStream;
- import java.util.zip.GZIPOutputStream;
- /**
- * GZIP工具
- *
- * @author <a href="mailto:zlex.dongliang@gmail.com">梁栋</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 = baos.toByteArray();
- baos.flush();
- baos.close();
- bais.close();
- 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(file.getPath() + EXT);
- compress(fis, fos);
- fis.close();
- fos.flush();
- fos.close();
- if (delete) {
- file.delete();
- }
- }
- /**
- * 数据压缩
- *
- * @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 = is.read(data, 0, BUFFER)) != -1) {
- gos.write(data, 0, count);
- }
- gos.finish();
- gos.flush();
- gos.close();
- }
- /**
- * 文件压缩
- *
- * @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 = baos.toByteArray();
- baos.flush();
- baos.close();
- bais.close();
- 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(file.getPath().replace(EXT,
- ""));
- decompress(fis, fos);
- fis.close();
- fos.flush();
- fos.close();
- if (delete) {
- file.delete();
- }
- }
- /**
- * 数据解压缩
- *
- * @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 = gis.read(data, 0, BUFFER)) != -1) {
- os.write(data, 0, count);
- }
- gis.close();
- }
- /**
- * 文件解压缩
- *
- * @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 org.zlex.commons.compress.compress;
- import static org.junit.Assert.assertEquals;
- import java.io.DataInputStream;
- import java.io.File;
- import java.io.FileInputStream;
- import java.io.FileOutputStream;
- import org.junit.Test;
- /**
- * @author <a href="mailto:zlex.dongliang@gmail.com">梁栋</a>
- * @since 1.0
- */
- public class GZipUtilsTest {
- private String inputStr = "zlex@zlex.org,snowolf@zlex.org,zlex.snowolf@zlex.org";
- @Test
- public final void testDataCompress() throws Exception {
- System.err.println("原文:\t" + inputStr);
- byte[] input = inputStr.getBytes();
- System.err.println("长度:\t" + input.length);
- byte[] data = GZipUtils.compress(input);
- System.err.println("压缩后:\t");
- System.err.println("长度:\t" + data.length);
- byte[] output = GZipUtils.decompress(data);
- String outputStr = new String(output);
- System.err.println("解压缩后:\t" + outputStr);
- System.err.println("长度:\t" + output.length);
- assertEquals(inputStr, outputStr);
- }
- @Test
- public final void testFileCompress() throws Exception {
- FileOutputStream fos = new FileOutputStream("d:/f.txt");
- fos.write(inputStr.getBytes());
- fos.flush();
- fos.close();
- GZipUtils.compress("d:/f.txt", false);
- GZipUtils.decompress("d:/f.txt.gz", false);
- File file = new File("d:/f.txt");
- FileInputStream fis = new FileInputStream(file);
- DataInputStream dis = new DataInputStream(fis);
- byte[] data = new byte[(int) file.length()];
- dis.readFully(data);
- fis.close();
- String outputStr = new String(data);
- assertEquals(inputStr, outputStr);
- }
- }
- http://snowolf.iteye.com/blog/643010