Golang

关注公众号 jb51net

关闭
首页 > 脚本专栏 > Golang > go压缩解压zip文件

go 压缩解压zip文件源码示例

作者:ChenXcc

这篇文章主要为大家介绍了go压缩及解压zip文件的源码示例,有需要的朋友可以借鉴参考下,希望能够有所帮助,祝大家多多进步,早日升职加薪

压缩zip

func Zip(dest string, paths ...string) error {
    zfile, err := os.Create(dest)
    if err != nil {
        return err
    }
    defer zfile.Close()
    zipWriter := zip.NewWriter(zfile)
    defer zipWriter.Close()
    for _, src := range paths {
        // remove the trailing path sepeartor if it is a directory
        src := strings.TrimSuffix(src, string(os.PathSeparator))
        err = filepath.Walk(src, func(path string, info os.FileInfo, err error) error {
            if err != nil {
                return err
            }
            // create local file header
            header, err := zip.FileInfoHeader(info)
            if err != nil {
                return err
            }
            // set compression method to deflate
            header.Method = zip.Deflate
            // set relative path of file in zip archive
            header.Name, err = filepath.Rel(filepath.Dir(src), path)
            if err != nil {
                return err
            }
            if info.IsDir() {
                header.Name += string(os.PathSeparator)
            }
            // create writer for writing header
            headerWriter, err := zipWriter.CreateHeader(header)
            if err != nil {
                return err
            }
            if info.IsDir() {
                return nil
            }
            f, err := os.Open(path)
            if err != nil {
                return err
            }
            defer f.Close()
            _, err = io.Copy(headerWriter, f)
            return err
        })
        if err != nil {
            return err
        }
    }
    return nil
}

解压zip

func Unzip(src string, dest string) error {
    reader, err := zip.OpenReader(src)
    if err != nil {
        return err
    }
    defer reader.Close()
    for _, file := range reader.File {
        filePath := path.Join(dest, file.Name)
        if file.FileInfo().IsDir() {
            os.MkdirAll(filePath, os.ModePerm)
        } else {
            if err = os.MkdirAll(filepath.Dir(filePath), os.ModePerm); err != nil {
                return err
            }
            inFile, err := file.Open()
            if err != nil {
                return err
            }
            defer inFile.Close()
            outFile, err := os.OpenFile(filePath, os.O_WRONLY|os.O_CREATE|os.O_TRUNC, file.Mode())
            if err != nil {
                return err
            }
            defer outFile.Close()
            _, err = io.Copy(outFile, inFile)
            if err != nil {
                return err
            }
        }
    }
    return nil
}

工程源码

https://github.com/xbmlz/gct

以上就是go 压缩解压zip文件源码示例的详细内容,更多关于go压缩解压zip文件的资料请关注脚本之家其它相关文章!

您可能感兴趣的文章:
阅读全文