Unable to create a tarball archive/tar write too long
Interview Questions practice on Codemia
Over 8,000 real interview questions from top companies, searchable by company and role.
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.
Related reading
- Unable to create Android Virtual Device
- Unable to create Android Virtual Device
- Unable to create call adapter for class example.Simple
- Unable to create new native thread error - but very few threads are in use
- Unable to create spark session
- Unable to delete cfn stack, role is invalid or cannot be assumed
- Unable to delete S3 bucket
- unable to dequeue a cell with identifier Cell - must register a nib or a class for the identifier or connect a prototype cell in a storyboard
.png&w=3840&q=75)
Tackling System Design Interview Problems
A short course that equips you with the skills to approach system design interviews methodically.
Start the free courseTrack what you have practised
A free account saves your progress, solutions and study plan across every problem on Codemia.
Interview Questions practice on Codemia
Over 8,000 real interview questions from top companies, searchable by company and role.