Golang : Archive directory with tar and gzip
In the previous tutorial, we learn how to extract files from tar ball and deal with gzipped tar ball in Golang. However, the tar ball created in the previous tutorial example is created with the tar
command in Unix/Linux OS. In this tutorial, we will learn how to create tar archive and gzip the archive in Golang.
The codes below will create a tar archive from the given source direcotry and compressed the tar if the filename ends with .gz
package main
import (
"archive/tar"
"compress/gzip"
"flag"
"fmt"
"io"
"os"
"strings"
"path/filepath"
)
func checkerror(err error) {
if err != nil {
fmt.Println(err)
os.Exit(1)
}
}
func main() {
flag.Parse() // get the arguments from command line
destinationfile := flag.Arg(0)
if destinationfile == "" {
fmt.Println("Usage : gotar destinationfile.tar.gz source")
os.Exit(1)
}
sourcedir := flag.Arg(1)
if sourcedir == "" {
fmt.Println("Usage : gotar destinationfile.tar.gz source-directory")
os.Exit(1)
}
dir, err := os.Open(sourcedir)
checkerror(err)
defer dir.Close()
files, err := dir.Readdir(0) // grab the files list
checkerror(err)
tarfile, err := os.Create(destinationfile)
checkerror(err)
defer tarfile.Close()
var fileWriter io.WriteCloser = tarfile
if strings.HasSuffix(destinationfile, ".gz") {
fileWriter = gzip.NewWriter(tarfile) // add a gzip filter
defer fileWriter.Close() // if user add .gz in the destination filename
}
tarfileWriter := tar.NewWriter(fileWriter)
defer tarfileWriter.Close()
for _, fileInfo := range files {
if fileInfo.IsDir() {
continue
}
// see https://www.socketloop.com/tutorials/go-file-path-independent-of-operating-system
file, err := os.Open(dir.Name() + string(filepath.Separator) + fileInfo.Name())
checkerror(err)
defer file.Close()
// prepare the tar header
header := new(tar.Header)
header.Name = file.Name()
header.Size = fileInfo.Size()
header.Mode = int64(fileInfo.Mode())
header.ModTime = fileInfo.ModTime()
err = tarfileWriter.WriteHeader(header)
checkerror(err)
_, err = io.Copy(tarfileWriter, file)
checkerror(err)
}
}
Sample tests :
> ./gotar testdir.tar.gz ./testdir1
> ./gotar testdir.tar ./testdir1
ls -la testdir
-rw-r--r-- 1 root root 3072 Dec 16 22:54 testdir.tar
-rw-r--r-- 1 root root 148 Dec 16 22:54 testdir.tar.gz
That's it. Creating tar balls and compressed them with gzip is simple in Golang. Hope this tutorial can be useful to you.
See also : Golang : untar or extract tar ball archive example
By Adam Ng
IF you gain some knowledge or the information here solved your programming problem. Please consider donating to the less fortunate or some charities that you like. Apart from donation, planting trees, volunteering or reducing your carbon footprint will be great too.
Advertisement
Tutorials
+20.4k Golang : Convert date string to variants of time.Time type examples
+19.7k Golang : Reset or rewind io.Reader or io.Writer
+12k Golang : Print UTF-8 fonts on image example
+15.9k Golang : Loop each day of the current month example
+9.6k Golang : Resumable upload to Google Drive(RESTful) example
+5.9k Golang : Convert Chinese UTF8 characters to Pin Yin
+10.2k Swift : Convert (cast) String to Integer
+4.9k Golang : Convert lines of string into list for delete and insert operation
+22.2k Golang : How to read JPG(JPEG), GIF and PNG files ?
+8.7k Golang : Go as a script or running go with shebang/hashbang style
+10.1k Fix ERROR 1045 (28000): Access denied for user 'root'@'ip-address' (using password: YES)
+10.7k Nginx : TLS 1.2 support