在没有本机支持的情况下在 Golang 1.2 中提取受密码保护的 ZIP 文件
虽然 Go 1.2 中的 archive/zip 包提供了基本的 ZIP 功能,它缺乏处理受密码保护的档案的机制。为了填补这一空白,建议的方法是利用 os/exec 包并利用 7-zip 等外部工具来执行提取。
使用 7-zip 提取受密码保护的 ZIP 文件
7-zip 提供强大的命令行功能来操作存档,包括受密码保护的 ZIP 文件。以下是如何在 Go 中使用 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>
示例程序
以下 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中文网其他相关文章!

Go的strings包提供了多种字符串操作功能。1)使用strings.Contains检查子字符串。2)用strings.Split将字符串分割成子字符串切片。3)通过strings.Join合并字符串。4)用strings.TrimSpace或strings.Trim去除字符串首尾的空白或指定字符。5)用strings.ReplaceAll替换所有指定子字符串。6)使用strings.HasPrefix或strings.HasSuffix检查字符串的前缀或后缀。

使用Go语言的strings包可以提升代码质量。1)使用strings.Join()优雅地连接字符串数组,避免性能开销。2)结合strings.Split()和strings.Contains()处理文本,注意大小写敏感问题。3)避免滥用strings.Replace(),考虑使用正则表达式进行大量替换。4)使用strings.Builder提高频繁拼接字符串的性能。

Go的bytes包提供了多种实用的函数来处理字节切片。1.bytes.Contains用于检查字节切片是否包含特定序列。2.bytes.Split用于将字节切片分割成smallerpieces。3.bytes.Join用于将多个字节切片连接成一个。4.bytes.TrimSpace用于去除字节切片的前后空白。5.bytes.Equal用于比较两个字节切片是否相等。6.bytes.Index用于查找子切片在largerslice中的起始索引。

theEncoding/binarypackageingoisesenebecapeitProvidesAstandArdArdArdArdArdArdArdArdAndWriteBinaryData,确保Cross-cross-platformCompatibilitiational and handhandlingdifferentendenness.itoffersfunctionslikeread,写下,写,dearte,readuvarint,andwriteuvarint,andWriteuvarIntforPreciseControloverBinary

回顾bytespackageingoiscialforhandlingbyteslicesandbuffers,offeringToolsforefficeMemoryManagement和datamAnipulation.1)ItProvidesfunctionalitiesLikeCreatingBuffers,比较,搜索/更换/更换/更换forlargedAtatAsetsets.n

你应该关心Go语言中的"strings"包,因为它提供了处理文本数据的工具,从基本的字符串拼接到高级的正则表达式匹配。1)"strings"包提供了高效的字符串操作,如Join函数用于拼接字符串,避免性能问题。2)它包含高级功能,如ContainsAny函数,用于检查字符串是否包含特定字符集。3)Replace函数用于替换字符串中的子串,需注意替换顺序和大小写敏感性。4)Split函数可以根据分隔符拆分字符串,常用于正则表达式处理。5)使用时需考虑性能,如

“编码/二进制”软件包interingoisentialForHandlingBinaryData,oferingToolSforreDingingAndWritingBinaryDataEfficely.1)Itsupportsbothlittle-endianandBig-endianBig-endianbyteorders,CompialforOss-System-System-System-compatibility.2)

掌握Go语言中的bytes包有助于提高代码的效率和优雅性。1)bytes包对于解析二进制数据、处理网络协议和内存管理至关重要。2)使用bytes.Buffer可以逐步构建字节切片。3)bytes包提供了搜索、替换和分割字节切片的功能。4)bytes.Reader类型适用于从字节切片读取数据,特别是在I/O操作中。5)bytes包与Go的垃圾回收器协同工作,提高了大数据处理的效率。


热AI工具

Undresser.AI Undress
人工智能驱动的应用程序,用于创建逼真的裸体照片

AI Clothes Remover
用于从照片中去除衣服的在线人工智能工具。

Undress AI Tool
免费脱衣服图片

Clothoff.io
AI脱衣机

Video Face Swap
使用我们完全免费的人工智能换脸工具轻松在任何视频中换脸!

热门文章

热工具

SecLists
SecLists是最终安全测试人员的伙伴。它是一个包含各种类型列表的集合,这些列表在安全评估过程中经常使用,都在一个地方。SecLists通过方便地提供安全测试人员可能需要的所有列表,帮助提高安全测试的效率和生产力。列表类型包括用户名、密码、URL、模糊测试有效载荷、敏感数据模式、Web shell等等。测试人员只需将此存储库拉到新的测试机上,他就可以访问到所需的每种类型的列表。

DVWA
Damn Vulnerable Web App (DVWA) 是一个PHP/MySQL的Web应用程序,非常容易受到攻击。它的主要目标是成为安全专业人员在合法环境中测试自己的技能和工具的辅助工具,帮助Web开发人员更好地理解保护Web应用程序的过程,并帮助教师/学生在课堂环境中教授/学习Web应用程序安全。DVWA的目标是通过简单直接的界面练习一些最常见的Web漏洞,难度各不相同。请注意,该软件中

Dreamweaver CS6
视觉化网页开发工具

Atom编辑器mac版下载
最流行的的开源编辑器

Dreamweaver Mac版
视觉化网页开发工具