package main
import (
"fmt"
"log"
"os"
"github.com/dustin/go-humanize"
"github.com/mholt/archiver"
)
func main() {
// List of Files to Zip
files := []string{"warpeace.txt"}
output := "warpeace.zip"
output2 := "warpeace.txt"
if err := archiver.Archive(files, output); err != nil {
log.Fatal(err)
}
if err := archiver.Unarchive(output, "test"); err != nil {
log.Fatal(err)
}
fmt.Println("Source File Size:", humanize.Bytes(getFileSize("warpeace.txt")))
fmt.Println("Compressed File Size:", humanize.Bytes(getFileSize(output)))
fmt.Println("Extracted File Size:", humanize.Bytes(getFileSize("test/"+output2)))
}
func getFileSize(filename string) uint64 {
file, err := os.Open(filename)
if err != nil {
// handle the error here
return 0
}
defer file.Close()
// get the file size
stat, err := file.Stat()
if err != nil {
return 0
}
return uint64(stat.Size())
}