前往小程序,Get更优阅读体验!
立即前往
首页
学习
活动
专区
工具
TVP
发布
社区首页 >专栏 >Java压缩解压工具类

Java压缩解压工具类

作者头像
飞奔去旅行
发布2019-06-13 10:09:46
1.7K0
发布2019-06-13 10:09:46
举报
文章被收录于专栏:智慧协同智慧协同

内容如下:

代码语言:javascript
复制
import org.apache.tools.zip.ZipEntry;
import org.apache.tools.zip.ZipFile;
import org.apache.tools.zip.ZipOutputStream;

import java.io.*;
import java.util.Enumeration;

/**
 * 解压压缩工具类
 * Created by fangshuai on 2014-09-12-0012.
 */
public class ZipUtils {
    public static final String ENCODING_DEFAULT = "UTF-8";

    public static final int BUFFER_SIZE_DIFAULT = 128;

    public static void makeZip(String[] inFilePaths, String zipPath)
            throws Exception {
        makeZip(inFilePaths, zipPath, ENCODING_DEFAULT);
    }

    public static void makeZip(String[] inFilePaths, String zipPath,
                               String encoding) throws Exception {
        File[] inFiles = new File[inFilePaths.length];
        for (int i = 0; i < inFilePaths.length; i++) {
            inFiles[i] = new File(inFilePaths[i]);
        }
        makeZip(inFiles, zipPath, encoding);
    }

    public static void makeZip(File[] inFiles, String zipPath) throws Exception {
        makeZip(inFiles, zipPath, ENCODING_DEFAULT);
    }

    public static void makeZip(File[] inFiles, String zipPath, String encoding)
            throws Exception {
        ZipOutputStream zipOut = new ZipOutputStream(new BufferedOutputStream(new FileOutputStream(zipPath)));
        zipOut.setEncoding(encoding);
        for (int i = 0; i < inFiles.length; i++) {
            File file = inFiles[i];
            doZipFile(zipOut, file, file.getParent());
        }
        zipOut.flush();
        zipOut.close();
    }

    private static void doZipFile(ZipOutputStream zipOut, File file,
                                  String dirPath) throws IOException {
        if (file.isFile()) {
            BufferedInputStream bis = new BufferedInputStream(new FileInputStream(file));
            String zipName = file.getPath().substring(dirPath.length());
            while (zipName.charAt(0) == '\' || zipName.charAt(0) == '/') {
                zipName = zipName.substring(1);
            }
            ZipEntry entry = new ZipEntry(zipName);
            zipOut.putNextEntry(entry);
            byte[] buff = new byte[BUFFER_SIZE_DIFAULT];
            int size;
            while ((size = bis.read(buff, 0, buff.length)) != -1) {
                zipOut.write(buff, 0, size);
            }
            zipOut.closeEntry();
            bis.close();
        } else {
            File[] subFiles = file.listFiles();
            for (File subFile : subFiles) {
                doZipFile(zipOut, subFile, dirPath);
            }
        }
    }

    public static void unZip(String zipFilePath, String storePath) throws IOException {
        unZip(new File(zipFilePath), storePath);
    }

    public static void unZip(File zipFile, String storePath) throws IOException {
        if (!new File(storePath).exists()) {
            new File(storePath).mkdirs();
        }

        ZipFile zip = new ZipFile(zipFile);
        Enumeration<ZipEntry> entries = zip.getEntries();
        while (entries.hasMoreElements()) {
            ZipEntry zipEntry = entries.nextElement();
            if (zipEntry.isDirectory()) {
                // TODO
            } else {
                String zipEntryName = zipEntry.getName();
                if (zipEntryName.indexOf(File.separator) > 0) {
                    String zipEntryDir = zipEntryName.substring(0, zipEntryName.lastIndexOf(File.separator) + 1);
                    String unzipFileDir = storePath + File.separator + zipEntryDir;
                    File unzipFileDirFile = new File(unzipFileDir);
                    if (!unzipFileDirFile.exists()) {
                        unzipFileDirFile.mkdirs();
                    }
                }

                InputStream is = zip.getInputStream(zipEntry);
                File entryFile = new File(storePath + File.separator + zipEntryName);
                if(!entryFile.getParentFile().exists())
                    entryFile.getParentFile().mkdirs();
                FileOutputStream fos = new FileOutputStream(entryFile);
                byte[] buff = new byte[BUFFER_SIZE_DIFAULT];
                int size;
                while ((size = is.read(buff)) > 0) {
                    fos.write(buff, 0, size);
                }
                fos.flush();
                fos.close();
                is.close();
            }
        }
    }

    public static void main(String[] args) throws Exception {
        String rootDir = "D:\SWFTools";
        String zipPath = "D:\SWFTools.zip";
        //makeZip(new String[]{rootDir}, zipPath);
        unZip(zipPath, "D:\temp");
    }
}
本文参与 腾讯云自媒体分享计划,分享自作者个人站点/博客。
原始发表:2015.05.11 ,如有侵权请联系 cloudcommunity@tencent.com 删除

本文分享自 作者个人站点/博客 前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文参与 腾讯云自媒体分享计划  ,欢迎热爱写作的你一起参与!

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档