创建带密码的zip压缩包
package mainimport ("io""log""os""path/filepath""github.com/alexmullins/zip"
)func init() {log.SetFlags(log.Lshortfile | log.LstdFlags)
}func main() {fzip, err := os.Create("./main.zip")if err != nil {log.Fatalln(err)}defer fzip.Close()zipw := zip.NewWriter(fzip)defer zipw.Close()header := &zip.FileHeader{Name: filepath.Base("main.go"),Method: zip.Deflate,Flags: 0x800, // 设置文件名称编码为UTF-8,避免在Windows上解压后文件名出现乱码}header.SetPassword("password")w, err := zipw.CreateHeader(header)if err != nil {log.Fatalln(err)}// 打开要添加到压缩包中的原文件f, err := os.Open("./main.go")if err != nil {log.Fatalln(err)}// 从文件中读取数据到压缩包中if _, err = io.Copy(w, f); err != nil {log.Fatalln(err)}// 写入到存储中if err := zipw.Flush(); err != nil {log.Fatalln(err)}
}