How to Unzip Password-Protected ZIP Files in Go 1.2 Using 7zip?

Patricia Arquette
Release: 2024-11-04 03:12:02
Original
973 people have browsed it

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>
Copy after login

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>
Copy after login

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.
Copy after login

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!

source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Latest Articles by Author
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!