From 554bbd68b4ccc14888c3456eb6b430de8aa14f12 Mon Sep 17 00:00:00 2001 From: NhanPT Date: Sun, 8 Jan 2023 16:06:55 +0700 Subject: [PATCH] try debug compress --- src/utils/job.go | 6 ++-- src/utils/zip.go | 90 ++++++++++++++++++++++-------------------------- 2 files changed, 44 insertions(+), 52 deletions(-) diff --git a/src/utils/job.go b/src/utils/job.go index 75458bf..e9ab9c2 100644 --- a/src/utils/job.go +++ b/src/utils/job.go @@ -32,13 +32,11 @@ func (runner *JobRunner) Run() error { uploadFile = stats.Name() } - fmt.Println("UseZip", runner.Job.UseZip) - fmt.Println("Contains zip", strings.Contains(uploadPath, ".zip")) - fmt.Println("stats.IsDir()", stats.IsDir()) if (runner.Job.UseZip && !strings.Contains(uploadPath, ".zip")) || stats.IsDir() { uploadFile = createZipName(runner.Job.Name) uploadPath = createTempPath(uploadFile) - err = ZipFolder(runner.Job.Path, uploadFile) + fmt.Println("Creating zip file:", uploadFile) + err = CompressZip(runner.Job.Path, uploadFile) if err != nil { fmt.Println(err.Error()) return err diff --git a/src/utils/zip.go b/src/utils/zip.go index ae84087..310fe8f 100644 --- a/src/utils/zip.go +++ b/src/utils/zip.go @@ -10,64 +10,59 @@ import ( "strings" ) -func ZipFolder(inputPath string, outputPath string) error { - return CompressZip(inputPath, outputPath) +//func ZipFolder(inputPath string, outputPath string) error { +// file, err := os.Create(outputPath) +// if err != nil { +// return err +// } +// defer file.Close() - // file, err := os.Create(outputPath) - // if err != nil { - // return err - // } - // defer file.Close() +// w := zip.NewWriter(file) +// defer w.Close() - // w := zip.NewWriter(file) - // defer w.Close() +// walker := func(path string, info os.FileInfo, err error) error { +// if err != nil { +// return err +// } +// if info.IsDir() { +// return nil +// } +// file, err := os.Open(path) +// if err != nil { +// return err +// } +// defer file.Close() - // walker := func(path string, info os.FileInfo, err error) error { - // if err != nil { - // return err - // } - // if info.IsDir() { - // return nil - // } - // file, err := os.Open(path) - // if err != nil { - // return err - // } - // defer file.Close() +// // Ensure that `path` is not absolute; it should not start with "/". +// // This snippet happens to work because I don't use +// // absolute paths, but ensure your real-world code +// // transforms path into a zip-root relative path. +// f, err := w.Create(path) +// if err != nil { +// return err +// } - // // Ensure that `path` is not absolute; it should not start with "/". - // // This snippet happens to work because I don't use - // // absolute paths, but ensure your real-world code - // // transforms path into a zip-root relative path. - // f, err := w.Create(path) - // if err != nil { - // return err - // } +// _, err = io.Copy(f, file) +// if err != nil { +// return err +// } - // _, err = io.Copy(f, file) - // if err != nil { - // return err - // } - - // return nil - // } - // err = filepath.Walk(inputPath, walker) - // if err != nil { - // return err - // } - // return nil -} +// return nil +// } +// err = filepath.Walk(inputPath, walker) +// if err != nil { +// return err +// } +// return nil +//} func CompressZip(inputPath string, outputPath string) error { archive, err := os.Create(outputPath) if err != nil { return err } - defer archive.Close() w := zip.NewWriter(archive) - defer w.Close() - stat, err := os.Stat(inputPath) if err != nil { return err @@ -112,20 +107,19 @@ func CompressZip(inputPath string, outputPath string) error { arPath = strings.TrimPrefix(arPath, "/") f, err := w.Create(arPath) if err != nil { - fmt.Println(err.Error()) return err } in, err := os.Open(path) if err != nil { - fmt.Println(err.Error()) return err } _, err = io.Copy(f, in) if err != nil { - fmt.Println(err.Error()) return err } } + w.Close() + archive.Close() return nil }