使用ant提供的zip进行压缩和解压缩,支持中文

本文介绍了一个基于Apache工具集实现的文件压缩与解压缩工具类。该工具类支持递归压缩目录下的所有文件,并能正确处理中文文件名,避免乱码问题。此外,还提供了解压缩功能。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

/**
 *
 */
package com.yfei.uti.zip;
 
import java.io.BufferedInputStream;
import java.io.BufferedOutputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.util.Enumeration;
 
import org.apache.tools.zip.ZipEntry;
import org.apache.tools.zip.ZipFile;
import org.apache.tools.zip.ZipOutputStream;
 
/**
 * @author yangfei
 * @since 2013-1-27
 */
public class ZipUtilApache {
 
    static final int BUFFER = 1024;
 
    public static void zip(String destDirPath, String inputPath)
            throws Exception {
        File inputFile = new File(inputPath);
        // 创建压缩文件
        File destDir = new File(destDirPath);
        if (!destDir.exists()) {
            destDir.mkdir();
        }
        File destFile = new File(destDir + File.separator + inputFile.getName()
                + ".zip");
        // 递归压缩方法
        ZipOutputStream out = new ZipOutputStream(
                new FileOutputStream(destFile));
        // 设置压缩编码.设置为GBK后在windows下就不会乱码了,如果要放到Linux或者Unix下就不要设置了
        out.setEncoding("GBK");
        zip(out, inputFile, "");
        System.out.println("zip done");
        out.close();
    }
 
    private static void zip(ZipOutputStream out, File f, String base)
            throws Exception {
        System.out.println("Zipping   " + f.getName()); // 记录日志,开始压缩
        if (f.isDirectory()) { // 如果是文件夹,则获取下面的所有文件
            File[] fl = f.listFiles();
            if (base.length() > 0) {
 
                out.putNextEntry(new ZipEntry(base + "/"));// 此处要将文件写到文件夹中只用在文件名前加"/"再加文件夹名
            }
 
            base = base.length() == 0 ? "" : base + "/";
            for (int i = 0; i < fl.length; i++) {
                zip(out, fl[i], base + fl[i].getName());
            }
        } else { // 如果是文件,则压缩
            out.putNextEntry(new ZipEntry(base)); // 生成下一个压缩节点
            FileInputStream in = new FileInputStream(f); // 读取文件内容
            int len;
            byte[] buf = new byte[BUFFER];
            while ((len = in.read(buf, 0, BUFFER)) != -1) {
                out.write(buf, 0, len); // 写入到压缩包
            }
            in.close();
            out.closeEntry();
        }
    }
 
    /**
     * 解压缩zip文件
     *
     * @param fileName
     *            要解压的文件名 包含路径 如:"c:\\test.zip"
     * @param filePath
     *            解压后存放文件的路径 如:"c:\\temp"
     * @throws Exception
     */
    public static void unZip(String fileName, String destFilePath)
            throws Exception {
        ZipFile zipFile = new ZipFile(fileName, "GBK"); // 以“GBK”编码创建zip文件,用来处理winRAR压缩的文件。
        Enumeration emu = zipFile.getEntries();
        while (emu.hasMoreElements()) {
            ZipEntry entry = (ZipEntry) emu.nextElement();
            if (entry.isDirectory()) {
                File dir = new File(destFilePath + entry.getName());
                if (!dir.exists()) {
                    dir.mkdirs();
                }
                continue;
            }
            BufferedInputStream bis = new BufferedInputStream(zipFile
                    .getInputStream(entry));
 
            File file = new File(destFilePath + entry.getName());
            File parent = file.getParentFile();
            if (parent != null && (!parent.exists())) {
                parent.mkdirs();
            }
            FileOutputStream fos = new FileOutputStream(file);
            BufferedOutputStream bos = new BufferedOutputStream(fos, BUFFER);
            byte[] buf = new byte[BUFFER];
            int len = 0;
            while ((len = bis.read(buf, 0, BUFFER)) != -1) {
                fos.write(buf, 0, len);
            }
            bos.flush();
            bos.close();
            bis.close();
            System.out.println("解压文件:" + file.getName());
        }
        zipFile.close();
    }
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值