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/keuin') 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 From e1420aea791cc94a9daf72f1cb7723952ac62ec3 Mon Sep 17 00:00:00 2001 From: Keuin Date: Thu, 21 Jan 2021 02:16:43 +0800 Subject: Performance optimization on file I/O streams. --- .../java/com/keuin/kbackupfabric/util/ZipUtil.java | 36 +++++++++++----------- 1 file changed, 18 insertions(+), 18 deletions(-) (limited to 'src/main/java/com/keuin') diff --git a/src/main/java/com/keuin/kbackupfabric/util/ZipUtil.java b/src/main/java/com/keuin/kbackupfabric/util/ZipUtil.java index 9b7cb83..62482c5 100644 --- a/src/main/java/com/keuin/kbackupfabric/util/ZipUtil.java +++ b/src/main/java/com/keuin/kbackupfabric/util/ZipUtil.java @@ -14,6 +14,8 @@ import static java.util.zip.Deflater.DEFAULT_COMPRESSION; public final class ZipUtil { + private static final int bufferSize = 1024 * 1024 * 8; // 1MB + /** * 递归压缩文件夹 * @@ -23,7 +25,7 @@ public final class ZipUtil { * @param filesSkipping 被忽略的文件 * @throws IOException IO Error */ - private static void zip(String srcRootDir, File file, ZipOutputStream zipOutputStream, Set filesSkipping) throws IOException { + private static void zip(String srcRootDir, File file, ZipOutputStream zipOutputStream, Set filesSkipping, final byte[] buffer) throws IOException { if (file == null) { return; } @@ -34,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(); @@ -47,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(); @@ -59,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); } } } @@ -136,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 { @@ -161,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."); } @@ -184,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 entries = zip.entries(); // 循环对压缩包里的每一个文件进行解压 @@ -227,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(); } -- cgit v1.2.3