码迷,mamicode.com
首页 > 其他好文 > 详细

ZipUtils-压缩工具类

时间:2015-08-10 13:43:08      阅读:106      评论:0      收藏:0      [点我收藏+]

标签:解压

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();
    }
}

版权声明:本文为博主原创文章,转载请注明原地址。

ZipUtils-压缩工具类

标签:解压

原文地址:http://blog.csdn.net/qiantujava/article/details/47396543

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!