本文整理汇总了Golang中archive/tar.Header类的典型用法代码### 示例。如果您正苦于以下问题:Golang Header类的具体用法?Golang Header怎么用?Golang Header使用的例子?那么恭喜您, 这里精选的类代码### 示例或许可以为您提供帮助。
在下文中一共展示了Header类的20个代码### 示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于我们的系统推荐出更棒的Golang代码### 示例。
示例1: ConvertWrite
func (overlayWhiteoutConverter) ConvertWrite(hdr *tar.Header, path string, fi os.FileInfo) error {
// convert whiteouts to AUFS format
if fi.Mode()&os.ModeCharDevice != 0 && hdr.Devmajor == 0 && hdr.Devminor == 0 {
// we just rename the file and make it normal
hdr.Name = WhiteoutPrefix + hdr.Name
hdr.Mode = 0600
hdr.Typeflag = tar.TypeReg
}
if fi.Mode()&os.ModeDir != 0 {
// convert opaque dirs to AUFS format by writing an empty file with the prefix
opaque, err := system.Lgetxattr(path, "trusted.overlay.opaque")
if err != nil {
return err
}
if opaque != nil && len(opaque) == 1 && opaque[0] == 'y' {
// create a header for the whiteout file
// it should inherit some properties from the parent, but be a regular file
*hdr = tar.Header{
Typeflag: tar.TypeReg,
Mode: hdr.Mode & int64(os.ModePerm),
Name: filepath.Join(hdr.Name, WhiteoutOpaqueDir),
Size: 0,
Uid: hdr.Uid,
Uname: hdr.Uname,
Gid: hdr.Gid,
Gname: hdr.Gname,
AccessTime: hdr.AccessTime,
ChangeTime: hdr.ChangeTime,
}
}
}
return nil
}
开发者ID:FlyingShit-XinHuang,项目名称:docker,代码行数:35,代码来源:archive_linux.go
示例2: Process
// Process impl
func (p *ArchiveExtract) Process(hdr *tar.Header, r io.Reader) (*tar.Header, io.Reader, error) {
// make sure we have our tempdir
if p.workingDir == "" {
t, err := ioutil.TempDir(p.tempDir, "tar-")
if err != nil {
return hdr, r, err
}
p.workingDir = t
}
// If a directory make it and continue
fpath := filepath.Join(p.workingDir, hdr.Name)
if hdr.FileInfo().IsDir() {
err := os.MkdirAll(fpath, 0755)
return hdr, r, err
}
// Extract the file!
file, err := os.OpenFile(fpath, os.O_WRONLY|os.O_CREATE, hdr.FileInfo().Mode())
if err != nil {
return hdr, r, err
}
defer file.Close()
_, err = io.Copy(file, r)
if err != nil {
return hdr, r, err
}
return hdr, r, nil
}
开发者ID:hughker,项目名称:wercker,代码行数:32,代码来源:archive.go
示例3: writeItem
// Writes the given file into tarfn
func writeItem(tarfn string, fn string) (pos uint64, err error) {
pos = 0
sfh, err := os.Open(fn)
defer sfh.Close()
if sfi, err := sfh.Stat(); err == nil {
hdr := new(tar.Header)
hdr.Name = sfi.Name()
hdr.Size = sfi.Size()
hdr.Mode = int64(sfi.Mode().Perm())
hdr.ModTime = sfi.ModTime()
var (
tw *tar.Writer
f ReadWriteSeekCloser
)
if tw, f, pos, err = OpenForAppend(tarfn); err == nil {
defer f.Close()
defer tw.Close() //LIFO
if err := tw.WriteHeader(hdr); err == nil {
if zw, err := gzip.NewWriterLevel(tw, flate.BestCompression); err == nil {
defer zw.Close()
_, err = io.Copy(zw, sfh)
}
}
}
}
return
}
开发者ID:tgulacsi,项目名称:aostor,代码行数:28,代码来源:tarhelper.go
示例4: writeTarGz
func writeTarGz(filePath, baseDir string, tarGzWriter *tar.Writer, fileInfo os.FileInfo) error {
file, err := os.Open(filePath)
if err != nil {
return err
}
defer file.Close()
relativePath, err := filepath.Rel(baseDir, filePath)
if err != nil {
return err
}
header := new(tar.Header)
header.Name = relativePath
header.Size = fileInfo.Size()
header.Mode = int64(fileInfo.Mode())
header.ModTime = fileInfo.ModTime()
err = tarGzWriter.WriteHeader(header)
if err != nil {
return err
}
_, err = io.Copy(tarGzWriter, file)
if err != nil {
return err
}
return nil
}
开发者ID:wantedly,项目名称:risu,代码行数:34,代码来源:cache.go
示例5: TarFilelist
// file name filelist is like this: './source/file'
func TarFilelist(filelist []string, case_dir string, object_name string) (tar_url string) {
tar_url = path.Join(case_dir, object_name) + ".tar.gz"
fw, err := os.Create(tar_url)
if err != nil {
fmt.Println("Failed in create tar file ", err)
return tar_url
}
defer fw.Close()
gw := gzip.NewWriter(fw)
defer gw.Close()
tw := tar.NewWriter(gw)
defer tw.Close()
for index := 0; index < len(filelist); index++ {
source_file := filelist[index]
fi, err := os.Stat(path.Join(case_dir, source_file))
if err != nil {
fmt.Println(err)
continue
}
fr, err := os.Open(path.Join(case_dir, source_file))
if err != nil {
fmt.Println(err)
continue
}
h := new(tar.Header)
h.Name = source_file
h.Size = fi.Size()
h.Mode = int64(fi.Mode())
h.ModTime = fi.ModTime()
err = tw.WriteHeader(h)
_, err = io.Copy(tw, fr)
}
return tar_url
}
开发者ID:hqhq,项目名称:oct,代码行数:36,代码来源:libocit.go
示例6: addFile
func (t *Tarball) addFile(file *File) error {
// grab a handle on the file to read it into the buffer
fr, err := os.Open(file.path)
if err != nil {
return err
}
defer fr.Close()
if stat, err := fr.Stat(); err == nil {
// create the tarball header for this file
header := new(tar.Header)
header.Name = file.path
header.Size = stat.Size()
header.Mode = int64(stat.Mode())
header.ModTime = stat.ModTime()
// write the header to the current tarball
if err := t.tw.WriteHeader(header); err != nil {
return err
}
// copy the file data over now that the header is set
if _, err := io.Copy(t.tw, fr); err == nil {
t.size += stat.Size()
} else {
return err
}
} else {
return err
}
return nil
}
开发者ID:jonmorehouse,项目名称:go-glacier-ball,代码行数:29,代码来源:tarball.go
示例7: addFile
func addFile(path string, fileInfo os.FileInfo, tarFileWriter *tar.Writer) error {
if fileInfo.IsDir() {
return nil
}
file, err := os.Open(path)
if err != nil {
return err
}
defer file.Close()
header := new(tar.Header)
header.Name = file.Name()
header.Size = fileInfo.Size()
header.Mode = int64(fileInfo.Mode())
header.ModTime = fileInfo.ModTime()
err = tarFileWriter.WriteHeader(header)
if err != nil {
return err
}
_, err = io.Copy(tarFileWriter, file)
if err != nil {
return err
}
return nil
}
开发者ID:jonfk,项目名称:gotar,代码行数:30,代码来源:main.go
示例8: extractFile
func extractFile(dir string, header *tar.Header, tarReader io.Reader) error {
path := filepath.Join(dir, header.Name)
glog.V(3).Infof("Creating %s", path)
file, err := os.Create(path)
// The file times need to be modified after it's been closed thus this function
// is deferred after the file close (LIFO order for defer)
defer os.Chtimes(path, time.Now(), header.FileInfo().ModTime())
defer file.Close()
if err != nil {
return err
}
glog.V(3).Infof("Extracting/writing %s", path)
written, err := io.Copy(file, tarReader)
if err != nil {
return err
}
if written != header.Size {
return fmt.Errorf("Wrote %d bytes, expected to write %d", written, header.Size)
}
if runtime.GOOS != "windows" { // Skip chmod if on windows OS
return file.Chmod(header.FileInfo().Mode())
}
return nil
}
开发者ID:kcbabo,项目名称:origin,代码行数:25,代码来源:tar.go
示例9: compress
func compress() {
err := os.Mkdir("non_sparse", 0777)
tarfile, err := os.Create("non_sparse/non_sparse.tar")
if err != nil {
log.Fatalln(err)
}
defer tarfile.Close()
var fileWriter io.WriteCloser = tarfile
tfw := tar.NewWriter(fileWriter)
defer tfw.Close()
file, err := os.Open("sparse.img")
defer file.Close()
fileInfo, err := file.Stat()
header := new(tar.Header)
header.Name = file.Name()
header.Size = fileInfo.Size()
header.Mode = int64(fileInfo.Mode())
header.ModTime = fileInfo.ModTime()
err = tfw.WriteHeader(header)
if err != nil {
log.Fatalln(err)
}
_, err = io.Copy(tfw, file)
if err != nil {
log.Fatalln(err)
}
}
开发者ID:grubernaut,项目名称:Golang-Tar,代码行数:29,代码来源:compress.go
示例10: GetOwnerAndGroup
// We have a dummy version of this call in posix.go.
// Windows does not implement the syscall.Stat_t type we
// need, but the *nixes do. We use this in util.AddToArchive
// to set owner/group on files being added to a tar archive.
func GetOwnerAndGroup(finfo os.FileInfo, header *tar.Header) {
systat := finfo.Sys().(*syscall.Stat_t)
if systat != nil {
header.Uid = int(systat.Uid)
header.Gid = int(systat.Gid)
}
}
开发者ID:APTrust,项目名称:exchange,代码行数:11,代码来源:posix.go
### 示例11: ExtractReader
点赞 1
func (t *TarArchiveProvider) ExtractReader(dst string, src io.Reader) error {
tr := tar.NewReader(src)
for {
var h *tar.Header
h, err := tr.Next()
if err == io.EOF {
break
}
if err != nil {
return err
}
fp := filepath.Join(dst, h.Name)
if h.FileInfo().IsDir() {
os.MkdirAll(fp, 0755)
continue
}
os.MkdirAll(filepath.Dir(fp), 0755)
f, err := os.Create(fp)
if err != nil {
return err
}
_, err = io.Copy(f, tr)
f.Close()
if err != nil {
return err
}
os.Chmod(fp, os.FileMode(h.Mode))
}
return nil
}
开发者ID:KalyaniSingh21,项目名称:stack,代码行数:34,代码来源:tar.go
示例12: writeTar
// Write the files into the tar dir
func writeTar(path string, tw *tar.Writer, fi os.FileInfo) error {
fr, err := os.Open(path)
if err != nil {
return err
}
defer fr.Close()
h := new(tar.Header)
h.Name = fi.Name()
h.Size = fi.Size()
h.Mode = int64(fi.Mode())
h.ModTime = fi.ModTime()
err = tw.WriteHeader(h)
if err != nil {
return err
}
_, err = io.Copy(tw, fr)
if err != nil {
return err
}
return err
}
开发者ID:yeah-right,项目名称:webhog,代码行数:27,代码来源:store.go
示例13: addFileToArchive
func addFileToArchive(filePath string, addPath string, tarWriter *tar.Writer, fileInfo os.FileInfo) error {
addPath = filepath.ToSlash(addPath)
if isSymlink(fileInfo) {
link, err := filepath.EvalSymlinks(filePath)
if err != nil {
return err
}
filePath = link
fileInfo, err = os.Lstat(filePath)
if err != nil {
return err
}
}
header := new(tar.Header)
header.Name = addPath
header.Size = fileInfo.Size()
header.Mode = int64(fileInfo.Mode())
header.ModTime = fileInfo.ModTime()
fileReader, err := os.Open(filePath)
if err != nil {
return err
}
defer fileReader.Close()
err = tarWriter.WriteHeader(header)
fail.Handle(err)
_, err = io.Copy(tarWriter, fileReader)
return err
}
开发者ID:rootwzrd,项目名称:appstax-cli,代码行数:31,代码来源:hosting.go
示例14: extractTarArchiveFile
func extractTarArchiveFile(header *tar.Header, dest string, input io.Reader) error {
filePath := filepath.Join(dest, header.Name)
fileInfo := header.FileInfo()
if fileInfo.IsDir() {
err := os.MkdirAll(filePath, fileInfo.Mode())
if err != nil {
return err
}
} else {
err := os.MkdirAll(filepath.Dir(filePath), 0755)
if err != nil {
return err
}
if fileInfo.Mode()&os.ModeSymlink != 0 {
return os.Symlink(header.Linkname, filePath)
}
fileCopy, err := os.OpenFile(filePath, os.O_RDWR|os.O_CREATE|os.O_TRUNC, fileInfo.Mode())
if err != nil {
return err
}
defer fileCopy.Close()
_, err = io.Copy(fileCopy, input)
if err != nil {
return err
}
}
return nil
}
开发者ID:divolgin,项目名称:archiver,代码行数:33,代码来源:tgz_extractor.go
示例15: Download
func (c AdminData) Download(createdTime string) revel.Result {
backup, ok := configService.GetBackup(createdTime)
if !ok {
return c.RenderText("")
}
dbname, _ := revel.Config.String("db.dbname")
path := backup["path"] + "/" + dbname
allFiles := ListDir(path)
filename := "backup_" + dbname + "_" + backup["createdTime"] + ".tar.gz"
// file write
fw, err := os.Create(revel.BasePath + "/files/" + filename)
if err != nil {
return c.RenderText("")
}
// defer fw.Close() // 不需要关闭, 还要读取给用户下载
// gzip write
gw := gzip.NewWriter(fw)
defer gw.Close()
// tar write
tw := tar.NewWriter(gw)
defer tw.Close()
// 遍历文件列表
for _, file := range allFiles {
fn := path + "/" + file
fr, err := os.Open(fn)
fileInfo, _ := fr.Stat()
if err != nil {
return c.RenderText("")
}
defer fr.Close()
// 信息头
h := new(tar.Header)
h.Name = file
h.Size = fileInfo.Size()
h.Mode = int64(fileInfo.Mode())
h.ModTime = fileInfo.ModTime()
// 写信息头
err = tw.WriteHeader(h)
if err != nil {
panic(err)
}
// 写文件
_, err = io.Copy(tw, fr)
if err != nil {
panic(err)
}
} // for
return c.RenderBinary(fw, filename, revel.Attachment, time.Now()) // revel.Attachm
}
开发者ID:nosqldb,项目名称:zhujian,代码行数:58,代码来源:AdminData.go
示例16: extractFile
func extractFile(archive *tar.Reader, hdr *tar.Header, path string) error {
file, err := os.OpenFile(path, os.O_WRONLY|os.O_CREATE|os.O_TRUNC, hdr.FileInfo().Mode())
if err != nil {
return err
}
defer file.Close()
_, err = io.Copy(file, archive)
return err
}
开发者ID:nicolasleger,项目名称:heroku-cli,代码行数:9,代码来源:tar.go
示例17: WriteHeader
// WriteHeader renames files and directories inline as a tarfile is being
// written
func (a RenameAdapter) WriteHeader(hdr *tar.Header) error {
if hdr.Name == a.Old {
hdr.Name = a.New
} else if strings.HasPrefix(hdr.Name, a.Old+"/") {
hdr.Name = a.New + hdr.Name[len(a.Old):]
}
return a.Writer.WriteHeader(hdr)
}
开发者ID:xgwang-zte,项目名称:origin,代码行数:11,代码来源:tar.go
示例18: MakeTarball
func MakeTarball(tarname string, fnames []string) error {
log.Printf("tarring %d entrires to %s ...", len(fnames), tarname)
tarfile, err := os.Create(tarname)
if err != nil {
return err
}
defer tarfile.Close()
var tarwriter *tar.Writer
if strings.HasSuffix(tarname, ".gz") {
zipper := gzip.NewWriter(tarfile)
defer zipper.Close()
tarwriter = tar.NewWriter(zipper)
/*
} else if strings.HasSuffix(tarname, ".xz") {
p := xz.WriterDefaults
p.DictCap = 1 << 24
zipper, err := xz.NewWriterParams(tarfile, &p) //xz.NewWriter(tarfile)
if err != nil {
return err
}
defer zipper.Close()
tarwriter = tar.NewWriter(zipper)
*/
} else {
tarwriter = tar.NewWriter(tarfile)
}
defer tarwriter.Close()
for _, fname := range fnames {
realm, ts, good := util.Parse_FName(fname)
if !good {
log.Printf("warning: skip ill-named file '%s'", fname)
continue // skip
}
data, err := util.Load(fname)
if err != nil {
return err
}
hdr := new(tar.Header)
hdr.Name = util.Make_FName(realm, ts, false)
hdr.Size = int64(len(data))
hdr.ModTime = ts
hdr.Mode = 0644
err = tarwriter.WriteHeader(hdr)
if err != nil {
return err
}
log.Printf("tar %d bytes for file %s", hdr.Size, hdr.Name)
_, err = tarwriter.Write(data)
if err != nil {
return err
}
}
log.Printf("%s tarred without errors", tarname)
return nil
}
开发者ID:gourytch,项目名称:gowowuction,代码行数:57,代码来源:backup.go
示例19: TarCompress
func TarCompress(source, target string) error {
fw, err := os.Create(target)
if err != nil {
return err
}
defer fw.Close()
// gzip write
gw := gzip.NewWriter(fw)
defer gw.Close()
// tar write
tw := tar.NewWriter(gw)
defer tw.Close()
dir, err := os.Open(source)
if err != nil {
return err
}
defer dir.Close()
fis, err := dir.Readdir(0)
if err != nil {
return err
}
for _, fi := range fis {
if fi.IsDir() {
continue
}
fmt.Println(fi.Name())
fr, err := os.Open(dir.Name() + "/" + fi.Name())
if err != nil {
return err
}
defer fr.Close()
h := new(tar.Header)
h.Name = fi.Name()
h.Size = fi.Size()
h.Mode = int64(fi.Mode())
h.ModTime = fi.ModTime()
err = tw.WriteHeader(h)
if err != nil {
return err
}
_, err = io.Copy(tw, fr)
if err != nil {
return err
}
}
return nil
}
开发者ID:rinosukmandityo,项目名称:toolkit,代码行数:56,代码来源:archive.go
示例20: ChanTar
func (t *Tar) ChanTar() {
defer sendBoolChan(t.CloseDone, true)
fw, err := os.Create(t.Dest)
if err != nil {
return
}
defer fw.Close()
// if t.Gz {
t.Gw = gzip.NewWriter(fw)
defer t.Gw.Close()
t.Tw = tar.NewWriter(t.Gw)
// }
defer t.Tw.Close()
dir, err := os.Open(t.OriPath)
if err != nil {
return
}
defer dir.Close()
t.Prepare.Done()
for {
var done bool = false
select {
case content := <-t.Ch:
header := new(tar.Header)
header.Name = content.FileName
header.Size = int64(len(content.Text))
header.Mode = content.Mode
if year, _, _ := content.ModTime.Date(); year == 1 {
header.ModTime = time.Now()
}
if err := t.Tw.WriteHeader(header); err != nil {
// todo error
fmt.Println(err.Error())
return
// break
}
if _, err := t.Tw.Write(content.Text); err != nil {
fmt.Println(err.Error())
return
// break
}
t.Job.Done()
case <-t.JobDone:
done = true
break
}
if done {
break
}
}
return
}
开发者ID:ianwoolf,项目名称:golib,代码行数:55,代码来源:tar.go