golang1.5 tar.gz 打包目录,代码分享

golang1.5 tar.gz 打包目录,代码分享,第1张

概述package mainimport ( "archive/tar" "compress/gzip" "fmt" "io" "io/ioutil" "os" "strings")func main() { fmt.Println(Gzip(".\\", "1.tar.gz"))}func Gzip(filepath, filename string) error {
package mainimport (	"archive/tar"	"compress/gzip"	"fmt"	"io"	"io/IoUtil"	"os"	"strings")func main() {	fmt.Println(Gzip(".\","1.tar.gz"))}func Gzip(filepath,filename string) error {	file,err := os.Create(filename)	if err != nil {		return err	}	defer file.Close()	gw := gzip.NewWriter(file)	defer gw.Close()	tw := tar.NewWriter(gw)	defer tw.Close()	return walk(filepath,tw)}func walk(path string,tw *tar.Writer) error {	path = strings.Replace(path,"\","/",-1)	info,err := IoUtil.ReadDir(path)	if err != nil {		return err	}	if !strings.HasSuffix(path,"/") {		path = path + "/"	}	index := strings.Index(path,"/")	List := strings.Join(strings.Split(path,"/")[index:],"/")	for _,v := range info {		if v.IsDir() {			head := tar.header{name: List + v.name(),Typeflag: tar.TypeDir,ModTime: v.ModTime()}			tw.Writeheader(&head)			walk(path+v.name(),tw)			continue		}		F,err := os.Open(path + v.name())		if err != nil {			fmt.Println("打开文件%s失败.",err)			continue		}		head := tar.header{name: List + v.name(),Size: v.Size(),Mode: int64(v.Mode()),ModTime: v.ModTime()}		tw.Writeheader(&head)		io.copy(tw,F)		F.Close()	}	return nil}
总结

以上是内存溢出为你收集整理的golang1.5 tar.gz 打包目录,代码分享全部内容,希望文章能够帮你解决golang1.5 tar.gz 打包目录,代码分享所遇到的程序开发问题。

如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。

欢迎分享,转载请注明来源:内存溢出

原文地址: http://outofmemory.cn/langs/1285096.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2022-06-09
下一篇 2022-06-09

发表评论

登录后才能评论

评论列表(0条)

保存