golang 创建压缩文件

发布时间:2020-08-03 23:38:05 作者:woahjknes
来源:网络 阅读:1442
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
//上面正好是我们需要的目录
推荐阅读:
  1. Golang如何配置和创建Logger
  2. golang入门

免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。

golang zip olang

上一篇:为SharePoint 2016 配置SQL 2016的always on 模式(四)

下一篇:用delete命令来个不小心删除了user表

相关阅读

您好,登录后才能下订单哦!

密码登录
登录注册
其他方式登录
点击 登录注册 即表示同意《亿速云用户服务条款》