ZipUtils-压缩工具类

ZipUtils-压缩工具类


支持中文的解压工具类。

更新于:2015-08-10

import org.apache.tools.zip.ZipEntry;
import org.apache.tools.zip.ZipFile;

import java.io.BufferedInputStream;
import java.io.BufferedOutputStream;
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.util.Enumeration;

public class ZipUtils {
    /**
     * 解压zip包,支持中文。
     * 需要ant.jar包,下载地址:http://download.csdn.net/detail/qiantujava/8984345
     *
     * @param zipFile    需要解压的文件,如:/mnt/sdcard/abc/abc.zip
     * @param zipFileDir 需要解压的文件所在的文件夹,如:/mnt/sdcard/abc/
     * @throws IOException
     */
    public static void unZipFile(String zipFile, String zipFileDir)
            throws IOException {
        BufferedInputStream bi;
        ZipFile zf = new ZipFile(zipFile, "GBK");
        Enumeration e = zf.getEntries();
        while (e.hasMoreElements()) {
            ZipEntry entry = (ZipEntry) e.nextElement();
            String entryName = entry.getName();
            String path = zipFileDir + "/" + entryName;
            if (entry.isDirectory()) {
                File dir = new File(path);
                if (!dir.exists()) dir.mkdirs();
            } else {
                String fileDir = path.substring(0, path.lastIndexOf("/"));
                File fileDirFile = new File(fileDir);
                if (!fileDirFile.exists()) fileDirFile.mkdirs();

                BufferedOutputStream bos = new BufferedOutputStream(
                        new FileOutputStream(zipFileDir + "/" + entryName));
                bi = new BufferedInputStream(zf.getInputStream(entry));
                byte[] readContent = new byte[1024];
                int readCount = bi.read(readContent);
                while (readCount != -1) {
                    bos.write(readContent, 0, readCount);
                    readCount = bi.read(readContent);
                }
                bos.close();
            }
        }
        zf.close();
    }
}
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值