1 Star 0 Fork 0

h79 / goutils

加入 Gitee
与超过 1200万 开发者一起发现、参与优秀开源项目,私有仓库也完全免费 :)
免费加入
克隆/下载
copy.go 1.36 KB
一键复制 编辑 原始数据 按行查看 历史
huqiuyun 提交于 2024-04-01 18:55 . build
package build
import (
"fmt"
"gitee.com/h79/goutils/common/archive"
fileconfig "gitee.com/h79/goutils/common/file/config"
"io"
"os"
"path/filepath"
)
type copyBuilder struct {
}
func (t *copyBuilder) Ext() string {
return ""
}
func (*copyBuilder) New(w io.WriteCloser, conf archive.Config) archive.Archive {
return &copyArchive{Path: conf.Path}
}
func (*copyBuilder) Coping(r *os.File, w io.Writer, conf archive.Config) (archive.Archive, error) {
return nil, fmt.Errorf("not support")
}
type copyArchive struct {
Path string
}
func (c *copyArchive) Close() error {
return nil
}
func (c *copyArchive) Add(f fileconfig.File, stream ...fileconfig.ReaderStream) error {
info, err := os.Lstat(f.Source) // #nosec
if err != nil {
return fmt.Errorf("%s: %w", f.Source, err)
}
filename := filepath.Join(c.Path, f.Destination)
if info.IsDir() {
return os.Mkdir(filename, f.Info.Mode)
}
// Open the file and create a new one
r, err := os.Open(f.Source)
if err != nil {
return err
}
defer func(r *os.File) {
err = r.Close()
if err != nil {
}
}(r)
w, err := os.Create(filename)
if err != nil {
return err
}
defer func(w *os.File) {
err = w.Close()
if err != nil {
}
}(w)
// Copy the content
_, err = io.Copy(w, r)
if err != nil {
return err
}
if err != nil {
return err
}
for i := range stream {
stream[i].OnReader(r)
}
return nil
}
Go
1
https://gitee.com/h79/goutils.git
git@gitee.com:h79/goutils.git
h79
goutils
goutils
v1.20.65

搜索帮助