How to Decrypt a Password-Protected ZIP File in Go
ZIP archive files have been utilized by businesses to compress and store confidential information for many years, and the encryption capabilities of the format make it a secure and relatively easy option for sharing with internal and external partners alike. However, if you have multiple ZIP files, it can become an inconvenience to constantly reference the password to unlock the contents. By using the following API in Go, you will be able to instantly decrypt and remove password protection from an encrypted ZIP file, improving accessibility for future use.
To call the function, all you need to do is input the encrypted file and password into the below example 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/decrypt"
method := "POST"payload := &bytes.Buffer{}
writer := multipart.NewWriter(payload)
file, errFile1 := os.Open("/path/to/file")
defer file.Close()
part1,
errFile1 := writer.CreateFormFile("inputFile",filepath.Base("/path/to/file"))
_, errFile1 = io.Copy(part1, file)
if errFile1 != nil {
fmt.Println(errFile1)
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("zipPassword", "<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 just like that, the process is complete!