From 5e6c09205677e0b9bfaf9a62cd5b59325e6ff4b5 Mon Sep 17 00:00:00 2001 From: Ding Chaofa Date: Mon, 12 Dec 2022 16:21:30 +0800 Subject: [PATCH 1/2] Add StandardEncryption desc --- README.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/README.md b/README.md index f4fdcde..5fbb739 100644 --- a/README.md +++ b/README.md @@ -13,7 +13,7 @@ zip.AES256Encryption ## Warning -Zip Standard Encryption isn't actually secure. +Zip Standard Encryption isn't actually secure(https://github.com/alexmullins/zip/issues/17). Unless you have to work with it, please use AES encryption instead. ## Example Encrypt Zip From 34ed2b0b0bc0dcdda5124ba2b76aa23b5ddf31c6 Mon Sep 17 00:00:00 2001 From: Ding Chaofa Date: Mon, 12 Dec 2022 16:21:50 +0800 Subject: [PATCH 2/2] ADD Example Encrypt Multiple Files --- README.md | 45 +++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 45 insertions(+) diff --git a/README.md b/README.md index 5fbb739..196b525 100644 --- a/README.md +++ b/README.md @@ -90,3 +90,48 @@ func main() { } } ``` + + +## Example Encrypt Multiple Files + +``` +// ZipFilesWithEncrypt encrypts several files at once. Absolve paths are filenames and files.  +func ZipFilesWithEncrypt(fileName string, files []string, password string) error { + zipFile, err := os.OpenFile(fileName, os.O_WRONLY|os.O_CREATE|os.O_TRUNC, 0644) + if err != nil { + return err + } + defer zipFile.Close() + + zipw := zip.NewWriter(zipFile) + for _, f := range files { + if err := appendFiles(f, password, zipw); err != nil { + return err + } + } + if err = zipw.Close(); err != nil { + return err + } + return nil +} + +func appendFiles(filename, password string, zipw *zip.Writer) error { + file, err := os.Open(filename) + if err != nil { + return err + } + defer file.Close() + + name := path.Base(filename) + + wr, err := zipw.Encrypt(name, password, zip.StandardEncryption) + if err != nil { + return err + } + + if _, err := io.Copy(wr, file); err != nil { + return err + } + return nil +} +``` \ No newline at end of file