您的位置:首页 > 其它

ZipUtils-压缩工具类

2015-08-10 11:42 387 查看

ZipUtils-压缩工具类

支持中文的解压工具类。

更新于:2015-08-10

[code]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();
    }
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: