Skip to content

Commit

Permalink
try debug compress
Browse files Browse the repository at this point in the history
  • Loading branch information
NhanPT committed Jan 8, 2023
1 parent f0c66e0 commit 554bbd6
Show file tree
Hide file tree
Showing 2 changed files with 44 additions and 52 deletions.
6 changes: 2 additions & 4 deletions src/utils/job.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
90 changes: 42 additions & 48 deletions src/utils/zip.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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
}

0 comments on commit 554bbd6

Please sign in to comment.