Search⌘ K
AI Features

Archiving Files

Understand how to add file archiving capabilities to Go applications by compressing files with gzip and maintaining relative directory trees. Learn to use Go’s standard libraries to create cross-platform, compressed backups before file deletion.

Before deleting files that are consuming too much space, we might want to back them up in a compressed form so we can keep them around in case we need them later. Let’s add an archiving feature to the walk tool to enable this feature.

Importing the compress/gzip package

To add this new feature, we’ll use these standard library packages:

  • compress/gzip to compress data using the gzip format.
  • io, which provides functions to help with Input/Output operations such as copying data.

The package io is already included in the import list. We add the compress/gzip package to the import list in the file actions.go:

Go (1.6.2)
import (
"compress/gzip"
"fmt"
"io"
"log"
"os"
"path/filepath"
)
Importing the gzip package

Adding the archiveFile() function

We define the new action function archiveFile(destDir, root, ...