Golang 1.2:解压缩密码保护的zip文件? [英] Golang 1.2: Unzip password protected zip file?
问题描述
查看最新版本(1.2)zip包 - 如何解压缩受密码保护的文件(使用7zip,AES-256编码)?我没有看到在那里/如何添加这些信息。一个简单的例子会很棒!
我会使用7zip解压缩使用 os / exec
包的受密码保护的zip文件。
Online 7位用户指南
理解7zip的最佳指南是7-zip.chm,它位于windows的zip文件中命令行。
以下代码不是最佳选择,但它会告诉您如何完成工作。
使用7zip提取密码保护zip的代码
pre $ func extractZipWithPassword(
fmt .Printf(将%s解压缩到目录`%s`\\\
,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)
}
示例程序
//显示如何使用7zip提取passsword加密zip文件。
// By Larry Battle< https://github.com/LarryBattle>
//回答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
//有效的Golang - http://golang.org/doc/effective_go.html
包裹主
导入(
fmt
os
os / exec
路径/文件路径
字符串
)
var(
txt_content =创建示例文件。
txt_filename =name.txt
zip_filename =sample.zip
zip_password =42
zip_encryptType =AES256
base_path =./
test_path = filepath.Join(base_path,test)
src_path = filepath.Join test_path,src)
extract_path = filepath.Join(test_path,extract)
extracted_txt_path = filepath.Join(extract_path,txt_filename)
txt_path = filepath.Join(src_path,txt_filename)
zip_path = filepath.Join(src_path,zip_filename)
)
var txt_fileSize int64
func checkError(e error){
if e!= nil {
恐慌(e)
}
}
func setupTestDir(){
fmt.Printf(删除`%s`\\\
,test_path)
var e错误
os.Remove(test_path)
fmt.Printf(Creating`%s`,`%s`\\\
,extract_path,src_path)
e = os.MkdirAll(src_path, os.ModeDir | os.ModePerm)
checkError(e)
e = os.MkdirAll(extract_path,os.ModeDir | os.ModePerm)
checkError(e)
}
func createSampleFile(){
fmt.Println(Creating,txt_path)
文件,e:= os.Create(txt_path)
checkError(e)
推迟文件.close()
_,e = file.WriteString(txt_content)
checkError(e)
fi,e:= file.Stat()
txt_fileSize = fi.Size )
func createZipWithPassword(){
fmt.Println(Creating,zip_path)
commandString:= fmt.Sprintf(`7za a%s%s -p %s-mem =%s`,zip_path,txt_path,zip_password,zip_encryptType)
commandSlice:= strings.Fields(commandString)
fmt.Println(commandString)
c:= exec.Command (commandSlice [0],commandSlice [1:] ...)
e:= c.Run()
checkError(e)
}
func extractZipWithPassword(){
fmt.Printf(将`%s`解压缩到目录`%s`\\\
,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)
}
func checkFor7Zip(){
_,e := exec.LookPath(7za)
if e!= nil {
fmt.Println(确保7zip已安装并包含您的路径。)
}
checkError(e)
}
func checkExtractedFile(){
fmt.Println(Reading,extracted_txt_path)
文件e:= os.Open(extracted_txt_path)
checkError(e)
推迟file.Close()
buf:= make([] byte,txt_fileSize)
n,e:= file.Read(buf)
checkError(e)
if!strings.Contains(string(buf [:n]),strings.Fields(txt_content)[0]){
panic(fmt.Sprintf(File`%s` is corrupted.\\\
,extracted_txt_path))
}
}
func main(){
fmt.Println(#Setup)
checkFor7Zip()
setupTestDir()
createSampleFile()
createZipWithPassword()
fmt.Println(#Answer to question ...)
extractZipWithPassword()
checkExtractedFile()
fmt.Println(完成。)
}
pre>
输出
#设置
删除`test `
创建`test / extract`,`test / src`
创建test / src / name.txt
创建test / src / sample.zip
7za test / src /sample.zip test / src / name.txt -p42-mem = AES256
#对问题的回答...
将`test / src / sample.zip'解压缩到`test / extract`目录
7za e test /src/sample.zip -stat / extracted -p42-aoa
读取测试/提取/ name.txt
完成。
Looking at the latest release (1.2) zip package - how can I unzip a file that was password protected (using 7zip, AES-256 encoding)? I don't see where/how to add in that information. A simple example would be great!
解决方案 The archive/zip
package seems to only provide basic zip functionality.
I would use 7zip to unzip password protected zip files using the os/exec
package.
Online 7-zip user guide
The best guide for understanding 7zip is 7-zip.chm, which is in the zip file for the windows command line.
The following code isn't optimal but it shows you how to get the job done.
Code for extracting a password protected zip using 7zip
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)
}
Example Program
// 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"
)
var (
txt_content = "Sample file created."
txt_filename = "name.txt"
zip_filename = "sample.zip"
zip_password = "42"
zip_encryptType = "AES256"
base_path = "./"
test_path = filepath.Join(base_path, "test")
src_path = filepath.Join(test_path, "src")
extract_path = filepath.Join(test_path, "extracted")
extracted_txt_path = filepath.Join(extract_path, txt_filename)
txt_path = filepath.Join(src_path, txt_filename)
zip_path = filepath.Join(src_path, zip_filename)
)
var txt_fileSize int64
func checkError(e error) {
if e != nil {
panic(e)
}
}
func setupTestDir() {
fmt.Printf("Removing `%s`\n", test_path)
var e error
os.Remove(test_path)
fmt.Printf("Creating `%s`,`%s`\n", extract_path, src_path)
e = os.MkdirAll(src_path, os.ModeDir|os.ModePerm)
checkError(e)
e = os.MkdirAll(extract_path, os.ModeDir|os.ModePerm)
checkError(e)
}
func createSampleFile() {
fmt.Println("Creating", txt_path)
file, e := os.Create(txt_path)
checkError(e)
defer file.Close()
_, e = file.WriteString(txt_content)
checkError(e)
fi, e := file.Stat()
txt_fileSize = fi.Size()
}
func createZipWithPassword() {
fmt.Println("Creating", zip_path)
commandString := fmt.Sprintf(`7za a %s %s -p"%s" -mem=%s`, zip_path, txt_path, zip_password, zip_encryptType)
commandSlice := strings.Fields(commandString)
fmt.Println(commandString)
c := exec.Command(commandSlice[0], commandSlice[1:]...)
e := c.Run()
checkError(e)
}
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)
}
func checkFor7Zip() {
_, e := exec.LookPath("7za")
if e != nil {
fmt.Println("Make sure 7zip is install and include your path.")
}
checkError(e)
}
func checkExtractedFile() {
fmt.Println("Reading", extracted_txt_path)
file, e := os.Open(extracted_txt_path)
checkError(e)
defer file.Close()
buf := make([]byte, txt_fileSize)
n, e := file.Read(buf)
checkError(e)
if !strings.Contains(string(buf[:n]), strings.Fields(txt_content)[0]) {
panic(fmt.Sprintf("File`%s` is corrupted.\n", extracted_txt_path))
}
}
func main() {
fmt.Println("# Setup")
checkFor7Zip()
setupTestDir()
createSampleFile()
createZipWithPassword()
fmt.Println("# Answer to question...")
extractZipWithPassword()
checkExtractedFile()
fmt.Println("Done.")
}
Output
# Setup
Removing `test`
Creating `test/extracted`,`test/src`
Creating test/src/name.txt
Creating test/src/sample.zip
7za a test/src/sample.zip test/src/name.txt -p"42" -mem=AES256
# 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.
这篇关于Golang 1.2:解压缩密码保护的zip文件?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
//显示如何使用7zip提取passsword加密zip文件。
// By Larry Battle< https://github.com/LarryBattle>
//回答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
//有效的Golang - http://golang.org/doc/effective_go.html
包裹主
导入(
fmt
os
os / exec
路径/文件路径
字符串
)
var(
txt_content =创建示例文件。
txt_filename =name.txt
zip_filename =sample.zip
zip_password =42
zip_encryptType =AES256
base_path =./
test_path = filepath.Join(base_path,test)
src_path = filepath.Join test_path,src)
extract_path = filepath.Join(test_path,extract)
extracted_txt_path = filepath.Join(extract_path,txt_filename)
txt_path = filepath.Join(src_path,txt_filename)
zip_path = filepath.Join(src_path,zip_filename)
)
var txt_fileSize int64
func checkError(e error){
if e!= nil {
恐慌(e)
}
}
func setupTestDir(){
fmt.Printf(删除`%s`\\\
,test_path)
var e错误
os.Remove(test_path)
fmt.Printf(Creating`%s`,`%s`\\\
,extract_path,src_path)
e = os.MkdirAll(src_path, os.ModeDir | os.ModePerm)
checkError(e)
e = os.MkdirAll(extract_path,os.ModeDir | os.ModePerm)
checkError(e)
}
func createSampleFile(){
fmt.Println(Creating,txt_path)
文件,e:= os.Create(txt_path)
checkError(e)
推迟文件.close()
_,e = file.WriteString(txt_content)
checkError(e)
fi,e:= file.Stat()
txt_fileSize = fi.Size )
func createZipWithPassword(){
fmt.Println(Creating,zip_path)
commandString:= fmt.Sprintf(`7za a%s%s -p %s-mem =%s`,zip_path,txt_path,zip_password,zip_encryptType)
commandSlice:= strings.Fields(commandString)
fmt.Println(commandString)
c:= exec.Command (commandSlice [0],commandSlice [1:] ...)
e:= c.Run()
checkError(e)
}
func extractZipWithPassword(){
fmt.Printf(将`%s`解压缩到目录`%s`\\\
,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)
}
func checkFor7Zip(){
_,e := exec.LookPath(7za)
if e!= nil {
fmt.Println(确保7zip已安装并包含您的路径。)
}
checkError(e)
}
func checkExtractedFile(){
fmt.Println(Reading,extracted_txt_path)
文件e:= os.Open(extracted_txt_path)
checkError(e)
推迟file.Close()
buf:= make([] byte,txt_fileSize)
n,e:= file.Read(buf)
checkError(e)
if!strings.Contains(string(buf [:n]),strings.Fields(txt_content)[0]){
panic(fmt.Sprintf(File`%s` is corrupted.\\\
,extracted_txt_path))
}
}
func main(){
fmt.Println(#Setup)
checkFor7Zip()
setupTestDir()
createSampleFile()
createZipWithPassword()
fmt.Println(#Answer to question ...)
extractZipWithPassword()
checkExtractedFile()
fmt.Println(完成。)
}
pre> 输出
#设置
删除`test `
创建`test / extract`,`test / src`
创建test / src / name.txt
创建test / src / sample.zip
7za test / src /sample.zip test / src / name.txt -p42-mem = AES256
#对问题的回答...
将`test / src / sample.zip'解压缩到`test / extract`目录
7za e test /src/sample.zip -stat / extracted -p42-aoa
读取测试/提取/ name.txt
完成。
Looking at the latest release (1.2) zip package - how can I unzip a file that was password protected (using 7zip, AES-256 encoding)? I don't see where/how to add in that information. A simple example would be great!
The archive/zip
package seems to only provide basic zip functionality.
I would use 7zip to unzip password protected zip files using the os/exec
package.
Online 7-zip user guide
The best guide for understanding 7zip is 7-zip.chm, which is in the zip file for the windows command line.
The following code isn't optimal but it shows you how to get the job done.
Code for extracting a password protected zip using 7zip
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)
}
Example Program
// 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"
)
var (
txt_content = "Sample file created."
txt_filename = "name.txt"
zip_filename = "sample.zip"
zip_password = "42"
zip_encryptType = "AES256"
base_path = "./"
test_path = filepath.Join(base_path, "test")
src_path = filepath.Join(test_path, "src")
extract_path = filepath.Join(test_path, "extracted")
extracted_txt_path = filepath.Join(extract_path, txt_filename)
txt_path = filepath.Join(src_path, txt_filename)
zip_path = filepath.Join(src_path, zip_filename)
)
var txt_fileSize int64
func checkError(e error) {
if e != nil {
panic(e)
}
}
func setupTestDir() {
fmt.Printf("Removing `%s`\n", test_path)
var e error
os.Remove(test_path)
fmt.Printf("Creating `%s`,`%s`\n", extract_path, src_path)
e = os.MkdirAll(src_path, os.ModeDir|os.ModePerm)
checkError(e)
e = os.MkdirAll(extract_path, os.ModeDir|os.ModePerm)
checkError(e)
}
func createSampleFile() {
fmt.Println("Creating", txt_path)
file, e := os.Create(txt_path)
checkError(e)
defer file.Close()
_, e = file.WriteString(txt_content)
checkError(e)
fi, e := file.Stat()
txt_fileSize = fi.Size()
}
func createZipWithPassword() {
fmt.Println("Creating", zip_path)
commandString := fmt.Sprintf(`7za a %s %s -p"%s" -mem=%s`, zip_path, txt_path, zip_password, zip_encryptType)
commandSlice := strings.Fields(commandString)
fmt.Println(commandString)
c := exec.Command(commandSlice[0], commandSlice[1:]...)
e := c.Run()
checkError(e)
}
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)
}
func checkFor7Zip() {
_, e := exec.LookPath("7za")
if e != nil {
fmt.Println("Make sure 7zip is install and include your path.")
}
checkError(e)
}
func checkExtractedFile() {
fmt.Println("Reading", extracted_txt_path)
file, e := os.Open(extracted_txt_path)
checkError(e)
defer file.Close()
buf := make([]byte, txt_fileSize)
n, e := file.Read(buf)
checkError(e)
if !strings.Contains(string(buf[:n]), strings.Fields(txt_content)[0]) {
panic(fmt.Sprintf("File`%s` is corrupted.\n", extracted_txt_path))
}
}
func main() {
fmt.Println("# Setup")
checkFor7Zip()
setupTestDir()
createSampleFile()
createZipWithPassword()
fmt.Println("# Answer to question...")
extractZipWithPassword()
checkExtractedFile()
fmt.Println("Done.")
}
Output
# Setup
Removing `test`
Creating `test/extracted`,`test/src`
Creating test/src/name.txt
Creating test/src/sample.zip
7za a test/src/sample.zip test/src/name.txt -p"42" -mem=AES256
# 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.
这篇关于Golang 1.2:解压缩密码保护的zip文件?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!