Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add StandardEncryption desc & Add Example Encrypt Multiple Files #12

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
47 changes: 46 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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
}
```