You are viewing a plain text version of this content. The canonical link for it is here.
Posted to dev@tomcat.apache.org by li...@apache.org on 2022/08/26 09:24:50 UTC

[tomcat-jakartaee-migration] branch main updated: Recalculate the CRC value after converting to avoid CRC checksum failures when the entry type is STORED

This is an automated email from the ASF dual-hosted git repository.

lihan pushed a commit to branch main
in repository https://gitbox.apache.org/repos/asf/tomcat-jakartaee-migration.git


The following commit(s) were added to refs/heads/main by this push:
     new d39df40  Recalculate the CRC value after converting to avoid CRC checksum failures when the entry type is STORED
d39df40 is described below

commit d39df404eb7285c8482759626c5fc2e751a08000
Author: lihan <li...@apache.org>
AuthorDate: Fri Aug 26 17:23:59 2022 +0800

    Recalculate the CRC value after converting to avoid CRC checksum failures when the entry type is STORED
---
 CHANGES.md                                         |  1 +
 .../org/apache/tomcat/jakartaee/Migration.java     | 75 ++++++++++++++++++++--
 2 files changed, 69 insertions(+), 7 deletions(-)

diff --git a/CHANGES.md b/CHANGES.md
index 1db82aa..51640b7 100644
--- a/CHANGES.md
+++ b/CHANGES.md
@@ -1,6 +1,7 @@
 # Tomcat Migration Tool for Jakarta EE - Changelog
 
 ## 1.0.2 (in progress)
+- Fix [#29](https://github.com/apache/tomcat-jakartaee-migration/issues/29) by recalculating the CRC value of the entry type is SORTED after converting (lihan)
 
 ## 1.0.1
 - Fix [#19](https://github.com/apache/tomcat-jakartaee-migration/issues/19). Add support for converting `.groovy` files.
diff --git a/src/main/java/org/apache/tomcat/jakartaee/Migration.java b/src/main/java/org/apache/tomcat/jakartaee/Migration.java
index 3d87d1a..bd18767 100644
--- a/src/main/java/org/apache/tomcat/jakartaee/Migration.java
+++ b/src/main/java/org/apache/tomcat/jakartaee/Migration.java
@@ -32,8 +32,11 @@ import java.util.Set;
 import java.util.concurrent.TimeUnit;
 import java.util.logging.Level;
 import java.util.logging.Logger;
+import java.util.zip.CRC32;
+import java.util.zip.ZipEntry;
 import java.util.zip.ZipException;
 
+import org.apache.commons.compress.archivers.ArchiveEntry;
 import org.apache.commons.compress.archivers.zip.ZipArchiveEntry;
 import org.apache.commons.compress.archivers.zip.ZipArchiveInputStream;
 import org.apache.commons.compress.archivers.zip.ZipArchiveOutputStream;
@@ -211,8 +214,8 @@ public class Migration {
 
 
     private void migrateArchiveStreaming(InputStream src, OutputStream dest) throws IOException {
-        try (ZipArchiveInputStream srcZipStream = new ZipArchiveInputStream(new CloseShieldInputStream(src));
-                ZipArchiveOutputStream destZipStream = new ZipArchiveOutputStream(new CloseShieldOutputStream(dest))) {
+        try (ZipArchiveInputStream srcZipStream = new ZipArchiveInputStream(CloseShieldInputStream.wrap(src));
+                CrcZipArchiveOutputStream destZipStream = new CrcZipArchiveOutputStream(CloseShieldOutputStream.wrap(dest))) {
             ZipArchiveEntry srcZipEntry;
             while ((srcZipEntry = srcZipStream.getNextZipEntry()) != null) {
                 String srcName = srcZipEntry.getName();
@@ -221,7 +224,7 @@ public class Migration {
                     continue;
                 }
                 String destName = profile.convert(srcName);
-                RenamableZipArchiveEntry destZipEntry = new RenamableZipArchiveEntry(srcZipEntry);
+                MigrationZipArchiveEntry destZipEntry = new MigrationZipArchiveEntry(srcZipEntry, false);
                 destZipEntry.setName(destName);
                 destZipStream.putArchiveEntry(destZipEntry);
                 migrateStream(srcName, srcZipStream, destZipStream);
@@ -251,7 +254,7 @@ public class Migration {
                     continue;
                 }
                 String destName = profile.convert(srcName);
-                RenamableZipArchiveEntry destZipEntry = new RenamableZipArchiveEntry(srcZipEntry);
+                MigrationZipArchiveEntry destZipEntry = new MigrationZipArchiveEntry(srcZipEntry, true);
                 destZipEntry.setName(destName);
                 destZipStream.putArchiveEntry(destZipEntry);
                 migrateStream(srcName, srcZipFile.getInputStream(srcZipEntry), destZipStream);
@@ -316,10 +319,31 @@ public class Migration {
         return false;
     }
 
-    private static class RenamableZipArchiveEntry extends ZipArchiveEntry {
-
-        public RenamableZipArchiveEntry(ZipArchiveEntry entry) throws ZipException {
+    private static class MigrationZipArchiveEntry extends ZipArchiveEntry {
+        protected final CRC32 crc = new CRC32();
+        protected long size = 0;
+        protected boolean needResetCrc;
+        public MigrationZipArchiveEntry(ZipArchiveEntry entry, boolean inMemory) throws ZipException {
             super(entry);
+            // No recalculation required, when in memory mode and not of type SORTED
+            needResetCrc = !inMemory && entry.getMethod() == ZipEntry.STORED;
+        }
+
+        @Override
+        public long getSize() {
+            return needResetCrc ? size : super.getSize();
+        }
+
+        @Override
+        public long getCrc() {
+            return needResetCrc ? crc.getValue() : super.getCrc();
+        }
+
+        public void update(byte[] b, int offset, int length) {
+            if (needResetCrc) {
+                crc.update(b, offset, length);
+                size += length;
+            }
         }
 
         @Override
@@ -327,4 +351,41 @@ public class Migration {
             super.setName(name);
         }
     }
+
+    private static class CrcZipArchiveOutputStream extends ZipArchiveOutputStream {
+        private MigrationZipArchiveEntry current;
+        private CrcZipArchiveOutputStream(OutputStream out) {
+            super(out);
+        }
+
+        @Override
+        public void write(byte[] b, int offset, int length) throws IOException {
+            super.write(b, offset, length);
+            update(b, offset, length);
+        }
+
+        @Override
+        public void putArchiveEntry(ArchiveEntry archiveEntry) throws IOException {
+            if (archiveEntry instanceof MigrationZipArchiveEntry) {
+                current = (MigrationZipArchiveEntry) archiveEntry;
+            }
+            super.putArchiveEntry(archiveEntry);
+        }
+
+        @Override
+        public void closeArchiveEntry() throws IOException {
+            reset();
+            super.closeArchiveEntry();
+        }
+
+        private void reset() {
+            current = null;
+        }
+
+        private void update(byte[] b, int offset, int length) {
+            if (current != null) {
+                current.update(b, offset, length);
+            }
+        }
+    }
 }


---------------------------------------------------------------------
To unsubscribe, e-mail: dev-unsubscribe@tomcat.apache.org
For additional commands, e-mail: dev-help@tomcat.apache.org