1 Star 0 Fork 0

h79 / goutils

加入 Gitee
与超过 1200万 开发者一起发现、参与优秀开源项目,私有仓库也完全免费 :)
免费加入
克隆/下载
targz.go 1.30 KB
一键复制 编辑 原始数据 按行查看 历史
huqiuyun 提交于 2023-11-29 00:53 . 7z
// Package targz implements the Archive interface providing tar.gz archiving
// and compression.
package targz
import (
"compress/gzip"
"gitee.com/h79/goutils/common/archive/tar"
fileconfig "gitee.com/h79/goutils/common/file/config"
"io"
)
// Archive as tar.gz.
type Archive struct {
gw *gzip.Writer
tw *tar.Archive
}
// New tar.gz archive.
func New(target io.Writer) Archive {
// the error will be nil since the compression level is valid
writer, _ := gzip.NewWriterLevel(target, gzip.BestCompression)
tw := tar.New(writer)
return Archive{
gw: writer,
tw: &tw,
}
}
func Copying(source io.Reader, target io.Writer) (Archive, error) {
// the error will be nil since the compression level is valid
writer, _ := gzip.NewWriterLevel(target, gzip.BestCompression)
src, err := gzip.NewReader(source)
if err != nil {
return Archive{}, err
}
tw, err := tar.Copying(src, writer)
return Archive{
gw: writer,
tw: &tw,
}, err
}
// Close all closeables.
func (a Archive) Close() error {
if a.tw != nil {
err := a.tw.Close()
a.tw = nil
if err != nil {
return err
}
}
if a.gw != nil {
err := a.gw.Close()
a.gw = nil
return err
}
return nil
}
// Add file to the archive.
func (a Archive) Add(f fileconfig.File, stream ...fileconfig.ReaderStream) error {
return a.tw.Add(f, stream...)
}
Go
1
https://gitee.com/h79/goutils.git
git@gitee.com:h79/goutils.git
h79
goutils
goutils
v1.20.65

搜索帮助