Archives
Archives are a file format that stores multiple files. Two of the most common archive formats are tar balls ZIP archives. The Go standard library has both the tar
and zip
packages. These examples use the ZIP format, but the tar format can easily be interchanged.
Archive (ZIP) files
The following example demonstrates how to create an archive with multiple files inside. The files in the example are hard-coded with only a few bytes, but should be easily adapted to suit other needs:
// This example uses zip but standard library // also supports tar archives package main import ( "archive/zip" "log" "os" ) func main() { // Create a file to write the archive buffer to // Could also use an in memory buffer. outFile, err := os.Create("test.zip") if err != nil { log.Fatal(err) } defer outFile.Close() // Create a zip writer on top of the file writer zipWriter := zip.NewWriter(outFile) // Add files to archive // We use some...