Skip to content

Commit

Permalink
Fixed bug
Browse files Browse the repository at this point in the history
Fixed bug that files inside archives were hashed wronh due to how 7-Zip-JBinding works (32 KB of data to each write() call). This affects archives with single file, that, if the file was bigger than 32KB got reconized with multiple files
  • Loading branch information
FlyingWolFox committed Apr 26, 2020
1 parent abbbe09 commit 9adf649
Showing 1 changed file with 11 additions and 15 deletions.
26 changes: 11 additions & 15 deletions Archive.java
Original file line number Diff line number Diff line change
Expand Up @@ -73,7 +73,7 @@ public void uncompress() throws IOException {
inArchive.extract(in, false, // Non-test mode
new MyExtractCallback(inArchive));
} catch (Exception e) {
System.err.println(":.. :> Error occurs: " + e);
System.err.println(":.. : > Error occurs: " + e);
e.printStackTrace();
} finally {
if (inArchive != null) {
Expand Down Expand Up @@ -102,12 +102,16 @@ public static class MyExtractCallback implements IArchiveExtractCallback {
private int index;
private boolean skipExtraction;
private IInArchive inArchive;
private static byte[] digest;
private static MessageDigest digest;
private static ArrayList<byte[]> digests;

public MyExtractCallback(IInArchive inArchive) {
this.inArchive = inArchive;
digest = null;
try {
digest = MessageDigest.getInstance("MD5");
} catch (NoSuchAlgorithmException e) {
e.printStackTrace();
}
digests = new ArrayList<byte[]>();
}

Expand All @@ -119,14 +123,7 @@ public ISequentialOutStream getStream(int index, ExtractAskMode extractAskMode)
}
return new ISequentialOutStream() {
public int write(byte[] data) throws SevenZipException {
MessageDigest complete;
try {
complete = MessageDigest.getInstance("MD5");
digest = complete.digest(data);
digests.add(digest);
} catch (NoSuchAlgorithmException e) {
e.printStackTrace();
}
digest.update(data);
return data.length; // Return amount of proceed data
}
};
Expand All @@ -143,7 +140,7 @@ public void setOperationResult(ExtractOperationResult extractOperationResult) th
System.err.println(":..: > Extraction error");
} else {
System.out.println(String.format(":.. :.. %s [OK]", inArchive.getProperty(index, PropID.PATH)));
digest = null;
digests.add(digest.digest());
}
}

Expand Down Expand Up @@ -172,8 +169,6 @@ public boolean equals(Object anObject) {
if (this == archive)
return true;

// Collections.sort(this.hashes);
// Collections.sort(archive.hashes);
return this.hashes.equals(archive.hashes);
}

Expand Down Expand Up @@ -237,7 +232,8 @@ public static boolean isArchive(String filename) {
i--;
}

if (i < 0) i = 0;
if (i < 0)
i = 0;

String fileExtension = filename.substring(i);
for (String extension : extensions) {
Expand Down

0 comments on commit 9adf649

Please sign in to comment.