diff options
author | keuin <[email protected]> | 2021-01-21 11:04:25 +0800 |
---|---|---|
committer | keuin <[email protected]> | 2021-01-21 11:04:25 +0800 |
commit | 64345efb5f4397993fbc9f2a2102ebcd3fe9f125 (patch) | |
tree | 2e2df193db961774807223e37e7cf371d30ebd39 | |
parent | ac7e70883c18602e7fd4b525b9e6fb9ea9620a6b (diff) | |
parent | 64eb2e27e37510a625df0709b69a741d6f821306 (diff) |
Merge remote-tracking branch 'origin/master'
-rw-r--r-- | gradle.properties | 2 | ||||
-rw-r--r-- | src/main/java/com/keuin/kbackupfabric/util/ZipUtil.java | 45 |
2 files changed, 27 insertions, 20 deletions
diff --git a/gradle.properties b/gradle.properties index ef7c112..7878280 100644 --- a/gradle.properties +++ b/gradle.properties @@ -6,7 +6,7 @@ minecraft_version=1.14.4 yarn_mappings=1.14.4+build.18 loader_version=0.11.0 # Mod Properties -mod_version=1.4.2 +mod_version=1.4.3 maven_group=com.keuin.kbackupfabric archives_base_name=kbackup-fabric # Dependencies diff --git a/src/main/java/com/keuin/kbackupfabric/util/ZipUtil.java b/src/main/java/com/keuin/kbackupfabric/util/ZipUtil.java index 0fa9d77..62482c5 100644 --- a/src/main/java/com/keuin/kbackupfabric/util/ZipUtil.java +++ b/src/main/java/com/keuin/kbackupfabric/util/ZipUtil.java @@ -10,8 +10,12 @@ import java.util.Optional; import java.util.Set; import java.util.zip.*; +import static java.util.zip.Deflater.DEFAULT_COMPRESSION; + public final class ZipUtil { + private static final int bufferSize = 1024 * 1024 * 8; // 1MB + /** * 递归压缩文件夹 * @@ -21,7 +25,7 @@ public final class ZipUtil { * @param filesSkipping 被忽略的文件 * @throws IOException IO Error */ - private static void zip(String srcRootDir, File file, ZipOutputStream zipOutputStream, Set<String> filesSkipping) throws IOException { + private static void zip(String srcRootDir, File file, ZipOutputStream zipOutputStream, Set<String> filesSkipping, final byte[] buffer) throws IOException { if (file == null) { return; } @@ -32,8 +36,7 @@ public final class ZipUtil { // 如果是文件,则直接压缩该文件 boolean skipping = Optional.ofNullable(filesSkipping).orElse(Collections.emptySet()).contains(file.getName()); if (file.isFile() && !skipping) { - int count, bufferLen = 1024; - byte[] data = new byte[bufferLen]; + int count; // 获取文件相对于压缩文件夹根目录的子路径 String subPath = file.getAbsolutePath(); @@ -45,9 +48,10 @@ public final class ZipUtil { // 写入压缩包 ZipEntry entry = new ZipEntry(subPath); zipOutputStream.putNextEntry(entry); - BufferedInputStream inputStream = new BufferedInputStream(new FileInputStream(file)); - while ((count = inputStream.read(data, 0, bufferLen)) != -1) { - zipOutputStream.write(data, 0, count); +// BufferedInputStream inputStream = new BufferedInputStream(new FileInputStream(file)); + InputStream inputStream = new FileInputStream(file); + while ((count = inputStream.read(buffer, 0, bufferSize)) != -1) { + zipOutputStream.write(buffer, 0, count); } inputStream.close(); zipOutputStream.closeEntry(); @@ -57,7 +61,7 @@ public final class ZipUtil { File[] childFileList = file.listFiles(); if (childFileList != null) { for (File value : childFileList) - zip(srcRootDir, value, zipOutputStream, filesSkipping); + zip(srcRootDir, value, zipOutputStream, filesSkipping, buffer); } } } @@ -76,7 +80,7 @@ public final class ZipUtil { * @throws IOException IO Error * @throws ZipUtilException General exception, such as loop recursion. */ - public static void makeBackupZip(String srcPath, String zipPath, String zipFileName, BackupMetadata backupMetadata) throws IOException, ZipUtilException { + public static void makeBackupZip(String srcPath, String zipPath, String zipFileName, BackupMetadata backupMetadata, int zipLevel) throws IOException, ZipUtilException { if (srcPath == null || zipPath == null || zipFileName == null || backupMetadata == null || srcPath.isEmpty() || zipPath.isEmpty() || zipFileName.isEmpty()) { throw new IllegalArgumentException("Parameter for zip() contains null."); } @@ -113,6 +117,7 @@ public final class ZipUtil { checkedOutputStream = new CheckedOutputStream(new FileOutputStream(zipFile), new CRC32()); zipOutputStream = new ZipOutputStream(checkedOutputStream); + zipOutputStream.setLevel(zipLevel); // If with backup metadata, we serialize it and write it into file "kbackup_metadata" ZipEntry metadataEntry = new ZipEntry(BackupMetadata.metadataFileName); @@ -133,7 +138,7 @@ public final class ZipUtil { } } //调用递归压缩方法进行目录或文件压缩 - zip(srcRootDir, srcFile, zipOutputStream, Collections.singleton("session.lock")); + zip(srcRootDir, srcFile, zipOutputStream, Collections.singleton("session.lock"), new byte[bufferSize]); zipOutputStream.flush(); } finally { try { @@ -146,6 +151,10 @@ public final class ZipUtil { } } + public static void makeBackupZip(String srcPath, String zipPath, String zipFileName, BackupMetadata backupMetadata) throws IOException, ZipUtilException { + makeBackupZip(srcPath, zipPath, zipFileName, backupMetadata, DEFAULT_COMPRESSION); + } + /** * 解压缩zip包 * @@ -154,6 +163,7 @@ public final class ZipUtil { * @param includeZipFileName 解压后的文件保存的路径是否包含压缩文件的文件名。true-包含;false-不包含 */ public static void unzip(String zipFilePath, String unzipFilePath, boolean includeZipFileName) throws IOException { + final byte[] buffer = new byte[bufferSize]; if (zipFilePath.isEmpty() || unzipFilePath.isEmpty()) { throw new IllegalArgumentException("Parameter for unzip() contains null."); } @@ -177,10 +187,7 @@ public final class ZipUtil { ZipEntry entry; String entryFilePath, entryDirPath; File entryFile, entryDir; - int index, count, bufferSize = 1024; - byte[] buffer = new byte[bufferSize]; - BufferedInputStream bis; - BufferedOutputStream bos; + int index, count; ZipFile zip = new ZipFile(zipFile); Enumeration<? extends ZipEntry> entries = zip.entries(); // 循环对压缩包里的每一个文件进行解压 @@ -220,13 +227,13 @@ public final class ZipUtil { } else { // Is a file, we write the data // 写入文件 - bos = new BufferedOutputStream(new FileOutputStream(entryFile)); - bis = new BufferedInputStream(zip.getInputStream(entry)); - while ((count = bis.read(buffer, 0, bufferSize)) != -1) { - bos.write(buffer, 0, count); + OutputStream outputStream = new FileOutputStream(entryFile); + InputStream inputStream = zip.getInputStream(entry); + while ((count = inputStream.read(buffer, 0, bufferSize)) != -1) { + outputStream.write(buffer, 0, count); } - bos.flush(); - bos.close(); + outputStream.flush(); + outputStream.close(); } |