bodewig commented on a change in pull request #85: COMPRESS-497 Handle missing 
endheader offset
URL: https://github.com/apache/commons-compress/pull/85#discussion_r341816574
 
 

 ##########
 File path: 
src/main/java/org/apache/commons/compress/archivers/sevenz/SevenZFile.java
 ##########
 @@ -441,17 +441,81 @@ private Archive readHeaders(final byte[] password) 
throws IOException {
                     archiveVersionMajor, archiveVersionMinor));
         }
 
+        boolean headerLooksValid = false;  // See 
https://www.7-zip.org/recover.html - "There is no correct End Header at the end 
of archive"
         final long startHeaderCrc = 0xffffFFFFL & buf.getInt();
-        final StartHeader startHeader = readStartHeader(startHeaderCrc);
+        if (startHeaderCrc == 0) {
+            // This is an indication of a corrupt header - peek the next 20 
bytes
+            long currentPosition = channel.position();
+            ByteBuffer peekBuf = ByteBuffer.allocate(20);
+            readFully(peekBuf);
+            channel.position(currentPosition);
+            // Header invalid if all data is 0
+            while (peekBuf.hasRemaining()) {
+                if (peekBuf.get()!=0) {
+                    headerLooksValid = true;
+                    break;
+                }
+            }
+        } else {
+            headerLooksValid = true;
+        }
+
+        if (headerLooksValid) {
+            final StartHeader startHeader = readStartHeader(startHeaderCrc);
+            return initializeArchive(startHeader, password, true);
+        } else {
+            // No valid header found - probably first file of multipart 
archive was removed too early. Scan for end header.
 
 Review comment:
   actually I can do this kind of cosmetic change myself,  :-)

----------------------------------------------------------------
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
 
For queries about this service, please contact Infrastructure at:
[email protected]


With regards,
Apache Git Services

Reply via email to