|
@@ -1,12 +1,18 @@
|
|
package com.hssx.bms.until;
|
|
package com.hssx.bms.until;
|
|
|
|
|
|
|
|
+import net.lingala.zip4j.core.ZipFile;
|
|
|
|
+import net.lingala.zip4j.exception.ZipException;
|
|
|
|
+import net.lingala.zip4j.model.FileHeader;
|
|
|
|
+import net.lingala.zip4j.model.ZipParameters;
|
|
|
|
+import net.lingala.zip4j.util.Zip4jConstants;
|
|
import org.springframework.util.DigestUtils;
|
|
import org.springframework.util.DigestUtils;
|
|
|
|
|
|
import java.io.*;
|
|
import java.io.*;
|
|
import java.text.ParseException;
|
|
import java.text.ParseException;
|
|
-import java.util.zip.ZipEntry;
|
|
|
|
-import java.util.zip.ZipFile;
|
|
|
|
-import java.util.zip.ZipOutputStream;
|
|
|
|
|
|
+import java.util.ArrayList;
|
|
|
|
+import java.util.List;
|
|
|
|
+
|
|
|
|
+import net.lingala.zip4j.*;
|
|
|
|
|
|
/**
|
|
/**
|
|
* Author: 吴涛涛 cuiyi@itany.com
|
|
* Author: 吴涛涛 cuiyi@itany.com
|
|
@@ -20,13 +26,14 @@ public class MD5Util {
|
|
return DigestUtils.md5DigestAsHex(password.getBytes());
|
|
return DigestUtils.md5DigestAsHex(password.getBytes());
|
|
}
|
|
}
|
|
|
|
|
|
- public static void main(String[] args) throws ParseException {
|
|
|
|
- String a = "1.0.1";
|
|
|
|
- String b = "1.0.2";
|
|
|
|
- int i = b.compareTo(a);
|
|
|
|
- int i1 = a.compareTo(b);
|
|
|
|
- System.out.println(i+":"+i1);
|
|
|
|
- System.out.println(MD5Util.getPassword("000000"));
|
|
|
|
|
|
+ public static void main(String[] args) throws ParseException, ZipException {
|
|
|
|
+ unZip4jDemo();
|
|
|
|
+// String a = "1.0.1";
|
|
|
|
+// String b = "1.0.2";
|
|
|
|
+// int i = b.compareTo(a);
|
|
|
|
+// int i1 = a.compareTo(b);
|
|
|
|
+// System.out.println(i+":"+i1);
|
|
|
|
+// System.out.println(MD5Util.getPassword("000000"));
|
|
// zip4jDemo();
|
|
// zip4jDemo();
|
|
|
|
|
|
// DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss");
|
|
// DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss");
|
|
@@ -106,31 +113,96 @@ public class MD5Util {
|
|
// }
|
|
// }
|
|
// }
|
|
// }
|
|
//
|
|
//
|
|
-// public static void zip4jDemo(){
|
|
|
|
|
|
+ public static void zip4jDemo(){
|
|
//// 生成的压缩文件
|
|
//// 生成的压缩文件
|
|
-// ZipFile zipFile = null;
|
|
|
|
-// try {
|
|
|
|
-// zipFile = new ZipFile("D:\\aa.zip");
|
|
|
|
-//
|
|
|
|
-// ZipParameters parameters = new ZipParameters();
|
|
|
|
-// // 压缩方式
|
|
|
|
-// parameters.setCompressionMethod(Zip4jConstants.COMP_DEFLATE);
|
|
|
|
-// // 压缩级别
|
|
|
|
-// parameters.setCompressionLevel(Zip4jConstants.DEFLATE_LEVEL_NORMAL);
|
|
|
|
-// // 要打包的文件夹
|
|
|
|
-// File currentFile = new File("D:\\666");
|
|
|
|
-// File[] fs = currentFile.listFiles();
|
|
|
|
-// // 遍历test文件夹下所有的文件、文件夹
|
|
|
|
-// for (File f : fs) {
|
|
|
|
-// if (f.isDirectory()) {
|
|
|
|
-// zipFile.addFolder(f.getPath(), parameters);
|
|
|
|
-// } else {
|
|
|
|
-// zipFile.addFile(f, parameters);
|
|
|
|
-// }
|
|
|
|
-// }
|
|
|
|
-// zipFile.addFolder("D:\\666", parameters);
|
|
|
|
-// } catch (Exception e) {
|
|
|
|
-// e.printStackTrace();
|
|
|
|
-// }
|
|
|
|
-// }
|
|
|
|
|
|
+ ZipFile zipFile = null;
|
|
|
|
+ try {
|
|
|
|
+ zipFile = new ZipFile("D:\\aa.zip");
|
|
|
|
+
|
|
|
|
+ ZipParameters parameters = new ZipParameters();
|
|
|
|
+ // 压缩方式
|
|
|
|
+ parameters.setCompressionMethod(Zip4jConstants.COMP_DEFLATE);
|
|
|
|
+ // 压缩级别
|
|
|
|
+ parameters.setCompressionLevel(Zip4jConstants.DEFLATE_LEVEL_NORMAL);
|
|
|
|
+ // 要打包的文件夹
|
|
|
|
+ File currentFile = new File("D:\\666");
|
|
|
|
+ File[] fs = currentFile.listFiles();
|
|
|
|
+ // 遍历test文件夹下所有的文件、文件夹
|
|
|
|
+ for (File f : fs) {
|
|
|
|
+ if (f.isDirectory()) {
|
|
|
|
+ zipFile.addFolder(f.getPath(), parameters);
|
|
|
|
+ } else {
|
|
|
|
+ zipFile.addFile(f, parameters);
|
|
|
|
+ }
|
|
|
|
+ }
|
|
|
|
+ zipFile.addFolder("D:\\666", parameters);
|
|
|
|
+ } catch (Exception e) {
|
|
|
|
+ e.printStackTrace();
|
|
|
|
+ }
|
|
|
|
+ }
|
|
|
|
+ //解压demo
|
|
|
|
+ public static void unZip4jDemo() throws ZipException {
|
|
|
|
+ ZipFile zipFile = null;
|
|
|
|
+ zipFile = new ZipFile("D:\\aa.zip");
|
|
|
|
+ File[] unzip = unzip("D:\\aa.zip", "D:\\testaaa");
|
|
|
|
+ for (File file : unzip) {
|
|
|
|
+ System.out.println(file.getName());
|
|
|
|
+ }
|
|
|
|
+
|
|
|
|
+ }
|
|
|
|
+
|
|
|
|
+ public static File [] unzip(String zip, String dest) throws ZipException {
|
|
|
|
+ ZipFile zipFile = new ZipFile(zip);
|
|
|
|
+ return unzip(zipFile.toString(), dest);
|
|
|
|
+ }
|
|
|
|
+
|
|
|
|
+ /**
|
|
|
|
+ * 使用给定密码解压指定的ZIP压缩文件到当前目录
|
|
|
|
+ * @param zip 指定的ZIP压缩文件
|
|
|
|
+ * @param passwd ZIP文件的密码
|
|
|
|
+ * @return 解压后文件数组
|
|
|
|
+ * @throws ZipException 压缩文件有损坏或者解压缩失败抛出
|
|
|
|
+ */
|
|
|
|
+ public static File [] unzip(String zip) throws ZipException {
|
|
|
|
+ File zipFile = new File(zip);
|
|
|
|
+ File parentDir = zipFile.getParentFile();
|
|
|
|
+ return unzip(zipFile.toString(), parentDir.getAbsolutePath());
|
|
|
|
+ }
|
|
|
|
+
|
|
|
|
+ /**
|
|
|
|
+ * 使用给定密码解压指定的ZIP压缩文件到指定目录
|
|
|
|
+ * <p>
|
|
|
|
+ * 如果指定目录不存在,可以自动创建,不合法的路径将导致异常被抛出
|
|
|
|
+ * @param zip 指定的ZIP压缩文件
|
|
|
|
+ * @param dest 解压目录
|
|
|
|
+ * @param passwd ZIP文件的密码
|
|
|
|
+ * @return 解压后文件数组
|
|
|
|
+ * @throws ZipException 压缩文件有损坏或者解压缩失败抛出
|
|
|
|
+ */
|
|
|
|
+ public static File [] unzip(File zipFile, String dest, String passwd) throws ZipException {
|
|
|
|
+ ZipFile zFile = new ZipFile(zipFile);
|
|
|
|
+ zFile.setFileNameCharset("GBK");
|
|
|
|
+ if (!zFile.isValidZipFile()) {
|
|
|
|
+ throw new ZipException("压缩文件不合法,可能被损坏.");
|
|
|
|
+ }
|
|
|
|
+ File destDir = new File(dest);
|
|
|
|
+ if (destDir.isDirectory() && !destDir.exists()) {
|
|
|
|
+ destDir.mkdir();
|
|
|
|
+ }
|
|
|
|
+ if (zFile.isEncrypted()) {
|
|
|
|
+ zFile.setPassword(passwd.toCharArray());
|
|
|
|
+ }
|
|
|
|
+ zFile.extractAll(dest);
|
|
|
|
+
|
|
|
|
+ List<FileHeader> headerList = zFile.getFileHeaders();
|
|
|
|
+ List<File> extractedFileList = new ArrayList<File>();
|
|
|
|
+ for(FileHeader fileHeader : headerList) {
|
|
|
|
+ if (!fileHeader.isDirectory()) {
|
|
|
|
+ extractedFileList.add(new File(destDir,fileHeader.getFileName()));
|
|
|
|
+ }
|
|
|
|
+ }
|
|
|
|
+ File [] extractedFiles = new File[extractedFileList.size()];
|
|
|
|
+ extractedFileList.toArray(extractedFiles);
|
|
|
|
+ return extractedFiles;
|
|
|
|
+ }
|
|
}
|
|
}
|