How ZIP Files Work, and How to Build Them in the Browser

After reading this you will understand what a ZIP file actually stores, why some files shrink and others do not, and how to build or unpack an archive entirely on your own machine without uploading anything.

What a ZIP file really is

A ZIP file is a container. It holds one or more files, each with its own name, timestamp, and stored size, plus a small index at the end that lists everything inside. Think of it as a folder you can email as a single object, with optional compression applied per file.

Here is the hook. Take a plain text file of 100,000 bytes that is nothing but the letter a repeated. Zip it, and the result is roughly 200 bytes. That is a compression ratio near 500 to 1. Now take a 100,000 byte JPEG photo, zip it, and you get back about 100,050 bytes: slightly larger than the original. The ZIP format did not fail. The photo was already compressed, so there was nothing left to squeeze, and the archive just added its bookkeeping overhead.

The lesson in those two numbers is the whole subject: ZIP saves space only when the input contains repetition. Text, logs, CSV files, and source code repeat heavily. Photos, video, and MP3 files do not.

The parts inside every ZIP

Open any ZIP in a byte viewer and you will see a repeating structure. Each stored file has three regions.

Local file header
Starts with the signature bytes 50 4B 03 04 (the letters PK followed by 03 04). It records the file name, the compression method, the compressed and uncompressed sizes, and a CRC-32 checksum of the original data.
Compressed data
The file contents, either compressed with DEFLATE (method 8) or stored raw (method 0).
Central directory
A second copy of every file's metadata, gathered at the very end of the archive and marked by the signature 50 4B 01 02. The archive is read from the end, so the central directory is the table of contents.

Those PK bytes are why the format is sometimes called PKZIP: the initials come from Phil Katz, who published it in 1989. If you look at a broken download and the first two bytes are not 50 4B, it is not a valid ZIP.

The central directory at the end is what lets a program list a 4 GB archive instantly without reading all 4 GB. It jumps to the tail, reads the index, and shows you the file list.

How DEFLATE decides what to compress

DEFLATE, the algorithm behind method 8, combines two ideas. First, it replaces repeated byte sequences with short back-references: instead of writing the same 20 bytes again, it writes "go back 300 bytes and copy 20." Second, it assigns short bit codes to common bytes and long codes to rare ones (Huffman coding). Frequent characters end up costing fewer bits than the 8 bits they take in raw form.

The size of the compressed output depends on how predictable the input is. A rough way to think about the floor is Shannon's entropy per byte:

H = -\sum_{i} p_i \log_2 p_i

Here p_i is the probability of byte value i appearing, and H is the average number of bits needed per byte. A file of one repeated character has H \approx 0, so it compresses almost to nothing. A file of uniformly random bytes has H = 8, the maximum, so DEFLATE cannot beat 8 bits per byte and the archive grows slightly.

This is exactly why the tool stores already-compressed files (like JPEGs) without recompressing them. Their bytes are already near-random from DEFLATE's point of view, so method 0 (store) avoids wasting CPU on a file that will not shrink.

A worked example you can reproduce

Zipping a repetitive text file

Create a file named notes.txt whose contents are the word hello followed by a newline, repeated 1,000 times. That is 6,000 bytes on disk (hello is 5 bytes plus 1 newline, times 1,000).

  1. The uncompressed size stored in the header is 6000 bytes.
  2. DEFLATE sees the same 6 bytes over and over, so after the first copy it emits back-references. The compressed payload lands around 40 bytes.
  3. The local header plus file name plus central directory entry add roughly 90 bytes of overhead.
  4. The final archive is about 130 bytes. The compression ratio is 6000 / 130 \approx 46 to 1, or a 97.8% size reduction.

Now repeat with a 6,000 byte file of random bytes. DEFLATE finds no repetition, stores it raw, and the archive is about 6090 bytes. The ratio is below 1: the file got larger by the header overhead.

Same original size, four very different results. Repetition compresses hard, prose compresses moderately, random and pre-compressed data do not compress at all.

