| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147 |
- package com.uas.report.util;
- import java.io.File;
- import java.io.FileInputStream;
- import java.io.FileOutputStream;
- import java.io.IOException;
- import java.io.InputStream;
- import java.io.OutputStream;
- import java.util.Enumeration;
- import java.util.zip.ZipEntry;
- import java.util.zip.ZipFile;
- import java.util.zip.ZipOutputStream;
- import org.apache.log4j.Logger;
- import com.alibaba.druid.util.StringUtils;
- import com.uas.report.core.exception.ReportException;
- /**
- * 压缩工具类
- *
- * @author sunyj
- * @since 2016年11月1日 下午4:51:38
- */
- public class ZipUtils {
- private static final Logger logger = Logger.getLogger(ZipUtils.class);
- /**
- * 压缩文件夹
- *
- * @param sourceFolderPath
- * 将压缩的文件夹
- * @param zipFilePath
- * 压缩后的压缩包路径
- */
- public static void zipFolder(String sourceFolderPath, String zipFilePath) {
- if (StringUtils.isEmpty(sourceFolderPath) || StringUtils.isEmpty(zipFilePath)) {
- return;
- }
- File folder = new File(sourceFolderPath);
- if (!folder.exists() || !folder.isDirectory()) {
- throw new ReportException("路径不存在或并非文件夹:" + sourceFolderPath);
- }
- File[] files = folder.listFiles();
- if (files.length < 0) {
- throw new ReportException("空文件夹:" + sourceFolderPath);
- }
- try {
- ZipOutputStream zipOutputStream = new ZipOutputStream(new FileOutputStream(zipFilePath));
- putNextEntryFromFolder(zipOutputStream, folder, "");
- zipOutputStream.close();
- logger.info("Zip finished to... " + zipFilePath);
- } catch (IOException e) {
- e.printStackTrace();
- }
- }
- /**
- * 递归将文件夹下的文件进行压缩
- *
- * @param zipOutputStream
- * 压缩包的输出流
- * @param folder
- * 需要压缩的文件夹
- * @param prefix
- * 为保持压缩后的路径层次不变,所记录的当前文件夹的相对层级
- */
- private static void putNextEntryFromFolder(ZipOutputStream zipOutputStream, File folder, String prefix) {
- File[] files = folder.listFiles();
- try {
- for (File file : files) {
- if (file.isDirectory()) {
- putNextEntryFromFolder(zipOutputStream, file, prefix + file.getName() + File.separator);
- } else {
- zipOutputStream.putNextEntry(new ZipEntry(prefix + file.getName()));
- InputStream inputStream = new FileInputStream(file);
- int b;
- while ((b = inputStream.read()) != -1) {
- zipOutputStream.write(b);
- }
- inputStream.close();
- }
- logger.info("Ziped from... " + file.getPath());
- }
- } catch (IOException e) {
- e.printStackTrace();
- }
- }
- /**
- * 解压缩
- *
- * @param zipFilePath
- * 将解压缩的压缩包路径
- * @param zipFilePath
- * 解压缩后的文件路径
- */
- public static void unzip(String zipFilePath, String folderPath) {
- if (StringUtils.isEmpty(zipFilePath) || StringUtils.isEmpty(folderPath)) {
- return;
- }
- File file = new File(zipFilePath);
- if (!file.exists()) {
- throw new ReportException("压缩包不存在:" + zipFilePath);
- }
- File folder = new File(folderPath);
- if (!folder.exists()) {
- folder.mkdirs();
- }
- if (!folder.isDirectory()) {
- throw new ReportException("并非文件夹:" + folderPath);
- }
- try {
- ZipFile zipFile = new ZipFile(file);
- Enumeration<? extends ZipEntry> zipEntries = zipFile.entries();
- while (zipEntries.hasMoreElements()) {
- ZipEntry zipEntry = zipEntries.nextElement();
- if (zipEntry.isDirectory()) {
- continue;
- }
- InputStream inputStream = zipFile.getInputStream(zipEntry);
- File outFile = new File(folder.getPath() + File.separator + zipEntry.getName());
- if (!outFile.getParentFile().exists()) {
- outFile.getParentFile().mkdirs();
- }
- String fileName = folder.getPath() + File.separator + zipEntry.getName();
- OutputStream outputStream = new FileOutputStream(fileName);
- int b;
- while ((b = inputStream.read()) != -1) {
- outputStream.write(b);
- }
- inputStream.close();
- outputStream.close();
- logger.info("Unziped to... " + fileName);
- }
- zipFile.close();
- } catch (IOException e) {
- e.printStackTrace();
- }
- logger.info("Unzip finished from... " + zipFilePath);
- }
- }
|