Created
May 22, 2020 09:13
-
-
Save phanirithvij/c1dbc4a304680bffc23010d79d2217e7 to your computer and use it in GitHub Desktop.
Gloang zip folder with progress
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package main | |
import ( | |
"archive/zip" | |
"errors" | |
"io" | |
"log" | |
"os" | |
"path/filepath" | |
"github.com/schollz/progressbar" | |
) | |
// ZipFiles compresses one or many files into a single zip archive file. | |
// Param 1: filename is the output zip file's name. | |
// Param 2: files is a list of files to add to the zip. | |
func ZipFiles(filename string, folder string) error { | |
newZipFile, err := os.Create(filename) | |
if err != nil { | |
return err | |
} | |
defer newZipFile.Close() | |
zipWriter := zip.NewWriter(newZipFile) | |
defer zipWriter.Close() | |
currDir, err := os.Getwd() | |
log.Println(currDir, filepath.Base(folder)) | |
// Add files to zip | |
if info, _ := os.Stat(folder); info.IsDir() { | |
localFiles := []string{} | |
err = filepath.Walk(folder, func(path string, info os.FileInfo, err error) error { | |
// if a file | |
if fileinfo, err := os.Stat(path); fileinfo.Mode().IsRegular() && err == nil { | |
localFiles = append(localFiles, path) | |
// log.Println("Added", path) | |
} | |
return nil | |
}) | |
count := int64(len(localFiles)) | |
bar := progressbar.Default(count) | |
log.Println("Number of files", count) | |
// Important | |
// or it will become highly nested | |
upOne, err := filepath.Abs(filepath.Join(folder, "..")) | |
os.Chdir(upOne) | |
if err != nil { | |
panic(err) | |
} | |
for _, loc := range localFiles { | |
bar.Add(1) | |
// log.Println("DAMNIT") | |
relpath, err := filepath.Rel(upOne, loc) | |
// log.Println(relpath) | |
if err != nil { | |
panic(err) | |
} | |
if err = addFileToZip(zipWriter, filepath.Join(relpath)); err != nil { | |
return err | |
} | |
} | |
os.Chdir(currDir) | |
return nil | |
} | |
return errors.New("not a directory") | |
} | |
// addFileToZip Adds a file to the zip | |
func addFileToZip(zipWriter *zip.Writer, filename string) error { | |
// log.Println(filename) | |
fileToZip, err := os.Open(filename) | |
if err != nil { | |
return err | |
} | |
defer fileToZip.Close() | |
// Get the file information | |
info, err := fileToZip.Stat() | |
if err != nil { | |
return err | |
} | |
header, err := zip.FileInfoHeader(info) | |
if err != nil { | |
return err | |
} | |
// Using FileInfoHeader() above only uses the basename of the file. If we want | |
// to preserve the folder structure we can overwrite this with the full path. | |
header.Name = filename | |
// Change to deflate to gain better compression | |
// see http://golang.org/pkg/archive/zip/#pkg-constants | |
header.Method = zip.Deflate | |
writer, err := zipWriter.CreateHeader(header) | |
if err != nil { | |
return err | |
} | |
_, err = io.Copy(writer, fileToZip) | |
return err | |
} | |
// Usage go run zip.go dirname zippath | |
// eg: go run zip.go . currfolder.zip | |
func main() { | |
log.Println(os.Args) | |
if len(os.Args) < 3 { | |
panic("Wrong usage pass dir, name") | |
} | |
path, err := filepath.Abs(os.Args[1]) | |
if err != nil { | |
panic(err) | |
} | |
err = ZipFiles( | |
filepath.Join(os.Args[2]), | |
path) | |
if err != nil { | |
panic(err) | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
There is an issue the extracted zip has some different tree. Will fix it later