Skip to content

Commit

Permalink
fix: clearer signalling of "zip overflow"
Browse files Browse the repository at this point in the history
Related to #5.
  • Loading branch information
klausbrunner committed Feb 23, 2023
1 parent 8b3f7ca commit 65f681b
Show file tree
Hide file tree
Showing 4 changed files with 29 additions and 9 deletions.
16 changes: 16 additions & 0 deletions src/main/java/net/e175/klaus/zip/ZipOverflowException.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
package net.e175.klaus.zip;

import java.util.zip.ZipException;


/**
* A ZipException that marks cases when the current ZIP file format cannot accommodate the new offsets.
*/
public final class ZipOverflowException extends ZipException {
public ZipOverflowException() {
}

public ZipOverflowException(String s) {
super(s);
}
}
15 changes: 9 additions & 6 deletions src/main/java/net/e175/klaus/zip/ZipPrefixer.java
Original file line number Diff line number Diff line change
Expand Up @@ -92,7 +92,8 @@ private ZipPrefixer() {
* @param targetPath Target ZIP file. Must be a writeable ZIP format file, in a writeable directory with enough space to hold a temporary copy.
* @param prefixes Binary prefixes that will be sequentially written before the original ZIP file's contents.
* @return Total number of bytes written as prefixes.
* @throws IOException on errors related to I/O and ZIP integrity
* @throws IOException on errors related to I/O and ZIP integrity
* @throws ZipOverflowException If the current ZIP format cannot accommodate the new offsets.
*/
public static long applyPrefixesToZip(Path targetPath, byte[]... prefixes) throws IOException {
validateZipOffsets(isUsableFile(targetPath));
Expand All @@ -106,7 +107,8 @@ public static long applyPrefixesToZip(Path targetPath, byte[]... prefixes) throw
* @param targetPath Target ZIP file. Must be a writeable ZIP format file, in a writeable directory with enough space to hold a temporary copy.
* @param prefixFiles Prefix files that will be sequentially written before the original file's contents.
* @return Total number of bytes written as prefixes.
* @throws IOException on errors related to I/O and ZIP integrity
* @throws IOException on errors related to I/O and ZIP integrity
* @throws ZipOverflowException If the current ZIP format cannot accommodate the new offsets.
*/
public static long applyPrefixesToZip(Path targetPath, Collection<Path> prefixFiles) throws IOException {
validateZipOffsets(isUsableFile(targetPath));
Expand Down Expand Up @@ -182,8 +184,9 @@ public static void validateZipOffsets(Path targetPath) throws IOException {
*
* @param targetPath ZIP file to process.
* @param adjustment Offset to add to the current offsets.
* @throws IOException On I/O errors.
* @throws ZipException On errors in the ZIP's integrity.
* @throws IOException On I/O errors.
* @throws ZipException On errors in the ZIP's integrity.
* @throws ZipOverflowException If the current ZIP format cannot accommodate the new offsets.
*/
public static void adjustZipOffsets(Path targetPath, long adjustment) throws IOException {
final boolean mustAdjust = adjustment != 0;
Expand Down Expand Up @@ -329,9 +332,9 @@ private static Queue<Write> analyseOffsets(boolean mustAdjust, long adjustment,
return writeQueue;
}

private static int uintBoundsChecked(long unsignedIntOffset) throws ZipException {
private static int uintBoundsChecked(long unsignedIntOffset) throws ZipOverflowException {
if (unsignedIntOffset > UINT_MAX_VALUE) {
throw new ZipException("This is a non-ZIP64 archive, but would have to be ZIP64 to accommodate the new offsets.");
throw new ZipOverflowException("This is a non-ZIP64 archive, but would have to be ZIP64 to accommodate the new offsets.");
}
return (int) unsignedIntOffset;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,9 @@
import java.nio.channels.SeekableByteChannel;
import java.util.concurrent.atomic.AtomicLong;

/** An instrumented pass-through SeekableByteChannel. */
/**
* An instrumented pass-through SeekableByteChannel.
*/
final class CountingSeekableByteChannel implements SeekableByteChannel {
private final SeekableByteChannel basedOn;

Expand Down
3 changes: 1 addition & 2 deletions src/test/java/net/e175/klaus/zip/ZipPrefixerTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,6 @@
import java.util.Arrays;
import java.util.Collections;
import java.util.List;
import java.util.zip.ZipException;

import static net.e175.klaus.zip.TestUtil.prepareTestFile;
import static net.e175.klaus.zip.ZipPrefixer.*;
Expand Down Expand Up @@ -117,7 +116,7 @@ void bailsOutOn4gBoundaryCrossing() throws IOException {
validateZipOffsets(zip);

assertThrows(
ZipException.class,
ZipOverflowException.class,
() -> applyPrefixesToZip(zip, Arrays.asList(filler, filler, filler, filler)));
}

Expand Down

0 comments on commit 65f681b

Please sign in to comment.