golang zip 压缩,解压(含目录文件)

xiaofengshuyu ·
这个代码旧版本Golang有漏洞!!而且zip内有文件夹会报错 https://snyk.io/research/zip-slip-vulnerability#go http://www.cnnvd.org.cn/web/xxk/ldxqById.tag?CNNVD=CNNVD-201807-1826
#5
更多评论
**改进写法** //zip解压 ```go func DeCompress_zip(zipFile, dest_path string) error { reader, err := zip.OpenReader(zipFile) if err != nil { return err } defer reader.Close() DeCompress_to_dest := func(f *zip.File) error { rc, err := f.Open() if err != nil { return err } defer rc.Close() filename := dest_path + f.Name if err = os.MkdirAll(filepath.Dir(filename), 0755); err != nil { return err } w, err := os.Create(filename) if err != nil { return err } defer w.Close() if _, err = io.Copy(w, rc); err != nil { return err } return nil } for _, file := range reader.File { err := DeCompress_to_dest(file) if err != nil { return err } } return nil } ```
#1
重新提交一下 ```go func DeCompress_zip(zipFile, dest_path string) error { reader, err := zip.OpenReader(zipFile) if err != nil { return err } defer reader.Close() DeCompress_to_dest := func(f *zip.File) error { rc, err := f.Open() if err != nil { return err } defer rc.Close() filename := dest_path + f.Name if err = os.MkdirAll(filepath.Dir(filename), 0755); err != nil { return err } w, err := os.Create(filename) if err != nil { return err } defer w.Close() _, err = io.Copy(w, rc) return err } for _, file := range reader.File { err := DeCompress_to_dest(file) if err != nil { return err } } return nil } ```
#2