diff --git a/go.mod b/go.mod index ef822f2..a47ceaf 100644 --- a/go.mod +++ b/go.mod @@ -7,7 +7,6 @@ require ( github.com/asaskevich/govalidator v0.0.0-20200108200545-475eaeb16496 github.com/eiannone/keyboard v0.0.0-20200508000154-caf4b762e807 github.com/glendc/go-external-ip v0.1.0 - github.com/jhoonb/archivex v0.0.0-20180718040744-0488e4ce1681 github.com/manifoldco/promptui v0.7.0 github.com/mattn/go-runewidth v0.0.9 // indirect github.com/skip2/go-qrcode v0.0.0-20191027152451-9434209cb086 diff --git a/go.sum b/go.sum index cc48d01..4058a78 100644 --- a/go.sum +++ b/go.sum @@ -1045,8 +1045,6 @@ github.com/ianlancetaylor/demangle v0.0.0-20181102032728-5e5cf60278f6/go.mod h1: github.com/ianlancetaylor/demangle v0.0.0-20200824232613-28f6c0f3b639/go.mod h1:aSSvb/t6k1mPoxDqO4vJh6VOCGPwU4O0C2/Eqndh1Sc= github.com/inconshreveable/mousetrap v1.1.0 h1:wN+x4NVGpMsO7ErUn/mUI3vEoE6Jt13X2s0bqwp9tc8= github.com/inconshreveable/mousetrap v1.1.0/go.mod h1:vpF70FUmC8bwa3OWnCshd2FqLfsEA9PFc4w1p2J65bw= -github.com/jhoonb/archivex v0.0.0-20180718040744-0488e4ce1681 h1:EiEjLram6Y0WXygV4WyzKmTr3XaR4CD3tvjdTrsk3cU= -github.com/jhoonb/archivex v0.0.0-20180718040744-0488e4ce1681/go.mod h1:GN1Mg/uXQ6qwXA0HypnUO3xlcQJS9/y68EsHNeuuRa4= github.com/jpillora/backoff v1.0.0/go.mod h1:J/6gKK9jxlEcS3zixgDgUAsiuZ7yrSoa/FX5e0EB2j4= github.com/json-iterator/go v1.1.6/go.mod h1:+SdeFBvtyEkXs7REEP0seUULqWtbJapLOCVDaaPEHmU= github.com/json-iterator/go v1.1.9/go.mod h1:KdQUCv79m/52Kvf8AW2vK1V8akMuk1QjK/uOdHXbAo4= diff --git a/util/util.go b/util/util.go index 23ba1a9..6b582b6 100644 --- a/util/util.go +++ b/util/util.go @@ -1,6 +1,7 @@ package util import ( + "archive/zip" "crypto/rand" "encoding/base64" "errors" @@ -14,8 +15,6 @@ import ( "strconv" "strings" "time" - - "github.com/jhoonb/archivex" ) // Expand tilde in paths @@ -33,44 +32,105 @@ func Expand(input string) string { return input } +// add folder to the zip file +func addFolderToZip(zipWriter *zip.Writer, source, target string) error { + + //explore the folder and add all to the zip + return filepath.Walk(source, func(path string, info os.FileInfo, err error) error { + if err != nil { + return err + } + + header, err := zip.FileInfoHeader(info) + if err != nil { + return err + } + header.Name = path + + if info.IsDir() { + header.Name += "/" + } else { + header.Method = zip.Deflate + } + + writer, err := zipWriter.CreateHeader(header) + if err != nil { + return err + } + + if info.IsDir() { + return nil + } + + file, err := os.Open(path) + if err != nil { + return err + } + defer file.Close() + + _, err = io.Copy(writer, file) + return err + }) +} + +// add files to the zip file +func addFileToZip(zipWriter *zip.Writer, fileToAdd string) error { + f1, err := os.Open(fileToAdd) + if err != nil { + return err + } + defer f1.Close() + + w1, err := zipWriter.Create(filepath.Base(fileToAdd)) + if err != nil { + panic(err) + } + if _, err := io.Copy(w1, f1); err != nil { + panic(err) + } + + return nil +} + // ZipFiles and return the resulting zip's filename func ZipFiles(files []string) (string, error) { - zip := new(archivex.ZipFile) - tmpfile, err := ioutil.TempFile("", "qrcp") + //create temporary file + tmpfile, err := os.CreateTemp("", "qrcp") if err != nil { return "", err } + tempFileName := tmpfile.Name() + ".zip" tmpfile.Close() - if err := os.Rename(tmpfile.Name(), tmpfile.Name()+".zip"); err != nil { + if err := os.Rename(tmpfile.Name(), tempFileName); err != nil { return "", err } - if err := zip.Create(tmpfile.Name() + ".zip"); err != nil { + + //create zip file + zipFile, err := os.Create(tempFileName) + if err != nil { return "", err } + defer zipFile.Close() + + //add files and folder in the zip + zipWriter := zip.NewWriter(zipFile) + defer zipWriter.Close() for _, filename := range files { fileinfo, err := os.Stat(filename) if err != nil { return "", err } if fileinfo.IsDir() { - if err := zip.AddAll(filename, true); err != nil { + if err := addFolderToZip(zipWriter, filename, tempFileName); err != nil { return "", err } } else { - file, err := os.Open(filename) - if err != nil { - return "", err - } - defer file.Close() - if err := zip.Add(filename, file, fileinfo); err != nil { + if err := addFileToZip(zipWriter, filename); err != nil { return "", err } } } - if err := zip.Close(); err != nil { - return "", nil - } - return zip.Name, nil + return tempFileName, nil } // GetRandomURLPath returns a random string of 4 alphanumeric characters