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
使用方法 一:
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
//上面正好是我们需要的目录
另外有需要云服务器可以了解下创新互联scvps.cn,海内外云服务器15元起步,三天无理由+7*72小时售后在线,公司持有idc许可证,提供“云服务器、裸金属服务器、高防服务器、香港服务器、美国服务器、虚拟主机、免备案服务器”等云主机租用服务以及企业上云的综合解决方案,具有“安全稳定、简单易用、服务可用性高、性价比高”等特点与优势,专为企业上云打造定制,能够满足用户丰富、多元化的应用场景需求。
当前题目:golang创建压缩文件-创新互联
文章地址:http://cdiso.cn/article/dgdoch.html