From 7ba13d11a9ef626506c129b420b560d87cbefcb4 Mon Sep 17 00:00:00 2001 From: WenG <450292408@qq.com> Date: Tue, 23 Mar 2021 23:44:47 +0800 Subject: [PATCH] new utils --- .../java/ink/wgink/util/ArrayListUtil.java | 35 ++ .../java/ink/wgink/util/SortCodeUtil.java | 325 ++++++++++++++++++ .../src/main/java/ink/wgink/util/ZipUtil.java | 126 +++++++ 3 files changed, 486 insertions(+) create mode 100644 basic-util/src/main/java/ink/wgink/util/ArrayListUtil.java create mode 100644 basic-util/src/main/java/ink/wgink/util/SortCodeUtil.java create mode 100644 basic-util/src/main/java/ink/wgink/util/ZipUtil.java diff --git a/basic-util/src/main/java/ink/wgink/util/ArrayListUtil.java b/basic-util/src/main/java/ink/wgink/util/ArrayListUtil.java new file mode 100644 index 00000000..693af011 --- /dev/null +++ b/basic-util/src/main/java/ink/wgink/util/ArrayListUtil.java @@ -0,0 +1,35 @@ +package ink.wgink.util; + +import java.io.*; +import java.util.List; + +/** + * When you feel like quitting. Think about why you started + * 当你想要放弃的时候,想想当初你为何开始 + * + * @ClassName: ArrayListUtil + * @Description: 列表工具 + * @Author: WangGeng + * @Date: 2020/8/24 17:25 + * @Version: 1.0 + **/ +public class ArrayListUtil { + + public static List deepClone(List sourceList, Class clazz) { + ByteArrayOutputStream byteOut = new ByteArrayOutputStream(); + try { + ObjectOutputStream out = new ObjectOutputStream(byteOut); + out.writeObject(sourceList); + ByteArrayInputStream byteIn = new ByteArrayInputStream(byteOut.toByteArray()); + ObjectInputStream inStream = new ObjectInputStream(byteIn); + List destList = (List) inStream.readObject(); + return destList; + } catch (IOException e) { + e.printStackTrace(); + } catch (ClassNotFoundException e) { + e.printStackTrace(); + } + return null; + } + +} diff --git a/basic-util/src/main/java/ink/wgink/util/SortCodeUtil.java b/basic-util/src/main/java/ink/wgink/util/SortCodeUtil.java new file mode 100644 index 00000000..d0bd519f --- /dev/null +++ b/basic-util/src/main/java/ink/wgink/util/SortCodeUtil.java @@ -0,0 +1,325 @@ +package ink.wgink.util; + +/** + * When you feel like quitting. Think about why you started + * 当你想要放弃的时候,想想当初你为何开始 + * + * @ClassName: SortCodeUtil + * @Description: 排序编码工具 + * @Author: WangGeng + * @Date: 2020/1/14 10:21 下午 + * @Version: 1.0 + **/ +public class SortCodeUtil { + + /** + * 获取排序Code + * + * @param lastCode 上一个编码,获取第一个值为null即可 + * @param separator 分隔符 默认"-" + * @param sectionLength 片段长度 默认长度3 + * @return + */ + public static String getNextSortCode(String lastCode, String separator, int sectionLength) { + separator = (separator == null || separator.isEmpty()) ? "-" : separator; + sectionLength = sectionLength <= 0 ? 3 : sectionLength; + String carryFlag = String.format("%0" + sectionLength + "d", 0); + if (lastCode == null || lastCode.isEmpty()) { + return carryFlag; + } + // 获取分段数组 + String[] sectionArray = lastCode.split(separator); + boolean hasCarry = true; + String resultSortCode = ""; + for (int i = sectionArray.length - 1; i >= 0; i--) { + String section = sectionArray[i]; + if (hasCarry) { + String nextCode = nextCode(section, sectionLength); + if (resultSortCode.isEmpty()) { + resultSortCode = nextCode; + } else { + resultSortCode = nextCode + separator + resultSortCode; + } + if (carryFlag.equals(nextCode)) { + hasCarry = true; + continue; + } + hasCarry = false; + } else { + resultSortCode = section + separator + resultSortCode; + } + } + return resultSortCode; + } + + /** + * 下一个编码,编码大小为 36^sectionLength,最小 0*sectionLength + * + * @param code 编码 + * @param sectionLength 长度 + * @return + */ + public static String nextCode(String code, int sectionLength) { + if (code == null || code.isEmpty()) { + return String.format("%0" + sectionLength + "d", 0); + } + code = code.toUpperCase(); + char[] charArray = code.toCharArray(); + boolean hasCarry = true; + String resultCode = ""; + for (int i = charArray.length - 1; i >= 0; i--) { + char currentChar = charArray[i]; + if (hasCarry) { + char nextChar = nextChar(currentChar); + resultCode = nextChar + resultCode; + if ('0' == nextChar) { + hasCarry = true; + continue; + } + hasCarry = false; + } else { + // 没有发生进位直接相加 + resultCode = currentChar + resultCode; + } + } + return resultCode; + } + + /** + * 下一个字符 + * + * @param currentChar + * @return + */ + private static char nextChar(char currentChar) { + char nextChar = '0'; + if ('0' == currentChar) { + nextChar = '1'; + } else if ('1' == currentChar) { + nextChar = '2'; + } else if ('2' == currentChar) { + nextChar = '3'; + } else if ('3' == currentChar) { + nextChar = '4'; + } else if ('4' == currentChar) { + nextChar = '5'; + } else if ('5' == currentChar) { + nextChar = '6'; + } else if ('6' == currentChar) { + nextChar = '7'; + } else if ('7' == currentChar) { + nextChar = '8'; + } else if ('8' == currentChar) { + nextChar = '9'; + } else if ('9' == currentChar) { + nextChar = 'A'; + } else if ('A' == currentChar) { + nextChar = 'B'; + } else if ('B' == currentChar) { + nextChar = 'C'; + } else if ('C' == currentChar) { + nextChar = 'D'; + } else if ('D' == currentChar) { + nextChar = 'E'; + } else if ('E' == currentChar) { + nextChar = 'F'; + } else if ('F' == currentChar) { + nextChar = 'G'; + } else if ('G' == currentChar) { + nextChar = 'H'; + } else if ('H' == currentChar) { + nextChar = 'I'; + } else if ('I' == currentChar) { + nextChar = 'J'; + } else if ('J' == currentChar) { + nextChar = 'K'; + } else if ('K' == currentChar) { + nextChar = 'L'; + } else if ('L' == currentChar) { + nextChar = 'M'; + } else if ('M' == currentChar) { + nextChar = 'N'; + } else if ('N' == currentChar) { + nextChar = 'O'; + } else if ('O' == currentChar) { + nextChar = 'P'; + } else if ('P' == currentChar) { + nextChar = 'Q'; + } else if ('Q' == currentChar) { + nextChar = 'R'; + } else if ('R' == currentChar) { + nextChar = 'S'; + } else if ('S' == currentChar) { + nextChar = 'T'; + } else if ('T' == currentChar) { + nextChar = 'U'; + } else if ('U' == currentChar) { + nextChar = 'V'; + } else if ('V' == currentChar) { + nextChar = 'W'; + } else if ('W' == currentChar) { + nextChar = 'X'; + } else if ('X' == currentChar) { + nextChar = 'Y'; + } else if ('Y' == currentChar) { + nextChar = 'Z'; + } + return nextChar; + } + + /** + * 获取排序Code + * + * @param nextCode 下一个编码,获取最后一个值为null即可 + * @param separator 分隔符 默认"-" + * @param sectionLength 片段长度 默认长度3 + * @return + */ + public static String getPrevSortCode(String nextCode, String separator, int sectionLength) { + separator = ((separator == null) || separator.isEmpty()) ? "-" : separator; + sectionLength = sectionLength <= 0 ? 3 : sectionLength; + String borrowFlag = String.format("%" + sectionLength + "s", "Z").replaceAll("\\s", "Z"); + if (nextCode == null || nextCode.isEmpty()) { + return borrowFlag; + } + // 获取分段数组 + String[] sectionArray = nextCode.split(separator); + boolean hasBorrow = true; + String resultSortCode = ""; + for (int i = sectionArray.length - 1; i >= 0; i--) { + String section = sectionArray[i]; + if (hasBorrow) { + String prevCode = prevCode(section, sectionLength); + if (resultSortCode.isEmpty()) { + resultSortCode = prevCode; + } else { + resultSortCode = prevCode + separator + resultSortCode; + } + if (borrowFlag.equals(prevCode)) { + hasBorrow = true; + continue; + } + hasBorrow = false; + } else { + resultSortCode = section + separator + resultSortCode; + } + } + return resultSortCode; + } + + /** + * 上一个编码,编码大小为 36^sectionLength,最大 Z*sectionLength + * + * @param code 编码 + * @param sectionLength 长度 + * @return + */ + public static String prevCode(String code, int sectionLength) { + if (code == null || code.isEmpty()) { + return String.format("%" + sectionLength + "s", "Z").replaceAll("\\s", "Z"); + } + code = code.toUpperCase(); + char[] charArray = code.toCharArray(); + boolean hasBorrow = true; + String resultCode = ""; + for (int i = charArray.length - 1; i >= 0; i--) { + char currentChar = charArray[i]; + if (hasBorrow) { + char prevChar = prevChar(currentChar); + resultCode = prevChar + resultCode; + if ('Z' == prevChar) { + hasBorrow = true; + continue; + } + hasBorrow = false; + } else { + // 没有发生借位直接 + resultCode = currentChar + resultCode; + } + } + return resultCode; + } + + /** + * 上一个字符 + * + * @param currentChar + * @return + */ + private static char prevChar(char currentChar) { + char prevChar = 'Z'; + if ('1' == currentChar) { + prevChar = '0'; + } else if ('2' == currentChar) { + prevChar = '1'; + } else if ('3' == currentChar) { + prevChar = '2'; + } else if ('4' == currentChar) { + prevChar = '3'; + } else if ('5' == currentChar) { + prevChar = '4'; + } else if ('6' == currentChar) { + prevChar = '5'; + } else if ('7' == currentChar) { + prevChar = '6'; + } else if ('8' == currentChar) { + prevChar = '7'; + } else if ('9' == currentChar) { + prevChar = '8'; + } else if ('A' == currentChar) { + prevChar = '9'; + } else if ('B' == currentChar) { + prevChar = 'A'; + } else if ('C' == currentChar) { + prevChar = 'B'; + } else if ('D' == currentChar) { + prevChar = 'C'; + } else if ('E' == currentChar) { + prevChar = 'D'; + } else if ('F' == currentChar) { + prevChar = 'E'; + } else if ('G' == currentChar) { + prevChar = 'F'; + } else if ('H' == currentChar) { + prevChar = 'G'; + } else if ('I' == currentChar) { + prevChar = 'H'; + } else if ('J' == currentChar) { + prevChar = 'I'; + } else if ('K' == currentChar) { + prevChar = 'J'; + } else if ('L' == currentChar) { + prevChar = 'K'; + } else if ('M' == currentChar) { + prevChar = 'L'; + } else if ('N' == currentChar) { + prevChar = 'M'; + } else if ('O' == currentChar) { + prevChar = 'N'; + } else if ('P' == currentChar) { + prevChar = 'O'; + } else if ('Q' == currentChar) { + prevChar = 'P'; + } else if ('R' == currentChar) { + prevChar = 'Q'; + } else if ('S' == currentChar) { + prevChar = 'R'; + } else if ('T' == currentChar) { + prevChar = 'S'; + } else if ('U' == currentChar) { + prevChar = 'T'; + } else if ('V' == currentChar) { + prevChar = 'U'; + } else if ('W' == currentChar) { + prevChar = 'V'; + } else if ('X' == currentChar) { + prevChar = 'W'; + } else if ('Y' == currentChar) { + prevChar = 'X'; + } else if ('Z' == currentChar) { + prevChar = 'Y'; + } + return prevChar; + } + +} diff --git a/basic-util/src/main/java/ink/wgink/util/ZipUtil.java b/basic-util/src/main/java/ink/wgink/util/ZipUtil.java new file mode 100644 index 00000000..f35d51c0 --- /dev/null +++ b/basic-util/src/main/java/ink/wgink/util/ZipUtil.java @@ -0,0 +1,126 @@ +package ink.wgink.util; + +import org.apache.commons.compress.archivers.zip.ZipArchiveEntry; +import org.apache.commons.compress.archivers.zip.ZipArchiveInputStream; +import org.apache.commons.io.FileUtils; +import org.apache.commons.io.IOUtils; + +import java.io.*; +import java.util.ArrayList; +import java.util.List; +import java.util.zip.ZipEntry; +import java.util.zip.ZipOutputStream; + +/** + * @ClassName: ZipUtil + * @Description: 压缩 + * @Author: WangGeng + * @Date: 2019/4/28 11:24 AM + * @Version: 1.0 + **/ +public class ZipUtil { + + /** + * 压缩文件 + * + * @param inputFileName + * @param zipFileName + * @return + * @throws Exception + */ + public static Boolean zip(String inputFileName, String zipFileName) throws Exception { + zip(zipFileName, new File(inputFileName)); + return true; + } + + private static void zip(String zipFileName, File inputFile) throws Exception { + ZipOutputStream out = new ZipOutputStream(new FileOutputStream(zipFileName)); + zip(out, inputFile, ""); + out.flush(); + out.close(); + } + + private static void zip(ZipOutputStream out, File f, String base) throws Exception { + if (f.isDirectory()) { + File[] fl = f.listFiles(); + out.putNextEntry(new ZipEntry(base + "/")); + base = base.length() == 0 ? "" : base + "/"; + for (int i = 0; i < fl.length; i++) { + zip(out, fl[i], base + fl[i].getName()); + } + } else { + out.putNextEntry(new ZipEntry(base)); + FileInputStream in = new FileInputStream(f); + int b; + while ((b = in.read()) != -1) { + out.write(b); + } + in.close(); + } + } + + /** + * 解压 zip 文件 + * + * @param zipFile zip 压缩文件 + * @param destDir zip 压缩文件解压后保存的目录 + * @param encoding zip 文件的编码 + * @return 返回 zip 压缩文件里的文件名的 list + * @throws IOException + */ + public static List unZip(File zipFile, String destDir, String encoding) throws IOException { + // 如果 destDir 为 null, 空字符串, 或者全是空格, 则解压到压缩文件所在目录 + if (destDir == null || destDir.length() == 0) { + destDir = zipFile.getParent(); + } + destDir = destDir.endsWith(File.separator) ? destDir : destDir + File.separator; + ZipArchiveInputStream is = null; + List fileNames = new ArrayList(); + try { + is = new ZipArchiveInputStream(new BufferedInputStream(new FileInputStream(zipFile), 1024), encoding); + ZipArchiveEntry entry = null; + while ((entry = is.getNextZipEntry()) != null) { + fileNames.add(entry.getName()); + File file = new File(destDir, entry.getName()); + if (entry.isDirectory()) { + // 创建文件夹,如果中间有路径会自动创建 + FileUtils.forceMkdir(file); + } else { + OutputStream os = null; + try { + FileUtils.touch(file); + os = new FileOutputStream(new File(destDir, entry.getName())); + IOUtils.copy(is, os); + } finally { + IOUtils.closeQuietly(os); + } + } + } + } catch (IOException e) { + e.printStackTrace(); + throw e; + } finally { + IOUtils.closeQuietly(is); + } + return fileNames; + } + + /** + * 解压 zip 文件 + * + * @param zipFile zip 压缩文件的路径 + * @param destDir zip 压缩文件解压后保存的目录 + * @param encoding zip 文件的编码 + * @return 返回 zip 压缩文件里的文件名的 list + * @throws Exception + */ + public static List unZip(String zipFile, String destDir, String encoding) throws IOException { + File zipfile = new File(zipFile); + return unZip(zipfile, destDir, encoding); + } + + public static List unZip(String zipFile, String destDir) throws IOException { + return unZip(zipFile, destDir, "UTF-8"); + } + +}