From 82e3986045ac7eaca6aaa290fb2283fd6c6c901a Mon Sep 17 00:00:00 2001 From: Keuin Date: Tue, 29 Dec 2020 10:06:01 +0800 Subject: Support specifying zip compression level (not finished) --- .../kbackupfabric/operation/backup/PrimitiveBackupMethod.java | 3 ++- src/main/java/com/keuin/kbackupfabric/util/ZipUtil.java | 9 ++++++++- 2 files changed, 10 insertions(+), 2 deletions(-) (limited to 'src/main/java/com') diff --git a/src/main/java/com/keuin/kbackupfabric/operation/backup/PrimitiveBackupMethod.java b/src/main/java/com/keuin/kbackupfabric/operation/backup/PrimitiveBackupMethod.java index 854355d..72e9cdb 100644 --- a/src/main/java/com/keuin/kbackupfabric/operation/backup/PrimitiveBackupMethod.java +++ b/src/main/java/com/keuin/kbackupfabric/operation/backup/PrimitiveBackupMethod.java @@ -18,6 +18,7 @@ import static org.apache.commons.io.FileUtils.forceDelete; public class PrimitiveBackupMethod implements BackupMethod { private static final PrimitiveBackupMethod INSTANCE = new PrimitiveBackupMethod(); + private static int zipLevel = 9; public static PrimitiveBackupMethod getInstance() { return INSTANCE; @@ -31,7 +32,7 @@ public class PrimitiveBackupMethod implements BackupMethod { PrintUtil.info(String.format("zip(srcPath=%s, destPath=%s)", levelPath, backupSaveDirectory)); PrintUtil.info("Compressing level ..."); - ZipUtil.makeBackupZip(levelPath, backupSaveDirectory, backupFileName, backupMetadata); + ZipUtil.makeBackupZip(levelPath, backupSaveDirectory, backupFileName, backupMetadata, zipLevel); } catch (ZipUtilException exception) { PrintUtil.info("Infinite recursive of directory tree detected, backup was aborted."); diff --git a/src/main/java/com/keuin/kbackupfabric/util/ZipUtil.java b/src/main/java/com/keuin/kbackupfabric/util/ZipUtil.java index c670cf1..285fe83 100644 --- a/src/main/java/com/keuin/kbackupfabric/util/ZipUtil.java +++ b/src/main/java/com/keuin/kbackupfabric/util/ZipUtil.java @@ -7,6 +7,8 @@ import java.io.*; import java.util.Enumeration; import java.util.zip.*; +import static java.util.zip.Deflater.DEFAULT_COMPRESSION; + public final class ZipUtil { /** @@ -70,7 +72,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."); } @@ -107,6 +109,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); @@ -140,6 +143,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包 * -- cgit v1.2.3