使用java.util.zip包提供的api完成压缩解压缩工作<转>

java中常用的压缩解压开发包
1.      AT4J是一套数据压缩和文件打包存档的工具,采用java开发 。它支持读取和创建zip和tar文件,以及通过第三方库实现多种类型的压缩算法。其中 bzip2 压缩库采用 Apache Commons Compress 项目.LZMA 压缩库采用 7-ZIP。


2.      jazzlib - 是一个完全用Java实现的压缩包,可以方便的替代现在的java.util.zip原始的实现。典型的使用场合是在applet中使用它,在applet中不允许使用原始代码。http://jazzlib.sourceforge.net/


3.      ZeroTurnaround 简称 zt-zip , 是一个 Java 用来处理压缩包的类库。www.zeroturnaround.com/
         有些童鞋打不开,那就去这里吧: https://github.com/zeroturnaround/zt-zip

4.      Apache Commons Compress用以实现将文件压缩或解压成 tar、zip、bzip2 等格式。http://commons.apache.org/compress/


5.       其它: 还有很多例如 junrar,jZipLib,jZlib4ME等等。



package com.accp.commons.util;

import java.io.BufferedInputStream;
import java.io.BufferedOutputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.util.zip.CRC32;
import java.util.zip.CheckedInputStream;
import java.util.zip.ZipEntry;
import java.util.zip.ZipException;
import java.util.zip.ZipInputStream;
import java.util.zip.ZipOutputStream;

import org.apache.commons.lang.StringUtils;
/**
 * 压缩解压缩工具包
 * @author yoyoflash
 * @date:2012-6-15 下午12:03:27
 */
public class ZipTools {
        private static final int ZIP_BUFFER = 2048;
        private int zip_level;

        public ZipTools() {
                zip_level = 0;
        }

        /**
         * setLevel level - the compression level (0-9)
         * 
         * @param level
         *            int
         */
        public void setLevel(int level) {
                this.zip_level = level;

        }

        /**
         * 支持对于文件和目录的压缩
         * 
         * @param inputFile
         *            File 要压缩的目录或者文件
         * @param outputFile
         *            File 输出的压缩文件
         * @throws ZipException
         */
        public void zipFile(File inputFile, File outputFile) throws ZipException {
                try {
                        BufferedOutputStream bout = new BufferedOutputStream(
                                        new FileOutputStream(outputFile), ZIP_BUFFER);
                        ZipOutputStream out = new ZipOutputStream(bout);
                        zip(out, inputFile, inputFile.getName());
                        out.close();
                } catch (IOException ex) {
                        throw new ZipException(ex.getMessage());
                }
        }

        /**
         * 批量压缩功能
         * 
         * @param inputFiles
         *            File[] 要压缩的文件数组
         * @param outputFile
         *            File 压缩后的文件
         * @throws ZipException
         */
        public void zipFiles(File[] inputFiles, File outputFile)
                        throws ZipException {
                try {
                        BufferedOutputStream bout = new BufferedOutputStream(
                                        new FileOutputStream(outputFile), ZIP_BUFFER);
                        ZipOutputStream out = new ZipOutputStream(bout);
                        for (int i = 0; i < inputFiles.length; i++) {
                                zip(out, inputFiles[i], inputFiles[i].getName());
                        }
                        out.close();
                } catch (IOException ex) {
                        throw new ZipException(ex.getMessage());
                }
        }

        /**
         * 解压缩文件
         * 
         * @param inputFile
         *            File 原压缩文件
         * @param outputFile
         *            File 解压目录
         * @throws ZipException
         */
        public void unZipFile(File inputFile, File outputFile) throws ZipException {
                try {
                        FileInputStream tin = new FileInputStream(inputFile);
                        CheckedInputStream cin = new CheckedInputStream(tin, new CRC32());
                        BufferedInputStream bufferIn = new BufferedInputStream(cin,
                                        ZIP_BUFFER);
                        ZipInputStream in = new ZipInputStream(bufferIn);
                        ZipEntry z = in.getNextEntry();

                        while (z != null) {
                                String name = z.getName();
                                if (z.isDirectory()) {
                                        File f = new File(outputFile.getPath() + File.separator
                                                        + name);
                                        f.mkdirs();
                                } else {
                                        File f = new File(outputFile.getPath() + File.separator
                                                        + name);
                                        f.createNewFile();
                                        FileOutputStream out = new FileOutputStream(f);
                                        byte data[] = new byte[ZIP_BUFFER];
                                        int b;

                                        while ((b = in.read(data, 0, ZIP_BUFFER)) != -1) {
                                                out.write(data, 0, b);
                                        }
                                        out.close();
                                }
                                z = in.getNextEntry();
                        }

                        in.close();
                }

                catch (IOException ex) {
                        throw new ZipException(ex.getMessage());
                }

        }

        private void zip(ZipOutputStream out, File f, String base)
                        throws FileNotFoundException, ZipException {
                if (zip_level != 0) {
                        out.setLevel(zip_level);
                }
                if (f.isDirectory()) {
                        zipDirectory(out, f, base);
                } else {
                        if (StringUtils.isEmpty(base)) {
                                base = f.getName();
                        }
                        zipLeapFile(out, f, base);
                }

        }

        private void zipDirectory(ZipOutputStream out, File f, String base)
                        throws FileNotFoundException, ZipException {
                File[] files = f.listFiles();
                if (zip_level != 0) {
                        out.setLevel(zip_level);
                }
                try {
                        out.putNextEntry(new ZipEntry(base + "/"));
                } catch (IOException ex) {
                        throw new ZipException(ex.getMessage());
                }
                if (StringUtils.isEmpty(base)) {
                        base = new String();
                } else {
                        base = base + "/";
                }

                for (int i = 0; i < files.length; i++) {
                        zip(out, files[i], base + files[i].getName());
                }

        }

        private void zipLeapFile(ZipOutputStream out, File f, String base)
                        throws FileNotFoundException, ZipException {
                if (zip_level != 0) {
                        out.setLevel(zip_level);
                }
                try {
                        out.putNextEntry(new ZipEntry(base));
                        FileInputStream in = new FileInputStream(f);
                        BufferedInputStream bufferIn = new BufferedInputStream(in,
                                        ZIP_BUFFER);
                        byte[] data = new byte[ZIP_BUFFER];
                        int b;
                        while ((b = bufferIn.read(data, 0, ZIP_BUFFER)) != -1) {
                                out.write(data, 0, b);
                        }
                        bufferIn.close();
                } catch (IOException ex) {
                        throw new ZipException(ex.getMessage());
                }
        }
}







猜你喜欢

转载自yingbin920.iteye.com/blog/1744758
今日推荐