package com.se.nsl.utils;
|
|
import cn.hutool.core.util.CharsetUtil;
|
import cn.hutool.core.util.ZipUtil;
|
|
import java.io.*;
|
import java.nio.file.Files;
|
import java.nio.file.Path;
|
import java.nio.file.Paths;
|
import java.util.ArrayList;
|
import java.util.Enumeration;
|
import java.util.List;
|
import java.util.zip.ZipEntry;
|
import java.util.zip.ZipFile;
|
import java.util.zip.ZipOutputStream;
|
|
/**
|
* zip
|
*/
|
public class ZipUtils {
|
// 解压zip格式
|
public static void unzip(String zipFilePath, String destDir) {
|
//所有的文件路径
|
List<String> filePaths = new ArrayList<>();
|
|
File destDirectory = new File(destDir);
|
if (!destDirectory.exists()) {
|
if (!destDirectory.mkdirs()) {
|
}
|
}
|
try (ZipFile zipFile = new ZipFile(zipFilePath)) {
|
Enumeration<? extends ZipEntry> entries = zipFile.entries();
|
|
while (entries.hasMoreElements()) {
|
ZipEntry entry = entries.nextElement();
|
String filePath = destDir + File.separator + entry.getName();
|
// 确保目标文件路径的父目录存在
|
File entryDestination = new File(filePath);
|
File parentDirectory = entryDestination.getParentFile();
|
if (parentDirectory != null && !parentDirectory.exists()) {
|
parentDirectory.mkdirs();
|
}
|
if (!entry.isDirectory()) {
|
// 提取文件
|
try (InputStream is = zipFile.getInputStream(entry);
|
BufferedOutputStream bos = new BufferedOutputStream(new FileOutputStream(filePath))) {
|
byte[] bytesIn = new byte[4096];
|
int read;
|
while ((read = is.read(bytesIn)) != -1) {
|
bos.write(bytesIn, 0, read);
|
}
|
}
|
// 打印文件路径
|
filePaths.add(filePath);
|
} else {
|
// 如果是目录,则创建目录(注意:在上面的mkdirs调用中已经处理了)
|
// 这里也可以选择打印目录路径
|
}
|
}
|
} catch (Exception e) {
|
e.printStackTrace();
|
System.out.println("GBK编码");
|
try {
|
File file = ZipUtil.unzip(zipFilePath, destDir, CharsetUtil.CHARSET_GBK);
|
File[] file1 = file.listFiles();
|
for (int i = 0; i < file1.length; i++) {
|
filePaths.add(file1[i].getAbsolutePath());
|
}
|
} catch (Exception ex) {
|
e.printStackTrace();
|
}
|
}
|
}
|
|
public static void zip(String sourceDirPath, String zipFilePath) throws IOException {
|
Path sourceDir = Paths.get(sourceDirPath);
|
try (ZipOutputStream zipOutputStream = new ZipOutputStream(new FileOutputStream(zipFilePath))) {
|
Files.walk(sourceDir).forEach(path -> {
|
try {
|
String zipEntryName = sourceDir.relativize(path).toString();
|
if (Files.isDirectory(path)) {
|
// 如果是目录,创建一个空的ZIP目录
|
if (!zipEntryName.isEmpty()) {
|
zipOutputStream.putNextEntry(new ZipEntry(zipEntryName + "/"));
|
}
|
} else {
|
// 如果是文件,写入ZIP文件
|
zipOutputStream.putNextEntry(new ZipEntry(zipEntryName));
|
Files.copy(path, zipOutputStream);
|
}
|
zipOutputStream.closeEntry();
|
} catch (IOException e) {
|
e.printStackTrace();
|
}
|
});
|
}
|
}
|
|
public static void toZarr(String sourceDir, String zipFile) {
|
try {
|
zip(sourceDir, zipFile);
|
System.out.println("文件夹已成功压缩为: " + zipFile);
|
updateFileSuffix(zipFile, "zarr");
|
deleteFile(sourceDir);
|
} catch (IOException e) {
|
e.printStackTrace();
|
}
|
}
|
|
public static void updateFileSuffix(String filePath, String newSuffix) {
|
// 创建File对象
|
File file = new File(filePath);
|
|
// 检查文件是否存在
|
if (file.exists()) {
|
// 获取文件名和后缀
|
String fileName = file.getName();
|
int dotIndex = fileName.lastIndexOf('.');
|
if (dotIndex > 0) {
|
// 分离文件名和后缀
|
String name = fileName.substring(0, dotIndex);
|
// 组合新的文件名
|
String newFileName = name + '.' + newSuffix;
|
|
// 重命名文件
|
File newFile = new File(file.getParent(), newFileName);
|
if (file.renameTo(newFile)) {
|
System.out.println("文件重命名成功:" + newFile.getName());
|
} else {
|
System.out.println("文件重命名失败。");
|
}
|
} else {
|
System.out.println("文件没有后缀。");
|
}
|
} else {
|
System.out.println("文件不存在。");
|
}
|
}
|
|
public static void deleteFile(String path) {
|
// 目标文件夹的路径
|
File directory = new File(path);
|
// 检查文件夹是否存在
|
if (!directory.exists()) {
|
System.out.println("文件夹不存在!");
|
return; // 文件夹不存在,退出程序
|
}
|
// 调用deleteDirectory方法删除文件夹及其内容
|
if (deleteDirectory(directory)) {
|
System.out.println("文件夹已成功删除!");
|
} else {
|
System.out.println("文件夹删除失败!");
|
}
|
}
|
|
// 递归删除文件夹及其内容的方法
|
public static boolean deleteDirectory(File directory) {
|
if (directory.isDirectory()) {
|
File[] files = directory.listFiles();
|
if (files != null) {
|
for (File file : files) {
|
// 递归处理
|
deleteDirectory(file);
|
}
|
}
|
}
|
// 删除当前文件或文件夹
|
return directory.delete();
|
}
|
|
}
|