【无标题】

zip导出_zip解压

import java.io.BufferedInputStream;
import java.io.BufferedOutputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.util.Enumeration;
import java.util.zip.ZipEntry;
import java.util.zip.ZipFile;
import java.util.zip.ZipOutputStream;


public class JdkZipUtils {

    public static final int BUFFER_SIZE_DIFAULT = 128;

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

    /**
     * 文件打包导出
     *
     * @param inFiles     导出文件
     * @param zipFilePath 导出地址
     * @throws Exception
     */
    public static void makeZip(File[] inFiles, String zipFilePath)
            throws Exception {
        ZipOutputStream zipOut = new ZipOutputStream(new BufferedOutputStream(
                new FileOutputStream(zipFilePath)));
        for (int i = 0; i < inFiles.length; i++) {
            doZipFile(zipOut, inFiles[i], inFiles[i].getParent());
        }
        zipOut.flush();
        zipOut.close();
    }

    private static void doZipFile(ZipOutputStream zipOut, File file,
                                  String dirPath) throws FileNotFoundException, 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);
    }

    /**
     * 文件解压
     *
     * @param zipFile   zip文件
     * @param storePath 解压后存放位置
     * @throws IOException
     */
    public static void unZip(File zipFile, String storePath) throws IOException {
        if (new File(storePath).exists()) {
            new File(storePath).delete();
        }
        new File(storePath).mkdirs();

        ZipFile zip = new ZipFile(zipFile);
        Enumeration<? extends ZipEntry> entries = zip.entries();
        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);
                FileOutputStream fos = new FileOutputStream(new File(storePath
                        + File.separator + zipEntryName));
                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();
            }
        }
    }

}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值