2 回答
TA贡献2011条经验 获得超2个赞
一种替代方法是使用内置的filepath.Walk函数
// root_directory has been set further up
walkFn := func(path string, info os.FileInfo, err error) error {
if info.Mode().IsDir() {
return nil
}
// Because of scoping we can reference the external root_directory variable
new_path := path[len(root_directory):]
if len(new_path) == 0 {
return nil
}
fr, err := os.Open(path)
if err != nil {
return err
}
defer fr.Close()
if h, err := tar.FileInfoHeader(info, new_path); err != nil {
log.Fatalln(err)
} else {
h.Name = new_path
if err = tw.WriteHeader(h); err != nil {
log.Fatalln(err)
}
}
if length, err := io.Copy( tw, fr ); err != nil {
log.Fatalln(err)
} else {
fmt.Println(length)
}
return nil
}
if err = filepath.Walk(root_directory, walkFn); err != nil {
return err
}
- 2 回答
- 0 关注
- 246 浏览
添加回答
举报