|
|
@@ -3,13 +3,18 @@ package com.izouma.nineth.utils;
|
|
|
import org.apache.commons.lang3.StringUtils;
|
|
|
|
|
|
import java.io.*;
|
|
|
+import java.nio.charset.Charset;
|
|
|
+import java.nio.charset.StandardCharsets;
|
|
|
import java.nio.file.Files;
|
|
|
import java.nio.file.Path;
|
|
|
import java.nio.file.attribute.PosixFileAttributeView;
|
|
|
import java.nio.file.attribute.PosixFileAttributes;
|
|
|
import java.nio.file.attribute.PosixFilePermission;
|
|
|
import java.nio.file.attribute.PosixFilePermissions;
|
|
|
+import java.util.Optional;
|
|
|
import java.util.Set;
|
|
|
+import java.util.zip.ZipEntry;
|
|
|
+import java.util.zip.ZipInputStream;
|
|
|
|
|
|
public class FileUtils {
|
|
|
|
|
|
@@ -200,5 +205,66 @@ public class FileUtils {
|
|
|
|
|
|
}
|
|
|
|
|
|
+ public static void unzip(InputStream in, File destDir) throws IOException {
|
|
|
+ try {
|
|
|
+ unzip(in, destDir, StandardCharsets.UTF_8);
|
|
|
+ } catch (Exception e) {
|
|
|
+ unzip(in, destDir, Charset.forName("GB2312"));
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ public static void unzip(InputStream in, File destDir, Charset charset) throws IOException {
|
|
|
+ byte[] buffer = new byte[1024];
|
|
|
+ ZipInputStream zis = new ZipInputStream(in);
|
|
|
+ ZipEntry zipEntry = zis.getNextEntry();
|
|
|
+ while (zipEntry != null) {
|
|
|
+ File newFile = newFile(destDir, zipEntry);
|
|
|
+ if (zipEntry.isDirectory()) {
|
|
|
+ if (!newFile.isDirectory() && !newFile.mkdirs()) {
|
|
|
+ throw new IOException("Failed to create directory " + newFile);
|
|
|
+ }
|
|
|
+ } else {
|
|
|
+ // fix for Windows-created archives
|
|
|
+ File parent = newFile.getParentFile();
|
|
|
+ if (!parent.isDirectory() && !parent.mkdirs()) {
|
|
|
+ throw new IOException("Failed to create directory " + parent);
|
|
|
+ }
|
|
|
|
|
|
+ // write file content
|
|
|
+ FileOutputStream fos = new FileOutputStream(newFile);
|
|
|
+ int len;
|
|
|
+ while ((len = zis.read(buffer)) > 0) {
|
|
|
+ fos.write(buffer, 0, len);
|
|
|
+ }
|
|
|
+ fos.close();
|
|
|
+ }
|
|
|
+ zipEntry = zis.getNextEntry();
|
|
|
+ }
|
|
|
+ zis.closeEntry();
|
|
|
+ zis.close();
|
|
|
+ }
|
|
|
+
|
|
|
+ public static File newFile(File destinationDir, ZipEntry zipEntry) throws IOException {
|
|
|
+ File destFile = new File(destinationDir, zipEntry.getName());
|
|
|
+
|
|
|
+ String destDirPath = destinationDir.getCanonicalPath();
|
|
|
+ String destFilePath = destFile.getCanonicalPath();
|
|
|
+
|
|
|
+ if (!destFilePath.startsWith(destDirPath + File.separator)) {
|
|
|
+ throw new IOException("Entry is outside of the target dir: " + zipEntry.getName());
|
|
|
+ }
|
|
|
+
|
|
|
+ return destFile;
|
|
|
+ }
|
|
|
+
|
|
|
+ public static File findInDir(File dir, String ext) {
|
|
|
+ if (!(dir.exists() && dir.isDirectory())) return null;
|
|
|
+ for (File file : Optional.ofNullable(dir.listFiles()).orElse(new File[0])) {
|
|
|
+ String name = file.getName().toLowerCase();
|
|
|
+ if (name.endsWith(ext.toLowerCase()) && !file.isHidden()) {
|
|
|
+ return file;
|
|
|
+ }
|
|
|
+ }
|
|
|
+ return null;
|
|
|
+ }
|
|
|
}
|