1 Star 0 Fork 0

h79/goutils

加入 Gitee
与超过 1200万 开发者一起发现、参与优秀开源项目,私有仓库也完全免费 :)
免费加入
文件
克隆/下载
archive.go 1.67 KB
一键复制 编辑 原始数据 按行查看 历史
huqiuyun 提交于 2023-09-06 14:47 . archive
// Package archive provides tar.gz and zip archiving
package archive
import (
"fmt"
"gitee.com/h79/goutils/common/archive/gzip"
"gitee.com/h79/goutils/common/archive/tar"
"gitee.com/h79/goutils/common/archive/targz"
"gitee.com/h79/goutils/common/archive/tarxz"
"gitee.com/h79/goutils/common/archive/zip"
fileconfig "gitee.com/h79/goutils/common/file/config"
"io"
"os"
)
// Archive represents a compression archive files from disk can be written to.
type Archive interface {
Close() error
Add(f fileconfig.File, stream ...fileconfig.ReaderStream) error
}
type Object interface {
New(w io.Writer) Archive
Coping(r *os.File, w io.Writer) (Archive, error)
}
func Register(format string, obj Object) {
reg[format] = obj
}
var reg map[string]Object = make(map[string]Object)
// New archive.
func New(w io.Writer, format string) (Archive, error) {
switch format {
case "tar.gz", "tgz":
return targz.New(w), nil
case "tar":
return tar.New(w), nil
case "gz":
return gzip.New(w), nil
case "tar.xz", "txz":
return tarxz.New(w), nil
case "zip":
return zip.New(w), nil
default:
if n, ok := reg[format]; ok {
return n.New(w), nil
}
}
return nil, fmt.Errorf("invalid archive format: %s", format)
}
// Copying copies the source archive into a new one, which can be appended at.
// Source needs to be in the specified format.
func Copying(r *os.File, w io.Writer, format string) (Archive, error) {
switch format {
case "tar.gz", "tgz":
return targz.Copying(r, w)
case "tar":
return tar.Copying(r, w)
case "zip":
return zip.Copying(r, w)
default:
if n, ok := reg[format]; ok {
return n.Coping(r, w)
}
}
return nil, fmt.Errorf("invalid archive format: %s", format)
}
Loading...
马建仓 AI 助手
尝试更多
代码解读
代码找茬
代码优化
Go
1
https://gitee.com/h79/goutils.git
git@gitee.com:h79/goutils.git
h79
goutils
goutils
v1.9.8

搜索帮助