|
|
@@ -18,7 +18,6 @@ import java.util.zip.ZipOutputStream;
|
|
|
import org.slf4j.Logger;
|
|
|
import org.slf4j.LoggerFactory;
|
|
|
|
|
|
-import com.alibaba.druid.util.StringUtils;
|
|
|
import com.uas.report.core.exception.ReportException;
|
|
|
|
|
|
/**
|
|
|
@@ -39,8 +38,9 @@ public class ZipUtils {
|
|
|
* @param fileFilter
|
|
|
* 文件过滤器,不压缩某些文件
|
|
|
* @return 压缩后的数据
|
|
|
+ * @throws IOException
|
|
|
*/
|
|
|
- public static byte[] zipFolder(String sourceFolderPath, FileFilter fileFilter) {
|
|
|
+ public static byte[] zipFolder(String sourceFolderPath, FileFilter fileFilter) throws IOException {
|
|
|
if (StringUtils.isEmpty(sourceFolderPath)) {
|
|
|
return null;
|
|
|
}
|
|
|
@@ -55,16 +55,12 @@ public class ZipUtils {
|
|
|
|
|
|
ByteArrayOutputStream outputStream = new ByteArrayOutputStream();
|
|
|
byte[] zipData = null;
|
|
|
- try {
|
|
|
- ZipOutputStream zipOutputStream = new ZipOutputStream(outputStream);
|
|
|
- putNextEntryFromFolder(zipOutputStream, folder, "", fileFilter);
|
|
|
- zipOutputStream.close();
|
|
|
- logger.info("Zip done");
|
|
|
- zipData = outputStream.toByteArray();
|
|
|
- outputStream.close();
|
|
|
- } catch (IOException e) {
|
|
|
- logger.error("", e);
|
|
|
- }
|
|
|
+ ZipOutputStream zipOutputStream = new ZipOutputStream(outputStream);
|
|
|
+ putNextEntryFromFolder(zipOutputStream, folder, "", fileFilter);
|
|
|
+ zipOutputStream.close();
|
|
|
+ logger.info("Zip done");
|
|
|
+ zipData = outputStream.toByteArray();
|
|
|
+ outputStream.close();
|
|
|
return zipData;
|
|
|
}
|
|
|
|
|
|
@@ -77,8 +73,10 @@ public class ZipUtils {
|
|
|
* 压缩后的压缩包路径
|
|
|
* @param fileFilter
|
|
|
* 文件过滤器,不压缩某些文件
|
|
|
+ * @throws IOException
|
|
|
*/
|
|
|
- public static void zipFolder(String sourceFolderPath, String zipFilePath, FileFilter fileFilter) {
|
|
|
+ public static void zipFolder(String sourceFolderPath, String zipFilePath, FileFilter fileFilter)
|
|
|
+ throws IOException {
|
|
|
if (StringUtils.isEmpty(sourceFolderPath) || StringUtils.isEmpty(zipFilePath)) {
|
|
|
return;
|
|
|
}
|
|
|
@@ -91,14 +89,10 @@ public class ZipUtils {
|
|
|
throw new ReportException("空文件夹:" + sourceFolderPath);
|
|
|
}
|
|
|
|
|
|
- try {
|
|
|
- ZipOutputStream zipOutputStream = new ZipOutputStream(new FileOutputStream(zipFilePath));
|
|
|
- putNextEntryFromFolder(zipOutputStream, folder, "", fileFilter);
|
|
|
- zipOutputStream.close();
|
|
|
- logger.info("Zip finished to... " + zipFilePath);
|
|
|
- } catch (IOException e) {
|
|
|
- logger.error("", e);
|
|
|
- }
|
|
|
+ ZipOutputStream zipOutputStream = new ZipOutputStream(new FileOutputStream(zipFilePath));
|
|
|
+ putNextEntryFromFolder(zipOutputStream, folder, "", fileFilter);
|
|
|
+ zipOutputStream.close();
|
|
|
+ logger.info("Zip finished to... " + zipFilePath);
|
|
|
}
|
|
|
|
|
|
/**
|
|
|
@@ -112,31 +106,28 @@ public class ZipUtils {
|
|
|
* 为保持压缩后的路径层次不变,所记录的当前文件夹的相对层级
|
|
|
* @param fileFilter
|
|
|
* 文件过滤器,不压缩某些文件
|
|
|
+ * @throws IOException
|
|
|
*/
|
|
|
private static void putNextEntryFromFolder(ZipOutputStream zipOutputStream, File folder, String prefix,
|
|
|
- FileFilter fileFilter) {
|
|
|
+ FileFilter fileFilter) throws IOException {
|
|
|
File[] files = folder.listFiles();
|
|
|
- try {
|
|
|
- for (File file : files) {
|
|
|
- if (file.isDirectory()) {
|
|
|
- putNextEntryFromFolder(zipOutputStream, file, prefix + file.getName() + File.separator, fileFilter);
|
|
|
- } else {
|
|
|
- // 过滤某些文件
|
|
|
- if (fileFilter != null && !fileFilter.accept(file)) {
|
|
|
- continue;
|
|
|
- }
|
|
|
- zipOutputStream.putNextEntry(new ZipEntry(prefix + file.getName()));
|
|
|
- InputStream inputStream = new FileInputStream(file);
|
|
|
- int b;
|
|
|
- while ((b = inputStream.read()) != -1) {
|
|
|
- zipOutputStream.write(b);
|
|
|
- }
|
|
|
- inputStream.close();
|
|
|
+ for (File file : files) {
|
|
|
+ if (file.isDirectory()) {
|
|
|
+ putNextEntryFromFolder(zipOutputStream, file, prefix + file.getName() + File.separator, fileFilter);
|
|
|
+ } else {
|
|
|
+ // 过滤某些文件
|
|
|
+ if (fileFilter != null && !fileFilter.accept(file)) {
|
|
|
+ continue;
|
|
|
+ }
|
|
|
+ zipOutputStream.putNextEntry(new ZipEntry(prefix + file.getName()));
|
|
|
+ InputStream inputStream = new FileInputStream(file);
|
|
|
+ int b;
|
|
|
+ while ((b = inputStream.read()) != -1) {
|
|
|
+ zipOutputStream.write(b);
|
|
|
}
|
|
|
- logger.info("Ziped from... " + file.getPath());
|
|
|
+ inputStream.close();
|
|
|
}
|
|
|
- } catch (IOException e) {
|
|
|
- logger.error("", e);
|
|
|
+ logger.info("Ziped from... " + file.getPath());
|
|
|
}
|
|
|
}
|
|
|
|
|
|
@@ -147,36 +138,33 @@ public class ZipUtils {
|
|
|
* 压缩包数据
|
|
|
* @param folderPath
|
|
|
* 解压缩后的文件路径
|
|
|
+ * @throws IOException
|
|
|
*/
|
|
|
- public static void unzip(byte[] zipData, String folderPath) {
|
|
|
+ public static void unzip(byte[] zipData, String folderPath) throws IOException {
|
|
|
if (StringUtils.isEmpty(folderPath)) {
|
|
|
return;
|
|
|
}
|
|
|
|
|
|
- try {
|
|
|
- ZipInputStream zipInputStream = new ZipInputStream(new ByteArrayInputStream(zipData));
|
|
|
- ZipEntry zipEntry;
|
|
|
- while ((zipEntry = zipInputStream.getNextEntry()) != null) {
|
|
|
- if (zipEntry.isDirectory()) {
|
|
|
- continue;
|
|
|
- }
|
|
|
- File outFile = new File(folderPath + File.separator + zipEntry.getName());
|
|
|
- if (!outFile.getParentFile().exists()) {
|
|
|
- outFile.getParentFile().mkdirs();
|
|
|
- }
|
|
|
+ ZipInputStream zipInputStream = new ZipInputStream(new ByteArrayInputStream(zipData));
|
|
|
+ ZipEntry zipEntry;
|
|
|
+ while ((zipEntry = zipInputStream.getNextEntry()) != null) {
|
|
|
+ if (zipEntry.isDirectory()) {
|
|
|
+ continue;
|
|
|
+ }
|
|
|
+ File outFile = new File(folderPath + File.separator + zipEntry.getName());
|
|
|
+ if (!outFile.getParentFile().exists()) {
|
|
|
+ outFile.getParentFile().mkdirs();
|
|
|
+ }
|
|
|
|
|
|
- OutputStream outputStream = new FileOutputStream(outFile);
|
|
|
- int b;
|
|
|
- while ((b = zipInputStream.read()) != -1) {
|
|
|
- outputStream.write(b);
|
|
|
- }
|
|
|
- outputStream.close();
|
|
|
- logger.info("Unziped to... " + outFile.getPath());
|
|
|
+ OutputStream outputStream = new FileOutputStream(outFile);
|
|
|
+ int b;
|
|
|
+ while ((b = zipInputStream.read()) != -1) {
|
|
|
+ outputStream.write(b);
|
|
|
}
|
|
|
- zipInputStream.close();
|
|
|
- } catch (IOException e) {
|
|
|
- logger.error("", e);
|
|
|
+ outputStream.close();
|
|
|
+ logger.info("Unziped to... " + outFile.getPath());
|
|
|
}
|
|
|
+ zipInputStream.close();
|
|
|
logger.info("Unzip done");
|
|
|
}
|
|
|
|
|
|
@@ -187,8 +175,9 @@ public class ZipUtils {
|
|
|
* 将解压缩的压缩包路径
|
|
|
* @param zipFilePath
|
|
|
* 解压缩后的文件路径
|
|
|
+ * @throws IOException
|
|
|
*/
|
|
|
- public static void unzip(String zipFilePath, String folderPath) {
|
|
|
+ public static void unzip(String zipFilePath, String folderPath) throws IOException {
|
|
|
if (StringUtils.isEmpty(zipFilePath) || StringUtils.isEmpty(folderPath)) {
|
|
|
return;
|
|
|
}
|
|
|
@@ -205,34 +194,29 @@ public class ZipUtils {
|
|
|
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();
|
|
|
- }
|
|
|
+ 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);
|
|
|
+ String fileName = folder.getPath() + File.separator + zipEntry.getName();
|
|
|
+ OutputStream outputStream = new FileOutputStream(fileName);
|
|
|
+ int b;
|
|
|
+ while ((b = inputStream.read()) != -1) {
|
|
|
+ outputStream.write(b);
|
|
|
}
|
|
|
- zipFile.close();
|
|
|
- } catch (IOException e) {
|
|
|
- logger.error("", e);
|
|
|
+ inputStream.close();
|
|
|
+ outputStream.close();
|
|
|
}
|
|
|
+ zipFile.close();
|
|
|
logger.info("Unzip finished from... " + zipFilePath);
|
|
|
}
|
|
|
}
|