Home > Article > Backend Development > How to Unzip Password-Protected ZIP Files in Go 1.2 Using 7zip?
Unzipping Password-Protected ZIP Files in Go 1.2
The os/exec package offers a convenient way to interact with external commands. For unzipping encrypted ZIP files using 7zip in Go 1.2, consider the following:
The archive/zip package provides basic ZIP manipulation functionality. Instead of using it to extract password-protected ZIP files, you can employ 7zip through os/exec.
Here's an illustration:
<code class="go">import ( "fmt" "os/exec" "strings" ) func extractZipWithPassword() { fmt.Printf("Unzipping `%s` to directory `%s`\n", zipPath, extractPath) commandString := fmt.Sprintf(`7za e %s -o%s -p"%s" -aoa`, zipPath, extractPath, zipPassword) commandSlice := strings.Fields(commandString) fmt.Println(commandString) c := exec.Command(commandSlice[0], commandSlice[1:]...) e := c.Run() checkError(e) }</code>
Example Program with 7zip
<code class="go">package main import ( "fmt" "os" "os/exec" "path/filepath" "strings" ) var ( // Your variables and paths... ) func main() { fmt.Println("# Setup") //... fmt.Println("# Answer to question...") extractZipWithPassword() //... fmt.Println("Done.") }</code>
Output:
# Setup # Answer to question... Unzipping `test/src/sample.zip` to directory `test/extracted` 7za e test/src/sample.zip -otest/extracted -p"42" -aoa Reading test/extracted/name.txt Done.
This approach allows you to unzip password-protected ZIP files using 7zip in Go 1.2.
The above is the detailed content of How to Unzip Password-Protected ZIP Files in Go 1.2 Using 7zip?. For more information, please follow other related articles on the PHP Chinese website!