JAVA解压、压缩ZIP文件

本文介绍了一个Java类,用于实现ZIP文件的压缩和解压缩功能。该类使用了Apache Commons Compress库来处理ZIP文件操作,并提供了方便的方法来压缩多个文件到一个ZIP文件或从ZIP文件中解压文件到指定目录。


package com.core.common;

import java.io.BufferedInputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.InputStream;
import java.io.OutputStream;

import org.apache.commons.compress.archivers.ArchiveEntry;
import org.apache.commons.compress.archivers.zip.ZipArchiveEntry;
import org.apache.commons.compress.archivers.zip.ZipArchiveInputStream;
import org.apache.commons.compress.archivers.zip.ZipArchiveOutputStream;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

/**
 * <p>
 * ClassName: ZipFile
 * </p>
 * <p>
 * Description: 压缩&解压文件
 * </p>
 */
public class ZipFile {
    /**
     * <p>
     * Field log: 日志
     * </p>
     */
    public Logger log = LoggerFactory.getLogger(this.getClass());

    /**
     * <p>
     * Field byteSize: 字节数组长度
     * </p>
     */
    private String byteSize = "1024";

    /**
     * <p>
     * Field encoding: 编码格式
     * </p>
     */
    private String encoding = "UTF-8";

    /**
     * <p>
     * Description: 构造函数
     * </p>
     */
    public ZipFile() {
    }

    /**
     * <p>
     * Description: 构造方法
     * </p>
     * 
     * @param encoding 编码
     */
    public ZipFile(String encoding) {
        this.encoding = encoding;
    }

    /**
     * <p>
     * Description: 把zip文件解压到指定的文件夹
     * </p>
     * 
     * @param zipFilePath zip文件路径
     * @param saveFileDir 解压后的文件存放路径
     * @throws Exception 异常
     */
    public void decompressZip(String zipFilePath, String saveFileDir) throws Exception {
        File file = null;
        file = new File(zipFilePath);
        if (file.exists()) {
            InputStream is = null;
            ZipArchiveInputStream zais = null;
            try {
                is = new FileInputStream(file);
                zais = new ZipArchiveInputStream(is, this.encoding);
                ArchiveEntry archiveEntry = null;

                //把zip包中的每个文件读取出来
                //然后把文件写到指定的文件夹
                while ((archiveEntry = zais.getNextEntry()) != null) {

                    //如果是目录,则自动创建
                    if (archiveEntry.isDirectory()) {
                        File f = new File(saveFileDir + archiveEntry.getName());
                        f.mkdir();
                        continue;
                    }

                    //构造解压出来的文件存放路径
                    String entryFilePath = null;
                    entryFilePath = saveFileDir + archiveEntry.getName();
                    byte[] content = null;
                    content = new byte[new Integer(this.byteSize)];
                    OutputStream os = null;
                    try {
                        //把解压出来的文件写到指定路径
                        File entryFile = null;
                        entryFile = new File(entryFilePath);
                        os = new FileOutputStream(entryFile);
                        int r;
                        while ((r = zais.read(content)) > 0) {
                            os.write(content, 0, r);
                        }
                    } catch (Exception throwable) {
                        this.log.error("解压缩错误:", throwable);
                        throw throwable;
                    } finally {
                        if (os != null) {
                            os.flush();
                            os.close();
                        }
                    }
                }
            } catch (Exception throwable) {
                this.log.error("解压缩错误:", throwable);
                throw throwable;
            } finally {
                try {
                    if (zais != null) {
                        zais.close();
                    }
                    if (is != null) {
                        is.close();
                    }
                } catch (Exception throwable) {
                    this.log.error("解压缩错误:", throwable);
                    throw throwable;
                }
            }
        }
    }

    /**
     * <p>
     * Description: 压缩文件
     * </p>
     * 
     * @param files 需要压缩的文件
     * @param zipFilePath zip文件
     * @throws Exception 异常
     */
    public void compressFiles2Zip(File[] files, String zipFilePath) throws Exception {
        if (files != null && files.length > 0) {
            ZipArchiveOutputStream zaos = null;
            try {
                File zipFile = null;
                zipFile = new File(zipFilePath);
                zaos = new ZipArchiveOutputStream(zipFile);

                //将每个文件用ZipArchiveEntry封装
                //再用ZipArchiveOutputStream写到压缩文件中
                for (File file : files) {
                    if (file != null) {
                        ZipArchiveEntry zipArchiveEntry = null;
                        zipArchiveEntry = new ZipArchiveEntry(file, file.getName());
                        zaos.putArchiveEntry(zipArchiveEntry);
                        InputStream is = null;
                        try {
                            is = new BufferedInputStream(new FileInputStream(file), Constants.IO_BUFFERED);
                            byte[] buffer = null;
                            buffer = new byte[new Integer(this.byteSize)];
                            int len = -1;
                            while ((len = is.read(buffer)) != -1) {
                                zaos.write(buffer, 0, len);
                            }
                            zaos.closeArchiveEntry();
                        } catch (Exception throwable) {
                            this.log.error("压缩错误:", throwable);
                            throw throwable;
                        } finally {
                            if (is != null) {
                                is.close();
                            }
                        }
                    }
                }
                zaos.finish();
            } catch (Exception throwable) {
                this.log.error("压缩错误:", throwable);
                throw throwable;
            } finally {
                try {
                    if (zaos != null) {
                        zaos.close();
                    }
                } catch (Exception throwable) {
                    this.log.error("关闭失败:", throwable);
                    throw throwable;
                }
            }

        }
    }
}
 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值