Home  >  Article  >  Backend Development  >  How to Extract Password-Protected ZIP Files in Golang 1.2 without Native Support?

How to Extract Password-Protected ZIP Files in Golang 1.2 without Native Support?

Mary-Kate Olsen
Mary-Kate OlsenOriginal
2024-10-31 01:46:29721browse

How to Extract Password-Protected ZIP Files in Golang 1.2 without Native Support?

Extracting Password-Protected ZIP Files in Golang 1.2 without Native Support

While the archive/zip package in Go 1.2 provides basic ZIP functionality, it lacks mechanisms for handling password-protected archives. To fill this gap, the recommended approach is to leverage the os/exec package and utilize an external tool like 7-zip to perform the extraction.

Using 7-zip to Extract Password-Protected ZIP Files

7-zip offers robust command-line capabilities for manipulating archives, including password-protected ZIP files. Here's how you can achieve this in Go using os/exec:

<code class="go">import (
    "os/exec"
    "fmt"
)

func extractZipWith7zip(zipPath, extractPath, password string) {
    cmdString := fmt.Sprintf("7za e %s -o%s -p%s", zipPath, extractPath, password)
    cmd := exec.Command("7za", strings.Fields(cmdString)...)
    err := cmd.Run()
    if err != nil {
        panic(err)
    }
}</code>

Example Program

The following Go program demonstrates the entire process of creating a password-protected ZIP file, extracting it using 7-zip, and verifying the extracted content:

<code class="go">package main

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

var (
    originalText = "Sample file created"
    txtFilename  = "name.txt"
    zipFilename  = "sample.zip"
    zipPassword  = "42"

    dirPath = "./test"
    srcPath = filepath.Join(dirPath, "src")
    extractPath = filepath.Join(dirPath, "extracted")
    txtPath = filepath.Join(srcPath, txtFilename)
    zipPath = filepath.Join(srcPath, zipFilename)
)

func main() {
    fmt.Println("# Setup")
    setupTestDirectory()
    createSampleFile()
    createZipWithPassword()
    fmt.Println("# Extraction of password-protected ZIP...")
    extractZipWithPassword()
    verifyExtractedFile()
    fmt.Println("Done.")
}

func setupTestDirectory() {
    os.RemoveAll(dirPath)
    os.MkdirAll(srcPath, os.ModePerm)
    os.MkdirAll(extractPath, os.ModePerm)
}

func createSampleFile() {
    file, err := os.Create(txtPath)
    if err != nil {
        panic(err)
    }
    defer file.Close()
    _, err = file.WriteString(originalText)
    if err != nil {
        panic(err)
    }
}

func createZipWithPassword() {
    cmdString := fmt.Sprintf("7za a %s %s -p%s", zipPath, txtPath, zipPassword)
    cmd := exec.Command("7za", strings.Fields(cmdString)...)
    err := cmd.Run()
    if err != nil {
        panic(err)
    }
}

func extractZipWithPassword() {
    cmdString := fmt.Sprintf("7za e %s -o%s -p%s", zipPath, extractPath, zipPassword)
    cmd := exec.Command("7za", strings.Fields(cmdString)...)
    err := cmd.Run()
    if err != nil {
        panic(err)
    }
}

func verifyExtractedFile() {
    extractedPath := filepath.Join(extractPath, txtFilename)
    content, err := os.ReadFile(extractedPath)
    if err != nil {
        panic(err)
    }
    if string(content) != originalText {
        panic("Extracted content mismatch")
    }
}</code>

Note: Make sure 7-zip is installed on your system and included in your system's path for this approach to work.

The above is the detailed content of How to Extract Password-Protected ZIP Files in Golang 1.2 without Native Support?. For more information, please follow other related articles on the PHP Chinese website!

Statement:
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