Zip Entry Size Budget Guard
Cap ZipInputStream entries with a streaming byte budget: declared sizes can be unknown, so count every read and abort the moment an entry overshoots.
Code
import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.nio.charset.StandardCharsets;
import java.util.zip.ZipEntry;
import java.util.zip.ZipInputStream;
import java.util.zip.ZipOutputStream;
public class UploadGatekeeper {
static final long MAX_ENTRY_BUDGET = 64 * 1024;
// 流式预算:边读边记账,条目一旦超限立即中止
static String readEntry(ZipInputStream zip, ZipEntry entry) throws IOException {
long declared = entry.getSize();
if (declared >= 0 && declared > MAX_ENTRY_BUDGET) {
throw new IOException("declared size " + declared + " breaks the budget");
}
ByteArrayOutputStream out = new ByteArrayOutputStream();
byte[] buffer = new byte[8192];
long total = 0;
for (int read; (read = zip.read(buffer)) != -1; ) {
total += read;
if (total > MAX_ENTRY_BUDGET) {
throw new IOException("entry crossed the " + MAX_ENTRY_BUDGET + " byte budget");
}
out.write(buffer, 0, read);
}
return out.toString(StandardCharsets.UTF_8);
}
public static void main(String[] args) throws IOException {
ByteArrayOutputStream archive = new ByteArrayOutputStream();
try (ZipOutputStream zip = new ZipOutputStream(archive)) {
zip.putNextEntry(new ZipEntry("payload.txt"));
zip.write(new byte[1_000_000]); // 全 0 明文,DEFLATED 后只有约 1KB
zip.closeEntry();
}
try (ZipInputStream unzip =
new ZipInputStream(new ByteArrayInputStream(archive.toByteArray()))) {
ZipEntry entry = unzip.getNextEntry();
System.out.println("declared size = " + entry.getSize()); // 流式读取时通常未知
try {
readEntry(unzip, entry);
} catch (IOException overBudget) {
System.out.println("guard fired: " + overBudget.getMessage());
}
}
}
}A close reading of the code
Zip Entry Size Budget Guard keeps everything in the class UploadGatekeeper.
Repetition is confined to for (int read; (read = zip.read(buffer)) != -1; ) — the rest of the file is straight-line. No finally block here — resources declared in the try header close themselves in reverse order. Invalid states are rejected up front: it throws IOException instead of continuing with bad data.
Caveats
- No lockfile entry, no version pinning — the file needs nothing beyond the runtime.
Applying it
The demo output is labeled (declared size =, guard fired:); each label names the call that printed it.
Statements that matter
throw new IOException("entry crossed the " + MAX_ENTRY_BUDGET + " byte budget");
throw new IOException("declared size " + declared + " breaks the budget");
return out.toString(StandardCharsets.UTF_8);
Interactive Tools for This Topic
A/B Test Sample Size CalculatorCalculate required per-variant sample size and experiment duration for two-proportion A/B tests from baseline conversion, MDE, alpha, and statistical power.Argon2id Hash InfoEstimate Argon2id password-hash latency and memory usage, and check your parameters against OWASP 2024 minimum and recommended settings.bcrypt Cost EstimatorEstimate bcrypt cost factor latency and brute-force resistance, and get a recommended cost for your target hash time and attacker throughput.Byte Size ConverterShow one byte count in every unit side by side, using decimal (kB/MB/GB) or binary (KiB/MiB/GiB) standards.
FAQ
- Is there a failure path for bad input?
- `IOException` is raised at the guard, so invalid state never reaches the main logic.
- What does Zip Entry Size Budget Guard print when run?
- Running the file directly is enough — expect labeled output lines such as `declared size =`, `guard fired:`.
Report a problem with this exampleOpens a pre-filled issue on GitHub.