package com.mms.webService.util;
import org.apache.poi.ss.usermodel.CellType;
import org.apache.poi.xssf.usermodel.*;
import java.io.*;
import java.nio.charset.Charset;
import java.nio.file.*;
import java.nio.file.attribute.BasicFileAttributes;
import java.util.*;
import java.util.stream.Collectors;
import java.util.zip.ZipEntry;
import java.util.zip.ZipFile;
import java.util.zip.ZipInputStream;
import java.util.zip.ZipOutputStream;
import static javax.swing.UIManager.getString;
public class ZipUtils {
public static void main(String[] args) throws Exception {
// 解压文件
Path file = Paths.get("D:\ALL\Java\MMS.zip"); //绝对路径
System.out.println("压缩文件:"+file);
Path targetDir = Paths.get("D:\ALL\Java\");
System.out.println("解压文件输出的目录:"+targetDir);
ZipUtils.unPacket(file,targetDir);
// 压缩指定的文件
// Path folder1 = Paths.get("C:\Users\hp\Desktop\1.txt");
// Path folder2 = Paths.get("C:\Users\hp\Desktop\2.txt");
// Path[] arr = {folder1,folder2};
// Path zipFile = Paths.get("C:\Users\hp\Desktop\www.zip");
// ZipUtils.packet(arr,zipFile);
// 压缩指定的目录
}
public static void unPacket(Path file, Path targetDir) throws IOException {
System.out.println("解压文件start......");
if (!Files.exists(targetDir)) {
Files.createDirectories(targetDir);
}
// 创建zip对象
ZipFile zipFile = new ZipFile(file.toFile());
try {
// 读取zip流
try(ZipInputStream zipInputStream = new ZipInputStream(Files.newInputStream(file))){
Charset.forName("GBK");
ZipEntry zipEntry = null;
// 遍历每一个zip项
while ((zipEntry = zipInputStream.getNextEntry()) != null) {
// 获取zip项目名称
String entryName = zipEntry.getName();
// 构建绝对路径
Path entryFile = targetDir.resolve(entryName);
if(zipEntry.isDirectory()) { // 文件夹
if (!Files.isDirectory(entryFile)) {
Files.createDirectories(entryFile);
}
} else { // 文件
// 读取zip项数据流
// try(InputStream zipEntryInputStream = zipFile.getInputStream(zipEntry)){
// try(OutputStream fileOutputStream = Files.newOutputStream(entryFile, StandardOpenOption.CREATE_NEW)){
// byte[] buffer = new byte[4096];
// int length = 0;
// while ((length = zipEntryInputStream.read(buffer)) != -1) {
// fileOutputStream.write(buffer, 0, length);
// }
// fileOutputStream.flush();
// }
// }
}
// File file1= new File(entryFile.toString());
// if (!file1.exists()){
// throw new Exception("文件不存在!");
// }
// InputStream in = new FileInputStream(file1);
InputStream in =zipFile.getInputStream(zipEntry);
// 读取整个Excel
XSSFWorkbook sheets = new XSSFWorkbook(in);
// 获取第一个表单Sheet
XSSFSheet sheetAt = sheets.getSheetAt(0);
ArrayList