Check out the official documentation, which comes with the official demo:
// Official demo package main import ( "archive/tar" "bytes" "fmt" "io" "log" "os" ) func main() { // Write several files to a zip file // The source files are written directly into the code, and then there's no documentation. // Later, we'll demonstrate the compression of the source file and output a compressed document. var buf tw := (&buf) var files = []struct { Name, Body string }{ {"", "This archive contains some text files."}, {"", "Gopher names:\nGeorge\nGeoffrey\nGonzo"}, {"", "Get animal handling license."}, } for _, file := range files { hdr := &{ Name: , Mode: 0600, Size: int64(len()), } if err := (hdr); err != nil { (err) } if _, err := ([]byte()); err != nil { (err) } } if err := (); err != nil { (err) } // After the above compression, the compressed content is stored in the buf variable. // Just iterate through it and output it. tr := (&buf) for { hdr, err := () if err == { break // The file has been traversed. } if err != nil { (err) } ("Contents of document %s: ", ) if _, err := (, tr); err != nil { (err) } () } }
1, compressed and output documents
The official demo does not output the compressed content to the document, but it is often used in practice. You can use the file operations in the os library to write the compressed content to a document.
The code is as follows:
// tar compression // Here's a demonstration of compressing a file from source and outputting a document. // By Jiebin Chen // Reference: http:///golang-tar/ package main import ( "archive/tar" "bytes" "log" "os" ) func main() { // Create a buffer to hold the contents of the compressed file. var buf // Create a compressed document tw := (&buf) // Define a bunch of files // Write files to zip file tw var files = []struct { Name, Body string }{ {"", "This archive contains some text files."}, {"", "Gopher names:\nGeorge\nGeoffrey\nGonzo"}, {"", "Get animal handling license."}, } for _, file := range files { hdr := &{ Name: , Mode: 0600, Size: int64(len()), } if err := (hdr); err != nil { (err) } if _, err := ([]byte()); err != nil { (err) } } if err := (); err != nil { (err) } // Write the contents of the compressed document to a file f, err := ("", os.O_CREATE|os.O_WRONLY, 0666) if err != nil { (err) } (f) }
Execute it haha:
> go run
Tar compression in golang:
2、tar decompression
As above, it is possible to useos
The library reads the contents of the document into the
// tar unpacking demo // This demonstrates decompressing a file from its source and outputting the contents of the file // By Jiebin Chen // Reference: http:///golang-tar/ package main import ( "archive/tar" "fmt" "io" "log" "os" ) func main() { // Decompressing requires a method, which takes an object // How do you get the object from the source file over there? // This way, by opening the file, you get an object. // Because of the Read method he implements, all can be passed directly to the file, err := ("") if err != nil { (err) } defer () // The tar object reads the contents of the file and traverses the output. tr := (file) for { hdr, err := () if err == { break // End of archive } if err != nil { (err) } ("%s file contents:\n", ) if _, err := (, tr); err != nil { (err) } () } }
Execute it:
> go run
tar unpacking in golang:
This article on golang tar compression and decompression file details of the article is introduced to this, more related to golang tar compression and decompression file content, please search for my previous articles or continue to browse the following related articles I hope you will support me in the future more!