Golang 7z 压缩设置密码 - 代码示例
Golang 7z 压缩设置密码的代码示例:
package main
import (
"fmt"
"github.com/kjk/lzma"
"os"
)
func main() {
// 创建7z压缩文件
f, err := os.Create('test.7z')
if err != nil {
panic(err)
}
defer f.Close()
// 设置压缩密码
password := '123456'
options := &lzma.WriterProperties{}
options.SetPassword(password)
// 压缩文件
w := lzma.NewWriter2(f, options)
defer w.Close()
data := []byte('hello world')
_, err = w.Write(data)
if err != nil {
panic(err)
}
fmt.Println('文件压缩成功!')
}
在上面的代码示例中,我们使用了第三方库 'github.com/kjk/lzma' 来实现 7z 压缩。其中 'options.SetPassword(password)' 方法用于设置压缩密码。
原文地址: https://www.cveoy.top/t/topic/m8Jw 著作权归作者所有。请勿转载和采集!