From 65f681b7838719283fd21b1aeef3ba4d67ba8e80 Mon Sep 17 00:00:00 2001 From: Klaus Brunner Date: Thu, 23 Feb 2023 14:20:18 +0100 Subject: [PATCH] fix: clearer signalling of "zip overflow" Related to #5. --- .../net/e175/klaus/zip/ZipOverflowException.java | 16 ++++++++++++++++ .../java/net/e175/klaus/zip/ZipPrefixer.java | 15 +++++++++------ .../klaus/zip/CountingSeekableByteChannel.java | 4 +++- .../java/net/e175/klaus/zip/ZipPrefixerTest.java | 3 +-- 4 files changed, 29 insertions(+), 9 deletions(-) create mode 100644 src/main/java/net/e175/klaus/zip/ZipOverflowException.java diff --git a/src/main/java/net/e175/klaus/zip/ZipOverflowException.java b/src/main/java/net/e175/klaus/zip/ZipOverflowException.java new file mode 100644 index 0000000..a898767 --- /dev/null +++ b/src/main/java/net/e175/klaus/zip/ZipOverflowException.java @@ -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); + } +} diff --git a/src/main/java/net/e175/klaus/zip/ZipPrefixer.java b/src/main/java/net/e175/klaus/zip/ZipPrefixer.java index dc70ebd..40aa8b4 100644 --- a/src/main/java/net/e175/klaus/zip/ZipPrefixer.java +++ b/src/main/java/net/e175/klaus/zip/ZipPrefixer.java @@ -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)); @@ -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 prefixFiles) throws IOException { validateZipOffsets(isUsableFile(targetPath)); @@ -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; @@ -329,9 +332,9 @@ private static Queue 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; } diff --git a/src/test/java/net/e175/klaus/zip/CountingSeekableByteChannel.java b/src/test/java/net/e175/klaus/zip/CountingSeekableByteChannel.java index 8919178..dd7c1e6 100644 --- a/src/test/java/net/e175/klaus/zip/CountingSeekableByteChannel.java +++ b/src/test/java/net/e175/klaus/zip/CountingSeekableByteChannel.java @@ -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; diff --git a/src/test/java/net/e175/klaus/zip/ZipPrefixerTest.java b/src/test/java/net/e175/klaus/zip/ZipPrefixerTest.java index debbd8e..53b0bb5 100644 --- a/src/test/java/net/e175/klaus/zip/ZipPrefixerTest.java +++ b/src/test/java/net/e175/klaus/zip/ZipPrefixerTest.java @@ -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.*; @@ -117,7 +116,7 @@ void bailsOutOn4gBoundaryCrossing() throws IOException { validateZipOffsets(zip); assertThrows( - ZipException.class, + ZipOverflowException.class, () -> applyPrefixesToZip(zip, Arrays.asList(filler, filler, filler, filler))); }