1 Star 0 Fork 0

h79/goutils

加入 Gitee
与超过 1200万 开发者一起发现、参与优秀开源项目,私有仓库也完全免费 :)
免费加入
文件
api
auth
build
common
algorithm
app
archive
gzip
tar
tar.go
tar_test.go
targz
tarxz
testdata
zip
archive.go
zip.go
attributes
banner
bus
coder
compress
config
data
debug
file
git
http
images
json
list
logger
mapper
option
perf
queue
random
result
scheduler
secret
server
ssh
stringutil
svc
system
tag
template
timer
trie
version
watcher
xml
yaml
bit_flag.go
common.go
integer.go
dao
discovery
loader
mq
plugins
request
rpc
sensitive
thrift
.gitignore
LICENSE
Makefile
README.md
doc.go
error.md
go.mod
go.sum
goutils.go
goutils_test.go
version.go
克隆/下载
tar.go 2.37 KB
一键复制 编辑 原始数据 按行查看 历史
huqiuyun 提交于 1年前 . build
// Package tar implements the Archive interface providing tar archiving.
package tar
import (
"archive/tar"
"fmt"
fileconfig "gitee.com/h79/goutils/common/file/config"
"io"
"io/fs"
"os"
)
// Archive as tar.
type Archive struct {
tw *tar.Writer
files map[string]bool
}
// New tar archive.
func New(target io.Writer) Archive {
return Archive{
tw: tar.NewWriter(target),
files: map[string]bool{},
}
}
// Copying creates a new tar with the contents of the given tar.
func Copying(source io.Reader, target io.Writer) (Archive, error) {
w := New(target)
r := tar.NewReader(source)
for {
header, err := r.Next()
if err == io.EOF || header == nil {
break
}
if err != nil {
return Archive{}, err
}
w.files[header.Name] = true
if err := w.tw.WriteHeader(header); err != nil {
return w, err
}
if _, err := io.Copy(w.tw, r); err != nil {
return w, err
}
}
return w, nil
}
// Close all closeables.
func (a Archive) Close() error {
return a.tw.Close()
}
// Add file to the archive.
func (a Archive) Add(f fileconfig.File, stream ...fileconfig.ReaderStream) error {
if _, ok := a.files[f.Destination]; ok {
return &fs.PathError{Err: fs.ErrExist, Path: f.Destination, Op: "add"}
}
a.files[f.Destination] = true
info, err := os.Lstat(f.Source) // #nosec
if err != nil {
return fmt.Errorf("%s: %w", f.Source, err)
}
var link string
if info.Mode()&os.ModeSymlink != 0 {
link, err = os.Readlink(f.Source) // #nosec
if err != nil {
return fmt.Errorf("%s: %w", f.Source, err)
}
}
header, err := tar.FileInfoHeader(info, link)
if err != nil {
return fmt.Errorf("%s: %w", f.Source, err)
}
header.Name = f.Destination
if !f.Info.ParsedMTime.IsZero() {
header.ModTime = f.Info.ParsedMTime
}
if f.Info.Mode != 0 {
header.Mode = int64(f.Info.Mode)
}
if f.Info.Owner != "" {
header.Uid = 0
header.Uname = f.Info.Owner
}
if f.Info.Group != "" {
header.Gid = 0
header.Gname = f.Info.Group
}
if err = a.tw.WriteHeader(header); err != nil {
return fmt.Errorf("%s: %w", f.Source, err)
}
if info.IsDir() || info.Mode()&os.ModeSymlink != 0 {
return nil
}
file, err := os.Open(f.Source) // #nosec
if err != nil {
return fmt.Errorf("%s: %w", f.Source, err)
}
defer file.Close()
if _, err = io.Copy(a.tw, file); err != nil {
return fmt.Errorf("%s: %w", f.Source, err)
}
for i := range stream {
stream[i].OnReader(file)
}
return nil
}
Loading...
马建仓 AI 助手
尝试更多
代码解读
代码找茬
代码优化
Go
1
https://gitee.com/h79/goutils.git
git@gitee.com:h79/goutils.git
h79
goutils
goutils
v1.9.7

搜索帮助