|
@@ -0,0 +1,143 @@
|
|
|
|
|
+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 completed");
|
|
|
|
|
+ } 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) {
|
|
|
|
|
+ logger.info("zip... " + prefix + file.getName());
|
|
|
|
|
+ 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();
|
|
|
|
|
+ }
|
|
|
|
|
+ }
|
|
|
|
|
+ } 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();
|
|
|
|
|
+ logger.info("unzip... " + zipEntry.getName());
|
|
|
|
|
+ InputStream inputStream = zipFile.getInputStream(zipEntry);
|
|
|
|
|
+ File outFile = new File(folder.getPath() + File.separator + zipEntry.getName());
|
|
|
|
|
+ if (!outFile.getParentFile().exists()) {
|
|
|
|
|
+ outFile.getParentFile().mkdirs();
|
|
|
|
|
+ }
|
|
|
|
|
+ OutputStream outputStream = new FileOutputStream(
|
|
|
|
|
+ folder.getPath() + File.separator + zipEntry.getName());
|
|
|
|
|
+ int b;
|
|
|
|
|
+ while ((b = inputStream.read()) != -1) {
|
|
|
|
|
+ outputStream.write(b);
|
|
|
|
|
+ }
|
|
|
|
|
+ inputStream.close();
|
|
|
|
|
+ outputStream.close();
|
|
|
|
|
+ }
|
|
|
|
|
+ zipFile.close();
|
|
|
|
|
+ } catch (IOException e) {
|
|
|
|
|
+ e.printStackTrace();
|
|
|
|
|
+ }
|
|
|
|
|
+ logger.info("unzip completed");
|
|
|
|
|
+ }
|
|
|
|
|
+}
|