fix: zip解压内含目录文件输出地址找不到的异常

This commit is contained in:
TsMask
2024-07-24 17:24:55 +08:00
parent e79ea30e52
commit e43ac65f8c

View File

@@ -32,20 +32,23 @@ func UnZip(zipFilePath, dirPath string) error {
defer rc.Close()
// 创建解压后的文件
path := filepath.Join(dirPath, f.Name)
path := filepath.ToSlash(filepath.Join(dirPath, f.Name))
if f.FileInfo().IsDir() {
// 如果是目录,创建目录
os.MkdirAll(path, f.Mode())
if err := os.MkdirAll(path, 0775); err != nil {
return err
}
} else {
// 如果是文件,创建文件并写入内容
file, err := os.OpenFile(path, os.O_WRONLY|os.O_CREATE|os.O_TRUNC, f.Mode())
if err = os.MkdirAll(filepath.Dir(path), 0775); err != nil {
return err
}
out, err := os.Create(path)
if err != nil {
return err
}
defer file.Close()
defer out.Close()
_, err = io.Copy(file, rc)
if err != nil {
if _, err = io.Copy(out, rc); err != nil {
return err
}
}