기본 지원 없이 Golang 1.2에서 비밀번호로 보호된 ZIP 파일을 추출하는 방법은 무엇입니까?
기본 지원 없이 Golang 1.2에서 비밀번호로 보호된 ZIP 파일 추출
Go 1.2의 archive/zip 패키지는 기본적인 ZIP 기능을 제공하지만, 비밀번호로 보호된 아카이브를 처리하는 메커니즘이 부족합니다. 이러한 공백을 메우기 위해 권장되는 접근 방식은 os/exec 패키지를 활용하고 7-zip과 같은 외부 도구를 활용하여 추출을 수행하는 것입니다.
7-zip을 사용하여 비밀번호로 보호된 ZIP 파일 추출
7-zip은 비밀번호로 보호된 ZIP 파일을 포함하여 아카이브를 조작하기 위한 강력한 명령줄 기능을 제공합니다. os/exec를 사용하여 Go에서 이를 달성할 수 있는 방법은 다음과 같습니다.
<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>
예제 프로그램
다음 Go 프로그램은 비밀번호로 보호된 파일을 생성하는 전체 프로세스를 보여줍니다. ZIP 파일, 7-zip을 사용하여 압축 풀기 및 추출된 내용 확인:
<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>
참고: 7-zip이 시스템에 설치되어 있고 시스템 경로에 포함되어 있는지 확인하세요. 이 작업 방식을 사용하세요.
위 내용은 기본 지원 없이 Golang 1.2에서 비밀번호로 보호된 ZIP 파일을 추출하는 방법은 무엇입니까?의 상세 내용입니다. 자세한 내용은 PHP 중국어 웹사이트의 기타 관련 기사를 참조하세요!

핫 AI 도구

Undresser.AI Undress
사실적인 누드 사진을 만들기 위한 AI 기반 앱

AI Clothes Remover
사진에서 옷을 제거하는 온라인 AI 도구입니다.

Undress AI Tool
무료로 이미지를 벗다

Clothoff.io
AI 옷 제거제

AI Hentai Generator
AI Hentai를 무료로 생성하십시오.

인기 기사

뜨거운 도구

메모장++7.3.1
사용하기 쉬운 무료 코드 편집기

SublimeText3 중국어 버전
중국어 버전, 사용하기 매우 쉽습니다.

스튜디오 13.0.1 보내기
강력한 PHP 통합 개발 환경

드림위버 CS6
시각적 웹 개발 도구

SublimeText3 Mac 버전
신 수준의 코드 편집 소프트웨어(SublimeText3)

뜨거운 주제











Go Language Pack 가져 오기 : 밑줄과 밑줄이없는 밑줄의 차이점은 무엇입니까?

Beego 프레임 워크에서 페이지간에 단기 정보 전송을 구현하는 방법은 무엇입니까?

MySQL 쿼리 결과 목록을 GO 언어로 사용자 정의 구조 슬라이스로 변환하는 방법은 무엇입니까?

GO에서 제네릭에 대한 사용자 정의 유형 제약 조건을 어떻게 정의 할 수 있습니까?

이동 중에 테스트를 위해 모의 개체와 스터브를 작성하려면 어떻게합니까?

추적 도구를 사용하여 GO 응용 프로그램의 실행 흐름을 이해하려면 어떻게해야합니까?
