How to Compress Folder in Golang - Backup Files Software From Scratch Written in Golang Tutorial #3

Опубликовано: 23 Ноябрь 2024
на канале: Maharlikans Code
639
17

In this tutorial series #3, we have learned how to compress the entire folder and its files using the Golang programming language and it's very easy to do this with Go. I use the tar.gz or gzip file compression in Go.

Installed Packages:
https://github.com/itrepablik/kopy
https://github.com/itrepablik/itrlog
https://github.com/itrepablik/sakto
https://github.com/fatih/color
https://github.com/common-nighthawk/g...
https://github.com/spf13/cobra

If you go with extra mile for buying me a cup of coffee, I appreciate it guys: https://ko-fi.com/maharlikanscode

#MaharlikansCode
#CompressFolderInGolang
#Golang
#LifeAsSoftwareDeveloper
#Maharlikans
#FilipinoSoftwareDeveloper
#GoProgramming

Source Codes:
main.go:
package main

import (
"gokopy/cmd"
)

func main() {
cmd.Execute()
}

cmd/root.go:
package cmd

import (
"fmt"
"gokopy/config"
"os"

"github.com/common-nighthawk/go-figure"
"github.com/spf13/cobra"
)

var logDTFormat string = "Jan 02 2006 03:04:05 PM"

var rootCmd = &cobra.Command{
Use: config.AppName,
Short: config.AppDesc,
Long: config.AppDesc,
Version: config.AppVersion,
// Run: func(cmd *cobra.Command, args []string) {
// // Do Stuff Here
// fmt.Println("Hello Cobra CLI application")
// },
}

// Execute ...
func Execute() {
if err := rootCmd.Execute(); err != nil {
fmt.Println(err)
os.Exit(1)
}
}

func init() {
// Display the app ASCII logo
myFigure := figure.NewFigure(config.AppDisplayName, "", true)
myFigure.Print()
}

cmd/comdir.go
package cmd

import (
"path"
"path/filepath"
"time"

"github.com/fatih/color"
"github.com/itrepablik/itrlog"
"github.com/itrepablik/kopy"
"github.com/itrepablik/sakto"

"github.com/spf13/cobra"
)

var comdirCmd = &cobra.Command{
Use: "comdir",
Short: "Compress the directory or a folder.",
Long: `comdir this is a long description for this comdir command.`,
Args: cobra.MinimumNArgs(2),
Run: func(cmd *cobra.Command, args []string) {
// To make directory path separator a universal, in Linux "/" and in Windows "\" to auto change
// depends on the user's OS using the filepath.FromSlash organic Go's library.
src := filepath.FromSlash(args[0])
dst := filepath.FromSlash(args[1])

// Compose the zip filename
fnWOext := kopy.FileNameWOExt(filepath.Base(args[0])) // Returns a filename without an extension.
zipDir := fnWOext + kopy.ComFileFormat

// To make directory path separator a universal, in Linux "/" and in Windows "\" to auto change
// depends on the user's OS using the filepath.FromSlash organic Go's library.
zipDest := filepath.FromSlash(path.Join(args[1], zipDir))

// Check if same filename is exist or not
if sakto.IsFileExist(zipDest) {
color.Red("compressed file already existed: " + zipDest)
return
}

msg := `Start compressing the directory or a folder:`
color.Blue(msg + " " + src)
itrlog.Infow(msg, "src", src, "dst", dst, "log_time", time.Now().Format(logDTFormat))

// Now, start compressing the entire directory or a folder using tar.gz compression format
comdirIgnore := []string{".jpg", ".png", "folder_name"}

if err := kopy.CompressDIR(src, dst, zipDest, true, comdirIgnore); err != nil {
color.Red(err.Error())
itrlog.Errorw("error", "err", err, "log_time", time.Now().Format(logDTFormat))
return
}

msg = `Done compressing the directory or a folder:`
color.Green(msg + " " + src)
itrlog.Infow(msg, "dst", zipDest, "log_time", time.Now().Format(logDTFormat))
},
}

func init() {
rootCmd.AddCommand(comdirCmd)
}

config/settings.go
package config

// AppName is the display name for the app
const AppName = "gokopy"

// AppDisplayName ...
const AppDisplayName = "Gokopy"

// AppDesc ...
const AppDesc = "A lightweight automated backup files software."

// AppVersion ...
const AppVersion = "1.0.0"