Browse Source

修复reWriteZip后文件占用空间变大的问题

JustryDeng 4 years ago
parent
commit
197c5cdfe6

+ 0 - 37
class-winter-core/dependency-reduced-pom.xml

@@ -1,37 +0,0 @@
-<?xml version="1.0" encoding="UTF-8"?>
-<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd">
-  <parent>
-    <artifactId>class-winter</artifactId>
-    <groupId>com.idea-aedi</groupId>
-    <version>2.1.0</version>
-  </parent>
-  <modelVersion>4.0.0</modelVersion>
-  <artifactId>class-winter-core</artifactId>
-  <build>
-    <plugins>
-      <plugin>
-        <artifactId>maven-shade-plugin</artifactId>
-        <version>3.0.0</version>
-        <executions>
-          <execution>
-            <phase>package</phase>
-            <goals>
-              <goal>shade</goal>
-            </goals>
-            <configuration>
-              <transformers>
-                <transformer>
-                  <manifestEntries>
-                    <Main-Class>winter.com.ideaaedi.classwinter.Forward</Main-Class>
-                    <Premain-Class>winter.com.ideaaedi.classwinter.Reverses</Premain-Class>
-                  </manifestEntries>
-                </transformer>
-              </transformers>
-            </configuration>
-          </execution>
-        </executions>
-      </plugin>
-    </plugins>
-  </build>
-</project>
-

+ 12 - 5
class-winter-core/src/main/java/winter/com/ideaaedi/classwinter/util/JarUtil.java

@@ -278,16 +278,23 @@ public final class JarUtil {
             ZipEntry zipEntry = entries.nextElement();
             zipEntryList.add(zipEntry);
         }
+        
+        Map<String, byte[]> originBytesMap = new HashMap<>(64);
+        for (ZipEntry zipEntry : zipEntryList) {
+            String zipEntryName = zipEntry.getName();
+            byte[] originBytes = IOUtil.toBytes(zipFile.getInputStream(zipEntry));
+            originBytesMap.put(zipEntryName, originBytes);
+        }
         // zipFile.getName()形如:   /abc/my-project.jar   /abc/my-project.war  /abc/my-projectzip
-        try (FileOutputStream fos = new FileOutputStream(zipFilePath, true);
+        try (FileOutputStream fos = new FileOutputStream(zipFilePath, false);
              ZipOutputStream zos = new ZipOutputStream(fos)) {
             for (ZipEntry zipEntry : zipEntryList) {
-                final String zipEntryName = zipEntry.getName();
-                final byte[] originBytes = IOUtil.toBytes(zipFile.getInputStream(zipEntry));
-                final byte[] replaceBytes = replacerMap.get(zipEntryName);
+                String zipEntryName = zipEntry.getName();
+                byte[] originBytes = originBytesMap.get(zipEntryName);
+                byte[] replaceBytes = replacerMap.get(zipEntryName);
                 if (replacerMap.containsKey(zipEntryName) && replaceBytes != null) {
                     // 覆盖
-                    final ZipEntry ze = new ZipEntry(zipEntryName);
+                    ZipEntry ze = new ZipEntry(zipEntryName);
                     ze.setMethod(ZipEntry.STORED);
                     ze.setSize(replaceBytes.length);
                     ze.setCrc(IOUtil.computeCrc32(replaceBytes));