ZIP Compression Implementation in JavaScript
14-day free trial
Try SeekerPro free for 14 days.
Full access while you try it · then $15.99/mo · cancel anytime
Start your 14-day free SeekerPro trial →30-day money-back guarantee · Cancel anytime
Not checking a site today? Turn this topic into a ready to paste AI prompt, free →
Key Takeaways
- ✓ZIP Compression runs in O(n) time and O(n) space, in JavaScript as in every other language.
- ✓Node has no ZIP container in the stdlib. node:zlib gives only the payload codec, and ZIP method 8 is RAW deflate, so the calls are deflateRaw / deflateRawSync / createDeflateRaw, never deflate (zlib-wrapped) or gzip.
- ✓Reviewers reject zlib.deflateSync anywhere the format needs raw deflate, and reject header building by string concatenation: everything goes through Buffer or DataView with an explicit little-endian argument.
- ✓The JavaScript trap: Two number traps that exist only in JavaScript. First, CRC-32 is unsigned but every JS bitwise operator coerces to signed int32, so (c ^ 0xFFFFFFFF) yields a negative number roughly half the time, whenever the running register's top bit is clear (measured: exactly 1000 of 2000 filenames).
- ✓deflateRawSync blocks the event loop for the entire member, so one 200 MB upload stalls every other request in the process.
What ZIP Compression actually is
ZIP Compression in JavaScript: what you actually reach for
JavaScript implementation
import { deflateRawSync } from "node:zlib";
const TABLE = Uint32Array.from({ length: 256 }, (_, n) => {
let c = n;
for (let k = 0; k < 8; k++) c = c & 1 ? 0xedb88320 ^ (c >>> 1) : c >>> 1;
return c >>> 0;
});
function crc32(bytes) {
let c = 0xffffffff;
for (const b of bytes) c = TABLE[(c ^ b) & 0xff] ^ (c >>> 8);
return (c ^ 0xffffffff) >>> 0; // >>> 0 or this is a negative int32
}
function localHeader(name, crc, csize, usize) {
const nameBytes = Buffer.from(name, "utf8");
const h = Buffer.alloc(30 + nameBytes.length);
const v = new DataView(h.buffer, h.byteOffset, h.byteLength);
v.setUint32(0, 0x04034b50, true); // true = little endian; default is BIG
v.setUint16(4, 20, true);
v.setUint16(6, 0x0800, true); // flag bit 11: name is UTF-8
v.setUint16(8, 8, true); // method 8 = deflate
const dosTime = 0; // 00:00:00
const dosDate = (0 << 9) | (1 << 5) | 1; // 1980-01-01; a zero date is month 0/day 0
v.setUint16(10, dosTime, true);
v.setUint16(12, dosDate, true);
v.setUint32(14, crc, true);
v.setUint32(18, csize, true);
v.setUint32(22, usize, true);
v.setUint16(26, nameBytes.length, true);
nameBytes.copy(h, 30);
return h;
}
const body = Buffer.from("hello zip\n");
const comp = deflateRawSync(body, { level: 6 });
const member = Buffer.concat([
localHeader("a.txt", crc32(body), comp.length, body.length),
comp,
]);Think Your Code Is Clean? Let NexusBro QA It in 20 Seconds.
Paste your code. Click QA. Get an instant expert-level audit with fixes.
QA My Code FreeThe JavaScript-specific trap
Performance in JavaScript, beyond the O(n)
Real-World Applications
Interview Tips & Practice
Frequently Asked Questions
Which JavaScript type or library should I use for ZIP Compression?
Node has no ZIP container in the stdlib. node:zlib gives only the payload codec, and ZIP method 8 is RAW deflate, so the calls are deflateRaw / deflateRawSync / createDeflateRaw, never deflate (zlib-wrapped) or gzip. Browsers expose the same codec as new CompressionStream("deflate-raw"). For the container the ecosystem standards are yauzl and yazl (streaming, central-directory correct), archiver (higher level, tar too), fflate (tiny, runs in the browser) and jszip (universal, but assembles the whole archive in memory).
What is the most common JavaScript mistake when implementing ZIP Compression?
Two number traps that exist only in JavaScript. First, CRC-32 is unsigned but every JS bitwise operator coerces to signed int32, so (c ^ 0xFFFFFFFF) yields a negative number roughly half the time, whenever the running register's top bit is clear (measured: exactly 1000 of 2000 filenames). That coin flip is the danger, because the first fixture you try has even odds of passing and the failures arrive in production; without a trailing >>> 0 you either write a garbage CRC or throw out of writeUInt32LE. Second, DataView.setUint32(offset, value) defaults to BIG-endian, while every field in a ZIP record is little-endian. Forget the third argument and the file writes cleanly, reports no error, and unzips nowhere. Buffer.writeUInt32LE carries the endianness in its name and has no such default, which is why mixed DataView/Buffer code is where this bug lives.
How does ZIP Compression actually perform in JavaScript?
deflateRawSync blocks the event loop for the entire member, so one 200 MB upload stalls every other request in the process. The async form runs on the libuv threadpool, whose default size is 4, so the fifth concurrent compression simply queues behind the others. Each zlib stream also holds a native arena sized by windowBits and memLevel, a few hundred KB, so thousands of concurrent tiny members run out of memory before they run out of speed. Collect chunks and Buffer.concat once; concatenating per chunk re-copies the whole payload each time. Time complexity is O(n) and space is O(n).
Related Articles
Rather try before you buy?
Get the rest of the vault: every cheat sheet, guide and audit in SeekerPro, free for 14 days, then $15.99/mo. Cancel anytime. Daily usage limits apply.
Start my 14-day SeekerPro trialBefore paying for Devin, check its student and free-tier options on Noizz.
Is your site built to last?
Run a free QA audit and get your Site Health Score in seconds.
Check Your Site FreeNo signup required
Reader comments
No account needed. Share what worked, what did not, and what you would add.
Add your take below. A concrete tip, a result or a question helps the next reader most.