Apache在Commons Compress简介和简单应用

Apache在CommonsCompress中提供了众所周知的tar、cpio、zip等压缩算法实现,其中最为丰富的当属zip实现了!

Commons Compress为压缩(GZip和BZip2)构建了压缩算法工厂类CompressorStreamFactory。通过这个类可以方便的构建GZip和BZip2的输入输出流,关键字分别为“gz”和“bzip2”。 
GZip 

Java代码 

//GzipCompressorInputStream       

    CompressorInputStream gzipIn = newCompressorStreamFactory() 

            .createCompressorInputStream("gz",is); 

     

    // GzipCompressorOutputStream 

    CompressorOutputStream gzipOut = newCompressorStreamFactory() 

           .createCompressorOutputStream("gz", os);

 

BZip2 

Java代码 

// BZip2CompressorInputStream 

    CompressorInputStream bzip2In =new CompressorStreamFactory() 

           .createCompressorInputStream("bzip2", is); 

     

    //BZip2CompressorOutputStream 

    CompressorOutputStream bzip2Out= new CompressorStreamFactory() 

           .createCompressorOutputStream("bzip2", os); 

 

下面是一个完整的通过apachecommons compress简单实现bzip2压缩的样例。

需要导入的包为:commons-compress-1.9.jar

packagetest.ffm83.commons.compress;

 

importjava.io.ByteArrayInputStream; 

importjava.io.ByteArrayOutputStream; 

importjava.io.File; 

importjava.io.FileInputStream; 

importjava.io.FileOutputStream; 

importjava.io.InputStream; 

importjava.io.OutputStream; 

 

importorg.apache.commons.compress.compressors.bzip2.BZip2CompressorInputStream;

importorg.apache.commons.compress.compressors.bzip2.BZip2CompressorOutputStream;

/* 通过apache commons compress简单实现bzip2压缩

 * 部分代码来自互联网

 * @author 范芳铭

 * */

publicclass BZip2Utils {

    public static final int BUFFER = 1024; 

    public static final CharSequence EXT =".bz2"; 

 

    /**

     * 数据压缩

     * 

     * @param data

     * @return

     * @throws Exception

     */ 

    public static byte[] compress(byte[] data)throws Exception { 

        ByteArrayInputStream bais = newByteArrayInputStream(data); 

        ByteArrayOutputStream baos = newByteArrayOutputStream(); 

 

        // 压缩 

        compress(bais, baos); 

 

        byte[] output =baos.toByteArray(); 

 

        baos.flush(); 

        baos.close(); 

 

        bais.close(); 

 

        return output; 

    } 

   

    /**

     * 文件压缩

     * 

     * @param file

     * @param delete

     *            是否删除原始文件

     * @throws Exception

     */ 

    public static void compress(File file,boolean delete) throws Exception { 

        FileInputStream fis = newFileInputStream(file); 

        FileOutputStream fos = newFileOutputStream(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 { 

 

        BZip2CompressorOutputStream gos = newBZip2CompressorOutputStream(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

     * @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 = newByteArrayInputStream(data); 

        ByteArrayOutputStream baos = newByteArrayOutputStream(); 

 

        // 解压缩 

 

        decompress(bais, baos); 

 

        data = baos.toByteArray(); 

 

        baos.flush(); 

        baos.close(); 

 

        bais.close(); 

 

        return data; 

    } 

  

    /**

     * 文件解压缩

     * 

     * @param file

     * @param delete

     *            是否删除原始文件

     * @throws Exception

     */ 

    public static void decompress(File file,boolean delete) throws Exception { 

        FileInputStream fis = newFileInputStream(file); 

        FileOutputStream fos = newFileOutputStream(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(InputStreamis, OutputStream os) 

            throws Exception { 

 

        BZip2CompressorInputStream gis = newBZip2CompressorInputStream(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

     * @param delete

     *            是否删除原始文件

     * @throws Exception

     */ 

    public static void decompress(String path,boolean delete) throws Exception { 

        File file = new File(path); 

        decompress(file, delete); 

    } 

   

 

    public static void main(String[] args)throws Exception{

        StringinputStr = "http://blog.csdn.net/ffm83/article/details/42240513";

       

        byte[]input = inputStr.getBytes(); 

        System.out.println("原文:\t" + inputStr); 

        System.out.println("长度:\t" + input.length); 

       

        byte[] data = compress(input); 

        System.err.println("压缩后:\t"); 

        System.err.println("长度:\t" + data.length); 

 

        byte[] output =  decompress(data); 

        String outputStr = newString(output); 

        System.err.println("解压缩后:\t" + outputStr); 

        System.err.println("长度:\t" + output.length);

    }

}


运行结果如下:

原文:   http://blog.csdn.net/ffm83/article/details/42240513

长度:   51

压缩后:

长度:   86

解压缩后:   http://blog.csdn.net/ffm83/article/details/42240513

长度:   51

编程技巧