|
@@ -1,5 +1,7 @@
|
|
|
package com.fdkankan.manage.common;
|
|
package com.fdkankan.manage.common;
|
|
|
|
|
|
|
|
|
|
+import cn.hutool.core.io.FileUtil;
|
|
|
|
|
+import cn.hutool.core.io.IoUtil;
|
|
|
import net.lingala.zip4j.*;
|
|
import net.lingala.zip4j.*;
|
|
|
import net.lingala.zip4j.exception.ZipException;
|
|
import net.lingala.zip4j.exception.ZipException;
|
|
|
import net.lingala.zip4j.model.FileHeader;
|
|
import net.lingala.zip4j.model.FileHeader;
|
|
@@ -11,106 +13,25 @@ public class ZipFileReaderUtil {
|
|
|
|
|
|
|
|
public static final String zipPassword = "a3ad34136de359536af553f9e7f3cefd";
|
|
public static final String zipPassword = "a3ad34136de359536af553f9e7f3cefd";
|
|
|
|
|
|
|
|
- public static String getFileStringFromZip(String zipPath, String targetFile) throws Exception{
|
|
|
|
|
- return new String(getFileBytesFromZip(zipPath,targetFile,zipPassword));
|
|
|
|
|
- }
|
|
|
|
|
- public static String getFileStringFromZip(String zipPath, String targetFile, String password) throws Exception{
|
|
|
|
|
- return new String(getFileBytesFromZip(zipPath,targetFile,password));
|
|
|
|
|
- }
|
|
|
|
|
-
|
|
|
|
|
- /**
|
|
|
|
|
- * 从加密ZIP获取文件内容为字节数组
|
|
|
|
|
- */
|
|
|
|
|
- public static byte[] getFileBytesFromZip(String zipPath, String targetFile, String password)
|
|
|
|
|
- throws IOException {
|
|
|
|
|
-
|
|
|
|
|
- try (ZipFile zipFile = new ZipFile(zipPath);
|
|
|
|
|
- InputStream is = getFileStreamFromZip(zipFile, targetFile, password)) {
|
|
|
|
|
-
|
|
|
|
|
- ByteArrayOutputStream buffer = new ByteArrayOutputStream();
|
|
|
|
|
- int nRead;
|
|
|
|
|
- byte[] data = new byte[1024];
|
|
|
|
|
- while ((nRead = is.read(data, 0, data.length)) != -1) {
|
|
|
|
|
- buffer.write(data, 0, nRead);
|
|
|
|
|
|
|
+ public static String readUtf8(String zipPath, String fileName) throws Exception {
|
|
|
|
|
+ ZipFile zipFile = new ZipFile(zipPath);
|
|
|
|
|
+ zipFile.setPassword(zipPassword.toCharArray());
|
|
|
|
|
+ // 读取所有条目(文件头)
|
|
|
|
|
+ for (FileHeader header : (List<FileHeader>)zipFile.getFileHeaders()) {
|
|
|
|
|
+ // 文件名(不含目录)
|
|
|
|
|
+ String filePath = header.getFileName();
|
|
|
|
|
+ String headerFileName = FileUtil.getName(filePath);
|
|
|
|
|
+ if (filePath.contains("backup") || !fileName.equals(headerFileName)) {
|
|
|
|
|
+ continue;
|
|
|
}
|
|
}
|
|
|
- return buffer.toByteArray();
|
|
|
|
|
- }
|
|
|
|
|
- }
|
|
|
|
|
-
|
|
|
|
|
- /**
|
|
|
|
|
- * 获取文件输入流
|
|
|
|
|
- */
|
|
|
|
|
- private static InputStream getFileStreamFromZip(ZipFile zipFile, String targetFile, String password)
|
|
|
|
|
- throws IOException {
|
|
|
|
|
-
|
|
|
|
|
- if (zipFile.isEncrypted() && password != null) {
|
|
|
|
|
- zipFile.setPassword(password.toCharArray());
|
|
|
|
|
- }
|
|
|
|
|
-
|
|
|
|
|
- FileHeader fileHeader = findFileHeader(zipFile, targetFile);
|
|
|
|
|
- if (fileHeader == null) {
|
|
|
|
|
- throw new IOException("File not found: " + targetFile);
|
|
|
|
|
- }
|
|
|
|
|
-
|
|
|
|
|
- return zipFile.getInputStream(fileHeader);
|
|
|
|
|
- }
|
|
|
|
|
-
|
|
|
|
|
- /**
|
|
|
|
|
- * 查找文件头(支持路径匹配)
|
|
|
|
|
- */
|
|
|
|
|
- private static FileHeader findFileHeader(ZipFile zipFile, String targetFile) throws IOException {
|
|
|
|
|
- // 首先尝试精确匹配
|
|
|
|
|
- FileHeader header = zipFile.getFileHeader(targetFile);
|
|
|
|
|
- if (header != null) {
|
|
|
|
|
- return header;
|
|
|
|
|
- }
|
|
|
|
|
-
|
|
|
|
|
- // 遍历所有文件进行模糊匹配
|
|
|
|
|
- List<FileHeader> headers = zipFile.getFileHeaders();
|
|
|
|
|
- for (FileHeader fileHeader : headers) {
|
|
|
|
|
- String fileName = getFileNameFromPath(fileHeader.getFileName());
|
|
|
|
|
- if (fileName.equals(targetFile)) {
|
|
|
|
|
- return fileHeader;
|
|
|
|
|
|
|
+ try (InputStream is = zipFile.getInputStream(header)) {
|
|
|
|
|
+ return IoUtil.readUtf8(is);
|
|
|
}
|
|
}
|
|
|
}
|
|
}
|
|
|
-
|
|
|
|
|
return null;
|
|
return null;
|
|
|
}
|
|
}
|
|
|
-
|
|
|
|
|
- /**
|
|
|
|
|
- * 从路径中提取文件名
|
|
|
|
|
- */
|
|
|
|
|
- private static String getFileNameFromPath(String path) {
|
|
|
|
|
- if (path == null || path.isEmpty()) {
|
|
|
|
|
- return path;
|
|
|
|
|
- }
|
|
|
|
|
- int lastSeparator = path.lastIndexOf('/');
|
|
|
|
|
- if (lastSeparator == -1) {
|
|
|
|
|
- lastSeparator = path.lastIndexOf('\\');
|
|
|
|
|
- }
|
|
|
|
|
- return lastSeparator == -1 ? path : path.substring(lastSeparator + 1);
|
|
|
|
|
- }
|
|
|
|
|
-
|
|
|
|
|
- /**
|
|
|
|
|
- * 列出ZIP中的所有文件
|
|
|
|
|
- */
|
|
|
|
|
- public static void listZipContents(String zipPath, String password) throws IOException {
|
|
|
|
|
- try (ZipFile zipFile = new ZipFile(zipPath)) {
|
|
|
|
|
- if (zipFile.isEncrypted() && password != null) {
|
|
|
|
|
- zipFile.setPassword(password.toCharArray());
|
|
|
|
|
- }
|
|
|
|
|
-
|
|
|
|
|
- List<FileHeader> fileHeaders = zipFile.getFileHeaders();
|
|
|
|
|
- System.out.println("ZIP contents:");
|
|
|
|
|
- for (FileHeader header : fileHeaders) {
|
|
|
|
|
- System.out.println(" - " + header.getFileName() +
|
|
|
|
|
- " (Encrypted: " + header.isEncrypted() + ")");
|
|
|
|
|
- }
|
|
|
|
|
- }
|
|
|
|
|
- }
|
|
|
|
|
-
|
|
|
|
|
public static void main(String[] args) throws Exception{
|
|
public static void main(String[] args) throws Exception{
|
|
|
- String content = getFileStringFromZip("D:\\panoxv20001.osc_202506161105346420.zip", "data.fdage");
|
|
|
|
|
|
|
+ String content = readUtf8("D:\\panoxv20001.osc_202506161105346420.zip", "data.fdage");
|
|
|
System.out.println(content);
|
|
System.out.println(content);
|
|
|
}
|
|
}
|
|
|
|
|
|