美文网首页
go操作zip

go操作zip

作者: 彳亍口巴 | 来源:发表于2022-06-12 14:09 被阅读0次

解压和压缩


func test01() {
    r, err := zip.OpenReader(`zip_test.zip`)
    if err != nil {
        fmt.Println(err)
        return
    }
    for _, k := range r.Reader.File {
        if k.FileInfo().IsDir() {
            err := os.MkdirAll(k.Name, 0644)
            if err != nil {
                fmt.Println(err)
            }
            continue
        }
        r, err := k.Open()
        if err != nil {
            fmt.Println(err)
            continue
        }
        fmt.Println("unzip: ", k.Name)
        defer r.Close()
        NewFile, err := os.Create(k.Name)
        if err != nil {
            fmt.Println(err)
            continue
        }
        io.Copy(NewFile, r)
        NewFile.Close()
    }
}

// zip 压缩(dirs格式:/data/ceph_fuse/storage/export_excel/20201030131059)
func zip01(dirs string) string {
    dir := dirs[strings.LastIndex(dirs, "/")+1:]
    zipName := fmt.Sprintf("%s.zip", dir)
    zipCmd := fmt.Sprintf("zip -jm %s/%s %s/*", dirs, zipName, dirs)

    defer shell.ErrExit()
    shell.Trace = true
    shell.Shell = []string{"/bin/bash", "-c"}
    shell.Run(zipCmd)

    return dir + "/" + zipName
}

相关文章

网友评论

      本文标题:go操作zip

      本文链接:https://www.haomeiwen.com/subject/oavgmrtx.html