Create and Encrypt a ZIP File in Go
Electronically sharing large files can prove to be a huge pain; email attempts can get stuck in your outbox and may never make it to their destination. To save yourself this frustration, you can utilize the following API in Go to compress your files into a ZIP format and encrypt and password-protect your compressed file to ensure your information remains confidential.
All you’ll need for this process are the input files (up to 10), password for the new ZIP file, encryption algorithm (default is AES-256), and your API key; these parameters can be plugged into the following code:
package mainimport (
"fmt"
"bytes"
"mime/multipart"
"os"
"path/filepath"
"io"
"net/http"
"io/ioutil"
)func main() {url := "https://api.cloudmersive.com/convert/archive/zip/create/encrypted"
method := "POST"payload := &bytes.Buffer{}
writer := multipart.NewWriter(payload)
file, errFile1 := os.Open("/path/to/file")
defer file.Close()
part1,
errFile1 := writer.CreateFormFile("inputFile1",filepath.Base("/path/to/file"))
_, errFile1 = io.Copy(part1, file)
if errFile1 != nil {
fmt.Println(errFile1)
return
}
file, errFile2 := os.Open("/path/to/file")
defer file.Close()
part2,
errFile2 := writer.CreateFormFile("inputFile2",filepath.Base("/path/to/file"))
_, errFile2 = io.Copy(part2, file)
if errFile2 != nil {
fmt.Println(errFile2)
return
}
file, errFile3 := os.Open("/path/to/file")
defer file.Close()
part3,
errFile3 := writer.CreateFormFile("inputFile3",filepath.Base("/path/to/file"))
_, errFile3 = io.Copy(part3, file)
if errFile3 != nil {
fmt.Println(errFile3)
return
}
file, errFile4 := os.Open("/path/to/file")
defer file.Close()
part4,
errFile4 := writer.CreateFormFile("inputFile4",filepath.Base("/path/to/file"))
_, errFile4 = io.Copy(part4, file)
if errFile4 != nil {
fmt.Println(errFile4)
return
}
file, errFile5 := os.Open("/path/to/file")
defer file.Close()
part5,
errFile5 := writer.CreateFormFile("inputFile5",filepath.Base("/path/to/file"))
_, errFile5 = io.Copy(part5, file)
if errFile5 != nil {
fmt.Println(errFile5)
return
}
file, errFile6 := os.Open("/path/to/file")
defer file.Close()
part6,
errFile6 := writer.CreateFormFile("inputFile6",filepath.Base("/path/to/file"))
_, errFile6 = io.Copy(part6, file)
if errFile6 != nil {
fmt.Println(errFile6)
return
}
file, errFile7 := os.Open("/path/to/file")
defer file.Close()
part7,
errFile7 := writer.CreateFormFile("inputFile7",filepath.Base("/path/to/file"))
_, errFile7 = io.Copy(part7, file)
if errFile7 != nil {
fmt.Println(errFile7)
return
}
file, errFile8 := os.Open("/path/to/file")
defer file.Close()
part8,
errFile8 := writer.CreateFormFile("inputFile8",filepath.Base("/path/to/file"))
_, errFile8 = io.Copy(part8, file)
if errFile8 != nil {
fmt.Println(errFile8)
return
}
file, errFile9 := os.Open("/path/to/file")
defer file.Close()
part9,
errFile9 := writer.CreateFormFile("inputFile9",filepath.Base("/path/to/file"))
_, errFile9 = io.Copy(part9, file)
if errFile9 != nil {
fmt.Println(errFile9)
return
}
file, errFile10 := os.Open("/path/to/file")
defer file.Close()
part10,
errFile10 := writer.CreateFormFile("inputFile10",filepath.Base("/path/to/file"))
_, errFile10 = io.Copy(part10, file)
if errFile10 != nil {
fmt.Println(errFile10)
return
}
err := writer.Close()
if err != nil {
fmt.Println(err)
return
}client := &http.Client {
}
req, err := http.NewRequest(method, url, payload)if err != nil {
fmt.Println(err)
return
}
req.Header.Add("password", "<string>")
req.Header.Add("encryptionAlgorithm", "<string>")
req.Header.Add("Content-Type", "multipart/form-data")
req.Header.Add("Apikey", "YOUR-API-KEY-HERE")req.Header.Set("Content-Type", writer.FormDataContentType())
res, err := client.Do(req)
if err != nil {
fmt.Println(err)
return
}
defer res.Body.Close()body, err := ioutil.ReadAll(res.Body)
if err != nil {
fmt.Println(err)
return
}
fmt.Println(string(body))
}
And voila! Your new secure ZIP file will be ready in no time for easy electronic sharing.