java压缩解压代码

下边是在项目用到的压缩解压class ,粘出来,分享。

 

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;

public class ZipTool {
    private static final int BUFFER = 2048;
    private int level;

    public ZipTool() {
        level = 0;
    }

    /**
     * setLevel
     * @param level int
     */
    public void setLevel(int level) {
        this.level = level;

    }

    /**
     * zip a File or Directory
     * @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), BUFFER);
            ZipOutputStream out = new ZipOutputStream(bout);
            zip(out, inputFile, inputFile.getName());
            out.close();
        }
        catch (IOException ex) {
            throw new ZipException(ex.getMessage());
        }
    }

    /**
     * zip some Files
     * @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),
                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());
        }
    }

    /**
     * unzip a File
     *
     * @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, 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.mkdir();
                }
                else {
                    File f = new File(outputFile.getPath() + File.separator + name);
                    f.createNewFile();
                    FileOutputStream out = new FileOutputStream(f);
                    byte data[] = new byte[BUFFER];
                    int b;

                    while ( (b = in.read(data, 0, 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 (level != 0) {
            out.setLevel(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 (level != 0) {
            out.setLevel(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 (level != 0) {
            out.setLevel(level);
        }
        try {
            out.putNextEntry(new ZipEntry(base));
            FileInputStream in = new FileInputStream(f);
            BufferedInputStream bufferIn = new BufferedInputStream(in, BUFFER);
            byte[] data = new byte[BUFFER];
            int b;
            while ( (b = bufferIn.read(data, 0, BUFFER)) != -1) {
                out.write(data, 0, b);
            }
            bufferIn.close();
        }
        catch (IOException ex) {
            throw new ZipException(ex.getMessage());
        }
    }
}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值