golang创建压缩文件
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
使用方法 一:
精河网站制作公司哪家好,找创新互联建站!从网页设计、网站建设、微信开发、APP开发、成都响应式网站建设公司等网站项目制作,到程序开发,运营维护。创新互联建站从2013年成立到现在10年的时间,我们拥有了丰富的建站经验和运维经验,来保证我们的工作的顺利进行。专注于网站建设就选创新互联建站。
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创建压缩文件
文章起源:http://hbruida.cn/article/igsojd.html