Unable to create a tarball archive/tar write too long
Master System Design with Codemia
Enhance your system design skills with over 120 practice problems, detailed solutions, and hands-on exercises.
Introduction
The Go error archive/tar: write too long usually means you wrote more bytes for an entry than the tar header declared for that entry. This is not normally a generic filesystem size limit problem. It is a mismatch between the tar.Header.Size value and the actual number of bytes sent to the tar writer for that file.
Why the Error Happens
When you create a tar entry with Go's archive/tar package, you first write a header and then stream the file contents. The header includes the expected byte length.
If the header says the file is 100 bytes long but you write 120 bytes, the tar writer detects the overflow and raises:
That is the core failure mode.
A Correct Minimal Example
A valid tar creation flow looks like this:
The important part is that Size matches the exact number of bytes written afterward.
A Common Broken Pattern
The error often appears when code computes the header size from one value and writes a different value later.
For example:
Here the header claims the entry is 3 bytes long, but the code writes 5 bytes. That triggers archive/tar: write too long.
The same bug appears when:
- string length is counted before encoding changes
- compressed bytes are written after an uncompressed size was recorded
- file content changes after the header is created
- a loop reuses a header incorrectly
Use the Real File Size for Disk Files
When archiving files from disk, the safest path is to read the file metadata and use the actual file size.
This keeps the header size aligned with the real file content.
Be Careful With Generated Content
In-memory content is where this error shows up most often. If you generate JSON, template output, or compressed data on the fly, compute the bytes first and use the exact final length in the header.
A safe pattern is:
- generate the full byte slice
- set
Sizetolen(data) - write the header
- write exactly that byte slice
Do not guess the length from an earlier representation.
Close the Tar Writer Properly
While write too long is specifically about an oversized write, remember to close the tar writer at the end. Closing flushes the tar end markers and finalizes the archive.
This does not fix the size mismatch error, but it is part of producing a valid archive.
Common Pitfalls
- Setting
tar.Header.Sizefrom the wrong value is the main cause ofarchive/tar: write too long. - Writing transformed or re-encoded content after computing the size from the original representation creates a mismatch.
- Reusing headers across loop iterations can accidentally preserve the wrong size for the next file.
- Assuming the error is about total tarball size or disk capacity sends debugging in the wrong direction. It is usually an entry-size mismatch.
- Forgetting to generate the full content before writing the header makes it harder to guarantee the declared size is correct.
Summary
- '
archive/tar: write too longusually means the tar header size is smaller than the actual bytes written for that entry.' - In Go,
tar.Header.Sizemust exactly match the content length that follows. - For files from disk, use
Stat()to get the real size. - For generated content, compute the final byte slice first, then write the header.
- Debug the per-entry size accounting, not generic tarball limits.

