博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
java中自己常用到的工具类-压缩解压zip文件
阅读量:6312 次
发布时间:2019-06-22

本文共 8498 字,大约阅读时间需要 28 分钟。

package com.ricoh.rapp.ezcx.admintoolweb.util;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.io.OutputStream;import java.nio.charset.Charset;import java.util.ArrayList;import java.util.Enumeration;import java.util.List;import java.util.zip.ZipEntry;import java.util.zip.ZipFile;import java.util.zip.ZipOutputStream;public class ZipUtil {    private static final int BUFFER_SIZE = 2 * 1024;    /**     * @param srcDir     *            需要压缩的文件夹     * @param zipPath     *            压缩文件目录     * @param zipFileName     *            压缩文件的名称     * @throws RuntimeException     * @throws FileNotFoundException     */    public static void toZip(String srcDir, String zipPath, String zipFileName)            throws RuntimeException, FileNotFoundException {        long start = System.currentTimeMillis();        File zipDir = new File(zipPath);        if (!zipDir.exists() || !zipDir.isDirectory()) {            zipDir.mkdirs();        }        File zipFile = new File(zipPath, zipFileName);        FileOutputStream out = new FileOutputStream(zipFile);        ZipOutputStream zos = null;        try {            zos = new ZipOutputStream(out);            File sourceFile = new File(srcDir);            compress(sourceFile, zos, sourceFile.getName(), false);            long end = System.currentTimeMillis();            System.out.println("压缩完成,耗时:" + (end - start) + " ms");        } catch (Exception e) {            throw new RuntimeException("zip error from ZipUtils", e);        } finally {            if (zos != null) {                try {                    zos.close();                } catch (IOException e) {                    e.printStackTrace();                }            }            if (out != null) {                try {                    out.close();                } catch (IOException e) {                    e.printStackTrace();                }            }        }    }    /**     * @param sourceFile     *            需要压缩的文件目录     * @param zos     *            zip输出流     * @param name     *            压缩后的zip名称     * @param KeepDirStructure     *            是否保留原来的目录结构(false:不保留;true:保留)     * @throws Exception     */    private static void compress(File sourceFile, ZipOutputStream zos, String name, boolean KeepDirStructure)            throws Exception {        byte[] buf = new byte[BUFFER_SIZE];        if (sourceFile.isFile()) {            zos.putNextEntry(new ZipEntry(name));            int len;            FileInputStream in = new FileInputStream(sourceFile);            while ((len = in.read(buf)) != -1) {                zos.write(buf, 0, len);            }            zos.closeEntry();            in.close();        } else {            File[] listFiles = sourceFile.listFiles();            if (listFiles == null || listFiles.length == 0) {                zos.putNextEntry(new ZipEntry(name + "/"));                zos.closeEntry();            } else {                for (File file : listFiles) {                    if (KeepDirStructure) {                        compress(file, zos, name + "/" + file.getName(), true);                    } else {                        compress(file, zos, file.getName(), true);                    }                }            }        }    }    /**     * 压缩成ZIP 方法2     *      * @param srcFiles     *            需要压缩的文件列表     * @param out     *            压缩文件输出流     * @throws RuntimeException     *      */    public static void toZip(File[] files, String zipPath, String zipFileName) throws RuntimeException {        long start = System.currentTimeMillis();        ZipOutputStream zos = null;        FileOutputStream out = null;        try {            File zipDir = new File(zipPath);            if (!zipDir.exists() || !zipDir.isDirectory()) {                zipDir.mkdirs();            }            File zipFile = new File(zipPath, zipFileName);            out = new FileOutputStream(zipFile);            zos = new ZipOutputStream(out);            for (File srcFile : files) {                byte[] buf = new byte[BUFFER_SIZE];                zos.putNextEntry(new ZipEntry(srcFile.getName()));                int len;                FileInputStream in = new FileInputStream(srcFile);                while ((len = in.read(buf)) != -1) {                    zos.write(buf, 0, len);                }                zos.closeEntry();                in.close();            }            long end = System.currentTimeMillis();            System.out.println("压缩完成,耗时:" + (end - start) + " ms");        } catch (Exception e) {            throw new RuntimeException("zip error from ZipUtils", e);        } finally {            if (zos != null) {                try {                    zos.close();                } catch (IOException e) {                    e.printStackTrace();                }            }            if (out != null) {                try {                    out.close();                } catch (IOException e) {                    e.printStackTrace();                }            }        }    }    /**     * 解压文件到指定目录     */    @SuppressWarnings("rawtypes")    public static void unZipFiles(String zipPath, String descDir) {        File zipFile = new File(zipPath);        File pathFile = new File(descDir);        if (!pathFile.exists()) {            pathFile.mkdirs();        }        // 解决zip文件中有中文目录或者中文文件        ZipFile zip = null;        try {            zip = new ZipFile(zipFile, Charset.forName("GBK"));            for (Enumeration entries = zip.entries(); entries.hasMoreElements();) {                // 输出文件路径信息                FileOutputStream out = null;                InputStream in = null;                try {                    ZipEntry entry = (ZipEntry) entries.nextElement();                    String zipEntryName = entry.getName();                    in = zip.getInputStream(entry);                    String outPath = descDir + File.separator + zipEntryName;                    // 判断路径是否存在,不存在则创建文件路径                    // File file = new File(outPath.substring(0, outPath.lastIndexOf("/")));                    File file = new File(outPath.substring(0, outPath.lastIndexOf(File.separator)));                    if (!file.exists()) {                        file.mkdirs();                    }                    // 判断文件全路径是否为文件夹,如果是上面已经上传,不需要解压                    if (new File(outPath).isDirectory()) {                        continue;                    }                    out = new FileOutputStream(outPath);                    byte[] buf1 = new byte[1024];                    int len;                    while ((len = in.read(buf1)) > 0) {                        out.write(buf1, 0, len);                    }                } catch (Exception e) {                    e.printStackTrace();                } finally {                    if (out != null) {                        try {                            out.close();                        } catch (IOException e) {                            e.printStackTrace();                        }                    }                    if (in != null) {                        try {                            in.close();                        } catch (IOException e) {                            e.printStackTrace();                        }                    }                }            }        } catch (IOException e1) {            e1.printStackTrace();        } finally {            if (zip != null) {                try {                    zip.close();                } catch (IOException e) {                    e.printStackTrace();                }            }        }    }    /**     * 测试     */    public static void main(String[] args) throws FileNotFoundException {        /*         * String sourFile = "D:/testzip"; String zipPath = "D:/wl"; String zipFileName         * = "tow.zip"; ZipUtil.toZip(sourFile, zipPath, zipFileName);         */        /*         * File sfile = new File(sourFile); List
fileList = new ArrayList<>(); * for(File f : sfile.listFiles()) { fileList.add(f); } toZip(sfile.listFiles(), * zipPath, zipFileName); */ String zipPath1 = "d:/testunzip/user.zip"; String zipPath2 = "D:/testunzip/photos.zip"; String descDir1 = "d:/testunzip/"; String descDir2 = "d:/testunzip/ps"; unZipFiles(zipPath2, descDir2); }}

 

转载于:https://www.cnblogs.com/wang-liang-blogs/p/10620730.html

你可能感兴趣的文章
xcode中没有autoSizing的设置
查看>>
字符编码
查看>>
企业应用:应用层查询接口设计
查看>>
浅谈Excel开发:十 Excel 开发中与线程相关的若干问题
查看>>
nfd指令的详细说明
查看>>
安装VisualSvn Server时遇到的问题
查看>>
不用Visual Studio,5分钟轻松实现一张报表
查看>>
人脸识别 开放书籍 下载地址
查看>>
Notepad++配置Python开发环境
查看>>
用户组概念 和 挂载 概念
查看>>
如何快速获取ADO连接字符串
查看>>
AspNetPager控件的最基本用法
查看>>
sessionKey
查看>>
高性能Javascript--脚本的无阻塞加载策略
查看>>
Java 编程的动态性, 第4部分: 用 Javassist 进行类转换--转载
查看>>
完毕port(CompletionPort)具体解释 - 手把手教你玩转网络编程系列之三
查看>>
iOS8 Push Notifications
查看>>
各大名企笔试及面经大全(程序猿必读)
查看>>
Oracle 连接、会话数的查看,修改
查看>>
Python使用QRCode模块生成二维码
查看>>