archive package provides implementations for reading and writing archive files. It contains two main subpackages for working with tar and zip formats.
Subpackages
archive/tar
Provides sequential access to tar archives. Key Types:Reader- Sequential access to tar archive contentsWriter- Writing tar archivesHeader- Represents a single header in a tar archive
archive/zip
Provides support for reading and writing ZIP archives. Key Types:Reader- Reading zip archivesReadCloser- Reader that must be closedWriter- Writing zip archivesFile- A single file in a ZIP archive
Common Use Cases
- Backup and restore: Create compressed archives of files and directories
- Distribution: Package multiple files for download or transfer
- Deployment: Bundle application assets and resources
- Data compression: Reduce storage space and transfer time
Security Considerations
Botharchive/tar and archive/zip validate file paths to prevent directory traversal attacks:
- Use
filepath.IsLocal()to check for non-local paths - The
ErrInsecurePatherror is returned for insecure paths - Control behavior with the
GODEBUGenvironment variables:tarinsecurepathandzipinsecurepath
Related Packages
compress/gzip- Compress tar archives with gzipcompress/bzip2- Compress with bzip2io- Low-level I/O primitivesos- Operating system file operations