内容简介:// 执行之后使用方法 二:替换成我们想要的结构
import ( "os" "archive/zip" "io" "fmt" "strings" ) // ZipFiles compresses one or many files into a single zip archive file. //压缩多个文件到一个文件里面 // Param 1: 输出的zip文件的名字 // Param 2: 需要添加到zip文件里面的文件 //Param 3: 由于file是绝对路径,打包后可能不是想要的目录,oldform就是filename中需要被替换的掉的路径 //Param 4: 要替换成的路径 func ZipFiles(filename string, files []string, oldform, newform string) error { newZipFile, err := os.Create(filename) if err != nil { return err } defer newZipFile.Close() zipWriter := zip.NewWriter(newZipFile) defer zipWriter.Close() // 把files添加到zip中 for _, file := range files { zipfile, err := os.Open(file) if err != nil { return err } defer zipfile.Close() // 获取file的基础信息 info, err := zipfile.Stat() if err != nil { return err } header, err := zip.FileInfoHeader(info) if err != nil { return err } //使用上面的FileInforHeader() 就可以把文件保存的路径替换成我们自己想要的了,如下面 header.Name = strings.Replace(file, oldform, newform, -1) // 优化压缩 // 更多参考see http://golang.org/pkg/archive/zip/#pkg-constants header.Method = zip.Deflate writer, err := zipWriter.CreateHeader(header) if err != nil { return err } if _, err = io.Copy(writer, zipfile); err != nil { return err } } return nil
使用方法 一:
func main() { fileList := []string{ "/Users/Jack/Downloads/QQreceiver/72bian_log/anal.txt", "/Users/Jack/Downloads/QQreceiver/72bian_log/config.json", "/Users/Jack/Downloads/QQreceiver/72bian_log/xzwx_client/md5.txt", } //保留原来文件的结构 err := ZipFiles("/Users/Jack/Downloads/QQreceiver/test.zip", fileList, "","") if err != nil { fmt.Println(err) } }
// 执行之后
//在对应目录下生成了我们定义的zip文件 ➜ QQreceiver# ls test.zip test.zip ➜ QQreceiver# pwd /Users/Jack/Downloads/QQreceiver //解压test.zip ➜ QQreceiver# ls Users test.zip ➜ QQreceiver# ls -R Users Jack Users/Jack: Downloads Users/Jack/Downloads: QQreceiver Users/Jack/Downloads/QQreceiver: 72bian_log Users/Jack/Downloads/QQreceiver/72bian_log: anal.txt config.json xzwx_client Users/Jack/Downloads/QQreceiver/72bian_log/xzwx_client: md5.txt //可以看到test打包的是文件的绝对路径,如果这个test.zip是我们要更新的程序的目录,这样肯定是不能用的
使用方法 二:
替换成我们想要的结构
func main() { fileList := []string{ "/Users/Jack/Downloads/QQreceiver/72bian_log/anal.txt", "/Users/Jack/Downloads/QQreceiver/72bian_log/config.json", "/Users/Jack/Downloads/QQreceiver/72bian_log/xzwx_client/md5.txt", } //比如线上的程序的目录为game ,"/Users/Jack/Downloads/QQreceiver/72bian_log/"下面的文件为我们线game下的程序文件内容 err := ZipFiles("/Users/Jack/Downloads/QQreceiver/test.zip", fileList, "/Users/Jack/Downloads/QQreceiver/72bian_log","game/") if err != nil { fmt.Println(err) } }
执行之后
//在对应目录下生同样生成了我们定义的zip文件 ➜ QQreceiver# ls test.zip test.zip ➜ QQreceiver# pwd /Users/Jack/Downloads/QQreceiver //解压test.zip ➜ QQreceiver# ls game test.zip ➜ QQreceiver# ls -R game anal.txt config.json xzwx_client game/xzwx_client: md5.txt //上面正好是我们需要的目录
以上所述就是小编给大家介绍的《golang 创建压缩文件》,希望对大家有所帮助,如果大家有任何疑问请给我留言,小编会及时回复大家的。在此也非常感谢大家对 码农网 的支持!
猜你喜欢:- HBase无法创建带有snappy压缩属性的表
- golang压缩和解压缩zip文件
- bitcoin:压缩公钥与未压缩公钥
- 一句话轻松实现压缩图片和文件压缩
- 基于Node.js实现压缩和解压缩的方法
- 一种 JNI 方法实现图片压缩,压缩率极高
本站部分资源来源于网络,本站转载出于传递更多信息之目的,版权归原作者或者来源机构所有,如转载稿涉及版权问题,请联系我们。