From ad7cd2f0b1915d4a2f8080f26edc699bf13be8d0 Mon Sep 17 00:00:00 2001 From: Matthew Date: Sat, 27 Jul 2024 11:47:43 +0800 Subject: [PATCH] =?UTF-8?q?=E4=BF=AE=E5=A4=8D=E5=8E=8B=E7=BC=A9=E5=A4=9A?= =?UTF-8?q?=E4=B8=AA=E6=96=87=E4=BB=B6=E7=9A=84=E8=B7=AF=E5=BE=84=E9=94=99?= =?UTF-8?q?=E8=AF=AF?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../java/com/xypower/common/ZipUtils.java | 25 ++++++++++++------- 1 file changed, 16 insertions(+), 9 deletions(-) diff --git a/common/src/main/java/com/xypower/common/ZipUtils.java b/common/src/main/java/com/xypower/common/ZipUtils.java index 8b89c461..c4038436 100644 --- a/common/src/main/java/com/xypower/common/ZipUtils.java +++ b/common/src/main/java/com/xypower/common/ZipUtils.java @@ -16,9 +16,12 @@ public class ZipUtils { public static void ZipFolder(File srcDirectory, File zipFile, FilenameFilter filter) { ZipOutputStream outZip = null; FileInputStream inputStream = null; + FileOutputStream fileOutputStream = null; try { - outZip = new ZipOutputStream(new FileOutputStream(zipFile)); + fileOutputStream = new FileOutputStream(zipFile); + outZip = new ZipOutputStream(fileOutputStream); + int len; byte[] buffer = new byte[1024 * 256]; ZipEntry zipEntry = null; @@ -48,6 +51,7 @@ public class ZipUtils { } catch (Exception ex) { ex.printStackTrace(); } + FilesUtils.closeFriendly(fileOutputStream); FilesUtils.closeFriendly(outZip); } } @@ -55,9 +59,11 @@ public class ZipUtils { public static void ZipFolders(Map srcDirectories, File zipFile, FilenameFilter filter) { ZipOutputStream outZip = null; FileInputStream inputStream = null; + FileOutputStream fileOutputStream = null; try { - outZip = new ZipOutputStream(new FileOutputStream(zipFile)); + fileOutputStream = new FileOutputStream(zipFile); + outZip = new ZipOutputStream(fileOutputStream); int len; byte[] buffer = new byte[1024 * 256]; ZipEntry zipEntry = null; @@ -91,6 +97,7 @@ public class ZipUtils { } catch (Exception ex) { ex.printStackTrace(); } + FilesUtils.closeFriendly(fileOutputStream); FilesUtils.closeFriendly(outZip); } } @@ -99,10 +106,10 @@ public class ZipUtils { ZipOutputStream outZip = null; FileInputStream inputStream = null; - + FileOutputStream fileOutputStream = null; try { - outZip = new ZipOutputStream(new FileOutputStream(zipFile)); - // ZipFiles(srcFile.getParent() + File.separator, srcFile.getName(), outZip); + fileOutputStream = new FileOutputStream(zipFile); + outZip = new ZipOutputStream(fileOutputStream); int len = 0; byte[] buffer = new byte[1024 * 256]; @@ -111,16 +118,15 @@ public class ZipUtils { if (!file.exists()) { continue; } - ZipEntry zipEntry = new ZipEntry(path); + ZipEntry zipEntry = new ZipEntry(srcFiles.size() > 1 ? path.substring(1) : file.getName()); inputStream = new FileInputStream(file); - outZip.putNextEntry(zipEntry); + outZip.putNextEntry(zipEntry); while ((len = inputStream.read(buffer)) != -1) { outZip.write(buffer, 0, len); } - FilesUtils.closeFriendly(inputStream); - outZip.closeEntry(); + FilesUtils.closeFriendly(inputStream); } } catch (Exception e) { @@ -134,6 +140,7 @@ public class ZipUtils { ex.printStackTrace(); } } + FilesUtils.closeFriendly(fileOutputStream); FilesUtils.closeFriendly(outZip); } }