Optimizing the CompressDir function

Now, the content of each file is no longer fully read into memory, but directly copied into a zip file through io. Copy(). Before the loop, we created the target file and reused the file handle in the loop, thereby reducing disk write operations. Finally, during export, we use the defer statement to release resources and ensure that these resources are properly closed before the program exits
pull/224/head
zengwei2000 2 years ago committed by GitHub
parent 3eaa2dd2bf
commit 71340f4650
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
  1. 42
      utils/fileutil/filetutil.go

@ -139,38 +139,42 @@ func CompressDir(dir string) error {
if err != nil {
return err
}
b := new(bytes.Buffer)
zw := zip.NewWriter(b)
outFileName := fmt.Sprintf("%s.zip", dir)
outFilePath := filepath.Join(dir, outFileName)
outFile, err := os.Create(outFilePath)
if err != nil {
return err
}
defer outFile.Close()
zw := zip.NewWriter(outFile)
defer zw.Close()
for _, f := range files {
fw, err := zw.Create(f.Name())
if err != nil {
return err
}
name := path.Join(dir, f.Name())
content, err := os.ReadFile(name)
contentPath := filepath.Join(dir, f.Name())
contentFile, err := os.Open(contentPath)
if err != nil {
return err
}
_, err = fw.Write(content)
defer contentFile.Close()
_, err = io.Copy(fw, contentFile)
if err != nil {
return err
}
err = os.Remove(name)
err = os.Remove(contentPath)
if err != nil {
return err
}
}
if err := zw.Close(); err != nil {
return err
}
filename := filepath.Join(dir, fmt.Sprintf("%s.zip", dir))
outFile, err := os.Create(filepath.Clean(filename))
if err != nil {
return err
}
_, err = b.WriteTo(outFile)
if err != nil {
return err
}
return outFile.Close()
return nil
}

Loading…
Cancel
Save