123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147 |
- package com.jkcredit.invoice.util;
- import java.io.*;
- import java.nio.charset.Charset;
- import java.util.Enumeration;
- import java.util.zip.ZipEntry;
- import java.util.zip.ZipFile;
- import java.util.zip.ZipOutputStream;
- /**
- * ZipUtils
- *
- * @author ZENG.XIAO.YAN
- * @version v1.0
- * @date 2017年11月19日 下午7:16:08
- */
- public class ZipUtils {
- private static final int BUFFEREDSIZE = 1024;
- /**
- * 压缩文件
- *
- * @param zipFileName 保存的压缩包文件路径
- * @param filePath 需要压缩的文件夹或者文件路径
- * @param isDelete 是否删除源文件
- * @throws Exception
- */
- public static void zip(String zipFileName, String filePath, boolean isDelete) throws Exception {
- zip(zipFileName, new File(filePath), isDelete);
- }
- /**
- * 压缩文件
- *
- * @param zipFileName 保存的压缩包文件路径
- * @param inputFile 需要压缩的文件夹或者文件
- * @param isDelete 是否删除源文件
- * @throws Exception
- */
- public static void zip(String zipFileName, File inputFile, boolean isDelete) throws Exception {
- ZipOutputStream out = new ZipOutputStream(new FileOutputStream(zipFileName));
- if (!inputFile.exists()) {
- throw new FileNotFoundException("在指定路径未找到需要压缩的文件!");
- }
- zip(out, inputFile, "", isDelete);
- out.close();
- }
- /**
- * 递归压缩方法
- *
- * @param out 压缩包输出流
- * @param inputFile 需要压缩的文件
- * @param base 压缩的路径
- * @param isDelete 是否删除源文件
- * @throws Exception
- */
- private static void zip(ZipOutputStream out, File inputFile, String base, boolean isDelete) throws Exception {
- // 如果是目录
- if (inputFile.isDirectory()) {
- File[] inputFiles = inputFile.listFiles();
- out.putNextEntry(new ZipEntry(base + "/"));
- base = base.length() == 0 ? "" : base + "/";
- for (int i = 0; i < inputFiles.length; i++) {
- zip(out, inputFiles[i], base + inputFiles[i].getName(), isDelete);
- }
- } else { // 如果是文件
- if (base.length() > 0) {
- out.putNextEntry(new ZipEntry(base));
- } else {
- out.putNextEntry(new ZipEntry(inputFile.getName()));
- }
- FileInputStream in = new FileInputStream(inputFile);
- try {
- int len;
- byte[] buff = new byte[BUFFEREDSIZE];
- while ((len = in.read(buff)) != -1) {
- out.write(buff, 0, len);
- }
- } catch (IOException e) {
- throw e;
- } finally {
- in.close();
- }
- }
- if (isDelete) {
- inputFile.delete();
- }
- }
- /**
- * 解压文件
- *
- * @param zipFilePath:需要解压缩的文件
- * @param descDir:解压后的目标目录
- */
- public static void unZipFiles(String zipFilePath, String descDir) throws IOException {
- File destFile = new File(descDir);
- if (!destFile.exists()) {
- destFile.mkdirs();
- }
- // 解决zip文件中有中文目录或者中文文件
- ZipFile zip = new ZipFile(zipFilePath, Charset.forName("GBK"));
- for (Enumeration entries = zip.entries(); entries.hasMoreElements(); ) {
- ZipEntry entry = (ZipEntry) entries.nextElement();
- InputStream in = zip.getInputStream(entry);
- String curEntryName = entry.getName();
- // 判断文件名路径是否存在文件夹
- int endIndex = curEntryName.lastIndexOf('/');
- // 替换
- String outPath = (descDir + curEntryName).replaceAll("\\\\", "/");
- if (endIndex != -1) {
- File file = new File(outPath.substring(0, outPath.lastIndexOf("/")));
- if (!file.exists()) {
- file.mkdirs();
- }
- }
- // 判断文件全路径是否为文件夹,如果是上面已经上传,不需要解压
- File outFile = new File(outPath);
- if (outFile.isDirectory()) {
- continue;
- }
- OutputStream out = new FileOutputStream(outPath);
- byte[] buf1 = new byte[1024];
- int len;
- while ((len = in.read(buf1)) > 0) {
- out.write(buf1, 0, len);
- }
- in.close();
- out.close();
- }
- zip.close();
- }
- public static void main(String[] args) {
- try {
- ZipUtils.unZipFiles("E:\\test\\test.zip", "E:\\test\\out\\");
- } catch (Exception e) {
- e.printStackTrace();
- }
- }
- }
|