Reading the results after you build or open an archive

When you build an archive, look at the ratio per file, not just the total. A total ratio of 1.2 to 1 might hide the truth: one huge log file compressed 10 to 1 while three JPEGs did not budge. The blended number tells you little about either.

When you open an archive, the file list comes from the central directory. Each entry shows the stored size and the original size. If a file shows a compression method of 0 (store) and equal sizes, that is expected for images and archives. A method of 8 (DEFLATE) with a smaller stored size means real compression happened.

The CRC-32 field matters for integrity. It is a 32-bit checksum computed over the uncompressed bytes. When you extract, the decompressor recomputes it and compares. A mismatch means the archive is corrupt or was truncated during download. This is a quick sanity check, not a security guarantee: CRC-32 catches accidental damage, not deliberate tampering.

A file made of one repeated character has entropy near 0 bits per byte and compresses roughly 100 to 1. A file of uniformly random bytes has entropy of 8 bits per byte and cannot be compressed at all. Real text sits around 4.5 bits per byte, compressing near 1.8 to 1.

Common mistakes

The errors below cost real time. Each has a concrete symptom.

  • Expecting photos to shrink. Zipping 50 JPEGs that total 200 MB gives you a 200 MB archive. If you need smaller images, re-encode them at a lower quality; do not blame the ZIP.
  • Double compression. Zipping a folder of .zip, .gz, or .mp4 files wastes CPU and adds overhead. The output is usually a hair larger than the sum of inputs.
  • Assuming a ZIP hides its contents. Without a password, file names and metadata are visible in the central directory as plain text. Anyone with a hex viewer reads them.
  • Confusing compression with encryption. A ZIP with no password protects nothing. This tool does not create password-protected archives, so treat every archive it produces as readable by anyone who receives it.

Never rely on a plain ZIP to keep data private. The bytes are compressed, not encrypted. If a file must stay confidential, encrypt it before you add it to the archive.

Privacy and why this runs locally

This tool builds and unpacks archives inside your browser using the built-in CompressionStream, the same zlib DEFLATE that desktop tools use. Your files never leave your device. There is no upload, no server round trip, and no copy sitting in someone else's logs.

That matters for anything sensitive: medical records, financial exports, personal photos. A web tool that uploads to compress is a tool that has your data. A tool that compresses in the page does not. The practical test is simple: turn off your network after the page loads, then build an archive. If it still works, nothing is being uploaded.

Related tools on this site

Two other tools pair naturally with this one.

After you download an archive from someone, verify it matches what they published. Compute its checksum with the File Hash Checker and compare against the value on their page. That catches corruption the CRC-32 inside the ZIP cannot, since it covers the whole file including the headers.

To see the 50 4B 03 04 signature and the central directory for yourself, open the archive in the Hex Viewer. Watching the offsets and ASCII columns is the fastest way to understand what the format stores.

Frequently asked questions

Why is my ZIP bigger than the original file?

The input was already compressed (a JPEG, MP4, PDF, or another ZIP), so DEFLATE found no repetition and stored it raw. The archive then added header and directory overhead, usually 50 to 100 bytes per file, making the total slightly larger.

Can this tool open password-protected ZIP files?

No. Encrypted archives are not supported for building or extracting. If a ZIP is password-protected, this tool cannot decrypt its contents.

Does zipping change the file's contents?

No. Compression is lossless. When you extract, the bytes come back exactly as they went in, which the CRC-32 checksum confirms. This is different from re-saving a JPEG at lower quality, which permanently discards detail.

Is a ZIP file the same as a folder?

Not quite. A folder is a live directory your operating system manages. A ZIP is a single file that packages a directory tree plus optional compression, so you can move or send it as one object. You must extract it to work with the contents as normal files again.

How much can text really compress?

English text carries about 4.5 bits of information per byte, so a large text file typically shrinks to around 40 to 55% of its size, a ratio near 1.8 to 2.2 to 1. Highly repetitive text, like logs with the same phrases, can go far beyond 10 to 1.