Skip to content
Back to Blog
2026-11-19Testing

Empty File vs Zero-Byte File: The Upload Validation Corner Everyone Misses

Every upload pipeline has a validation branch for "file too large." Almost none have a correct branch for "file contains nothing" — and that branch has two distinct cases that get conflated: the zero-byte file and the empty-but-valid file. One is the absence of data. The other is a perfectly legal container with no payload. Your code paths for them should not be the same, and testing both takes five minutes once you can create each on demand.

The Two Kinds of Empty

Zero-byte (0 B): no bytes at all. Created by touch, by a failed transfer, or by a process that opened a file and died before writing.

Empty-but-valid: a structurally correct file whose payload is empty. An empty ZIP archive (22 bytes — just the end-of-central-directory record). A JSON document that is {} (2 bytes). A CSV with a header row and zero data rows. These files parse cleanly; they just carry nothing. Their size is nonzero, so a naive size > 0 check waves them through.

The trap: validators that pass both, parsers that choke on one of them, and UIs that report "upload complete" for the first. If you have never tested this, your system has an untested branch today.

Creating the Zero-Byte File

Every platform makes this trivial, which is exactly why it slips into production by accident:

touch empty.txt           # 0 bytes
: > empty2.txt            # 0 bytes, pure shell
truncate -s 0 empty3.txt  # truncate an existing file to nothing
printf '' > empty4.txt    # note: echo > f is NOT zero bytes (newline = 1 byte)
// Node.js
const fs = require('fs');
fs.writeFileSync('empty.txt', '');        // 0 bytes
fs.promises.open('empty.txt', 'w').then(f => f.close()); // also 0 bytes

That last bash comment deserves emphasis: echo hi > f and echo > f both write a newline. A whole class of "mystery 1-byte file" bugs is just that. Use printf '' when you truly mean empty, and reach for a plain TXT fixture when you mean "known small text."

Creating the Empty-but-Valid File

This is where it gets format-specific, and where each one is a genuine parser test:

# Empty ZIP: the 22-byte EOCD record, nothing else
printf 'PK\x05\x06%.0s' ... # easier with Python:
python3 -c "open('empty.zip','wb').write(b'PK\x05\x06'+b'\x00'*18)"

# Empty JSON object / array
printf '{}' > empty-object.json
printf '[]' > empty-array.json

# Header-only CSV
printf 'id,name,email\n' > header-only.csv

An empty ZIP is a thing of beauty: 22 bytes, opens in every archive tool, contains nothing. Java's ZipInputStream, Python's zipfile, and Node's yauzl all accept it — but iterate differently. If your ZIP processing path assumes at least one entry, here is your failing test. Similarly, a JSON parser test suite without {}, [], and an empty-string document ("") is not complete, and a CSV importer that crashes on a header-only file will crash on the first honest empty export from someone else's system.

Why Zero-Byte Files Appear in Production

Not because anyone created them on purpose:

  • Aborted uploads. A proxy or browser writes the destination file first, then streams into it. User closes the tab mid-transfer — a 0-byte ghost remains, often with a success-looking log line.
  • S3 multipart aborts. An incomplete multipart upload can finalize as a 0-byte object if the completion call is malformed but tolerated.
  • rsync / scp interrupted. The destination file exists, fully named, containing nothing.
  • Disk-full writes. Many programs create the file, then fail on the first write, then exit without cleaning up.

The defensive sweep is one line: find /var/uploads -type f -size 0. Run it in cron, alert on results, and you will find real incidents.

What Your Validator Should Actually Do

InputSizeValid?Correct behavior
Zero-byte file0 BNoReject: "no content" — distinct error message
Empty ZIP (22 B)> 0 BYes, structurallyAccept or reject by business rule, but parse it first
{} JSON2 BYesUsually accept; empty is valid JSON
Header-only CSV~15 BYesAccept with row count 0
1-byte file (newline)1 BProbably notFlag as suspicious; often a broken echo

Two rules fall out of the table. First, size checks and parse checks are different layers — do both, report which one failed. Second, never infer content from the extension: a 0-byte .pdf is not "an empty PDF," it is no PDF at all; a valid PDF with zero pages barely exists as a format and every viewer argues about it. If you need a real minimal PDF fixture, use a generated one with actual structure, not a truncated accident.

CI Recipe

# zero-and-empty.yml
- name: empty-input matrix
  run: |
    python3 -c "open('t.zip','wb').write(b'PK\x05\x06'+b'\x00'*18)"
    printf '{}' > t.json
    touch t.zero
    for f in t.zip t.json t.zero; do
      ./your-uploader "$f" && echo "PASS accepted $f" || echo "PASS rejected $f"
    done

The assertions live in your uploader's exit codes. What you are really testing is that each branch returns a specific, documented outcome — not that it merely does not crash. Zero-byte in, "ERR_NO_CONTENT" out; empty ZIP in, "OK, 0 entries" or "ERR_EMPTY_ARCHIVE" out — pick one, write it down, test it.

Empty is not one test case. It is at least five, and now you have the commands for all of them.

#empty-file#zero-byte#upload-validation#test-files#qa