Home > Backend Development > Golang > How to Unzip Password-Protected ZIP Files in Go 1.2?

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

Mary-Kate Olsen
Release: 2024-10-30 09:09:03
Original
609 people have browsed it

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

Unzipping Password-Protected ZIP Files in Go 1.2

The archive/zip package in Go 1.2 provides basic functionality for handling ZIP archives but lacks support for password protection. To unzip a ZIP file protected by a password, one can utilize the os/exec package to invoke an external tool such as 7zip.

To do so, follow these steps:

  1. Create a Password-Protected ZIP File: Create a sample ZIP file using the 7zip tool, specifying the password and encryption algorithm:
7za a sample.zip name.txt -p"your_password" -mem=AES256
Copy after login
  1. Extract the ZIP File with Password: Use the following code to extract the password-protected ZIP file:
<code class="go">import (
    "fmt"
    "os/exec"
)

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

In this code snippet:

  • zip_path is the path to the password-protected ZIP file.
  • extract_path is the path to the directory where the extracted files will be saved.
  • zip_password is the password for the ZIP file.
  1. Example Program: Use the following complete example program to demonstrate the process:
<code class="go">// Shows how to extract an passsword encrypted zip file using 7zip.
// By Larry Battle <https://github.com/LarryBattle>
// Answer to http://stackoverflow.com/questions/20330210/golang-1-2-unzip-password-protected-zip-file
// 7-zip.chm - http://sevenzip.sourceforge.jp/chm/cmdline/switches/index.htm
// Effective Golang - http://golang.org/doc/effective_go.html
package main

import (
    "fmt"
    "os"
    "os/exec"
    "path/filepath"
    "strings"
)

// ...

func main() {
    // ...
    extractZipWithPassword()
    // ...
}</code>
Copy after login
  1. Run the Program: Compile and run the example program:
go run main.go
Copy after login

The program will extract the password-protected ZIP file to the specified directory.

The above is the detailed content of How to Unzip Password-Protected ZIP Files in Go 1.2?. 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