mirror of
https://github.com/duke-git/lancet.git
synced 2026-02-12 16:52:29 +08:00
fix: fix bug of Zip
This commit is contained in:
@@ -197,54 +197,58 @@ func IsZipFile(filepath string) bool {
|
|||||||
return bytes.Equal(buf, []byte("PK\x03\x04"))
|
return bytes.Equal(buf, []byte("PK\x03\x04"))
|
||||||
}
|
}
|
||||||
|
|
||||||
// Zip create zip file, fpath could be a single file or a directory.
|
// Zip create zip file, fpath should be a directory.
|
||||||
// Play: https://go.dev/play/p/j-3sWBp8ik_P
|
// Play: https://go.dev/play/p/j-3sWBp8ik_P
|
||||||
func Zip(fpath string, destPath string) error {
|
func Zip(fpath string, destPath string) error {
|
||||||
zipFile, err := os.Create(destPath)
|
outFile, err := os.Create(destPath)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
defer zipFile.Close()
|
defer outFile.Close()
|
||||||
|
|
||||||
archive := zip.NewWriter(zipFile)
|
w := zip.NewWriter(outFile)
|
||||||
defer archive.Close()
|
|
||||||
|
|
||||||
return addFileToArchive(fpath, archive)
|
err = addFileToArchive(w, fpath, "")
|
||||||
|
if err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
|
||||||
|
err = w.Close()
|
||||||
|
if err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
|
||||||
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
func addFileToArchive(fpath string, archive *zip.Writer) error {
|
func addFileToArchive(w *zip.Writer, basePath, baseInZip string) error {
|
||||||
err := filepath.Walk(fpath, func(path string, info os.FileInfo, err error) error {
|
files, err := os.ReadDir(basePath)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
|
|
||||||
header, err := zip.FileInfoHeader(info)
|
for _, file := range files {
|
||||||
if err != nil {
|
if !file.IsDir() {
|
||||||
return err
|
dat, err := os.ReadFile(basePath + file.Name())
|
||||||
}
|
|
||||||
|
|
||||||
header.Name = strings.TrimPrefix(path, filepath.Dir(fpath)+"/")
|
|
||||||
|
|
||||||
if info.IsDir() {
|
|
||||||
header.Name += "/"
|
|
||||||
} else {
|
|
||||||
header.Method = zip.Deflate
|
|
||||||
writer, err := archive.CreateHeader(header)
|
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
file, err := os.Open(path)
|
|
||||||
|
f, err := w.Create(baseInZip + file.Name())
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
defer file.Close()
|
_, err = f.Write(dat)
|
||||||
if _, err := io.Copy(writer, file); err != nil {
|
if err != nil {
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
|
} else if file.IsDir() {
|
||||||
|
newBase := basePath + file.Name() + "/"
|
||||||
|
addFiles(w, newBase, baseInZip+file.Name()+"/")
|
||||||
}
|
}
|
||||||
return nil
|
}
|
||||||
})
|
|
||||||
return err
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
// UnZip unzip the file and save it to destPath.
|
// UnZip unzip the file and save it to destPath.
|
||||||
|
|||||||
Reference in New Issue
Block a user