ZIP has good libraries (fflate, JSZip). TAR gets less attention, but it's one of the simplest file formats you'll come across, which makes it a good format to write by hand. Here's how I build .tar files fully in the browser.

The format in one paragraph

A TAR file is a sequence of 512-byte blocks. Each file gets one header block, followed by its data padded with zeros to a multiple of 512. The archive ends with two empty blocks. There's no central directory, no compression and no index. (.tar.gz is just that stream run through gzip.)

The header

The fields that matter, in the POSIX "ustar" layout:

Offset Size Field
0 100 file name
100 8 mode (octal, e.g. 0000644)
124 12 size in bytes (octal)
136 12 mtime, Unix seconds (octal)
148 8 checksum
156 1 type (0 = file, 5 = directory)
257 6 ustar\0 magic
263 2 version 00
345 155 name prefix (for long paths)

Numbers are ASCII octal strings, not binary. That's the one odd thing about the format.

The code

const enc = new TextEncoder(); function writeStr(buf: Uint8Array, off: number, len: number, s: string) { buf.set(enc.encode(s).subarray(0, len), off); } function writeOctal(buf: Uint8Array, off: number, len: number, n: number) { writeStr(buf, off, len, n.toString(8).padStart(len - 1, "0") + "\0"); } function header(name: string, size: number, mtime: number): Uint8Array { const h = new Uint8Array(512); writeStr(h, 0, 100, name); writeOctal(h, 100, 8, 0o644); writeOctal(h, 108, 8, 0); // uid writeOctal(h, 116, 8, 0); // gid writeOctal(h, 124, 12, size); writeOctal(h, 136, 12, Math.floor(mtime / 1000)); h[156] = 0x30; // '0' = regular file writeStr(h, 257, 6, "ustar\0"); writeStr(h, 263, 2, "00"); // Checksum: sum of all bytes with the checksum field read as spaces h.fill(0x20, 148, 156); const sum = h.reduce((a, b) => a + b, 0); writeStr(h, 148, 8, sum.toString(8).padStart(6, "0") + "\0 "); return h; } export async function createTar(files: File[]): Promise<Blob> { const parts: BlobPart[] = []; for (const f of files) { parts.push(header(f.name, f.size, f.lastModified)); parts.push(f); // stream, don't copy const pad = (512 - (f.size % 512)) % 512; if (pad) parts.push(new Uint8Array(pad)); } parts.push(new Uint8Array(1024)); // two end blocks return new Blob(parts, { type: "application/x-tar" }); } 

Notice that parts.push(f) adds the original File to the Blob without reading it into memory. The browser puts the archive together lazily, so even multi-gigabyte inputs don't use up RAM.

Things that will bite you

  • Names longer than 100 bytes. Split the path at a / into prefix (155) and name (100). For anything longer you need PAX extended headers (type x).
  • UTF-8 names. Measure length in bytes, not characters. "é".length is 1 but takes 2 bytes.
  • Directories. Type 5, size 0, and the name ends with /.
  • The checksum is computed with its own 8 bytes treated as spaces. Forget that and tar will say "checksum error".

Gzip it for free

Modern browsers have compression built in:

const tgz = await new Response( tar.stream().pipeThrough(new CompressionStream("gzip")) ).blob(); 

That gives you a .tar.gz without any library.

Test against the real thing

I test the writer by generating archives and checking them with tar -tvf and tar -xf, plus a unit test that parses the headers back. Byte-level formats make round-trip tests very easy to write.

If you just need the result, I put this in a free tool: Create TAR online. Files never leave your browser. There's also a ZIP ↔ TAR ↔ TAR.GZ converter built the same way.