Unzipping Password-Protected ZIP Files in Go 1.2
In Go 1.2, the archive/zip package provides basic zip functionality, but lacks support for handling password-protected files. To unzip such files, it is recommended to utilize the os/exec package in conjunction with external tools like 7zip.
Using 7zip to Extract Password-Protected ZIP Files
The following example demonstrates how to extract a password-protected ZIP file using 7zip:
<code class="go">func extractZipWithPassword() { fmt.Printf("Unzipping `%s` to directory `%s`\n", zip_path, extract_path) commandString := fmt.Sprintf(`7za e %s -o%s -p"%s" -aoa`, zip_path, extract_path, zip_password) commandSlice := strings.Fields(commandString) fmt.Println(commandString) c := exec.Command(commandSlice[0], commandSlice[1:]...) e := c.Run() checkError(e) }</code>
In this example, we construct a command string using the 7za executable to extract the ZIP file. We specify the file path, extraction directory, password, and additional options to overwrite existing files and update archive timestamps (-aoa). Then, we execute the command using the exec.Command function and check for any errors.
Additional Resources
Note:
While this approach relies on an external tool, it provides a straightforward solution for handling password-protected ZIP files in Go.
The above is the detailed content of How to Unzip Password-Protected ZIP Files in Go?. For more information, please follow other related articles on the PHP Chinese website!