hashicorp/packer · error

Failed to add zip header for %s: %s

Error message

Failed to add zip header for %s: %s

What it means

In createZipArchive, archive.Create(path) registers the file in the zip and writes its header. If the zip writer cannot create the entry (rare — usually the underlying writer already failed), this error is returned with the path and cause. The zip is abandoned.

Source

Thrown at post-processor/compress/post-processor.go:427

	return nil
}

func createZipArchive(files []string, output io.WriteCloser) error {
	archive := zip.NewWriter(output)
	defer archive.Close()

	for _, path := range files {
		path = filepath.ToSlash(path)

		source, err := os.Open(path)
		if err != nil {
			return fmt.Errorf("Unable to read file %s: %s", path, err)
		}
		defer source.Close()

		target, err := archive.Create(path)
		if err != nil {
			return fmt.Errorf("Failed to add zip header for %s: %s", path, err)
		}

		_, err = io.Copy(target, source)
		if err != nil {
			return fmt.Errorf("Failed to copy %s data to archive: %s", path, err)
		}
	}
	return nil
}

View on GitHub (pinned to eb36e3c3e4)

Solutions

  1. Check disk space and writability of the compress post-processor's output path.
  2. Re-run the build after fixing the output stream.
  3. Look at the wrapped cause — it points at the actual writer failure.
  4. Ensure no other step concurrently truncates or closes the output archive.

Example fix

// before
{"type": "compress", "output": "/full-disk/a.zip"}
// after: point at a volume with free space
{"type": "compress", "output": "/data/artifacts/a.zip"}
Defensive patterns

Strategy: validation

Validate before calling

# preflight: writable output path with free space
[ -w "$(dirname "$OUTPUT")" ] && [ "$(df --output=avail -B1 "$OUTPUT_DIR" | tail -1)" -gt "$MIN_BYTES" ] || exit 1

Prevention

When it happens

Trigger: zip.Writer.Create returns an error — practically only when the output writer is broken (closed pipe, disk full) because Create itself only formats a header and defers writing.

Common situations: Output stream/pipe closed early (e.g. downstream writer already errored); disk full so a later flush fails and surfaces on a subsequent Create; corrupt or closed output file handle.

Related errors


AI-assisted analysis of hashicorp/packer@eb36e3c3e4 (2026-09-05). Data as JSON: /api/errors/072caf216e97f3c8. Report an issue: GitHub.