通过接口实现压缩文件、目录,修正遍历方式,未添加排除文件 *** 作

通过接口实现压缩文件、目录,修正遍历方式,未添加排除文件 *** 作,第1张

概述package zipimport ( "io" "os" "path/filepath" "strings")type Compress interface { Close() error WriteHead(path string, info os.FileInfo) error Write(p []byte) (int, error)}func walk(pat
package zipimport (	"io"	"os"	"path/filepath"	"strings")type Compress interface {	Close() error	Writehead(path string,info os.fileInfo) error	Write(p []byte) (int,error)}func walk(path string,compresser Compress) error {	info,err := os.Lstat(path)	if err != nil {		return err	}	var baseDir string	if info.IsDir() {		baseDir = filepath.Base(path)	}	filepath.Walk(path,func(root string,info os.fileInfo,err error) error {		if err != nil {			return err		}		root = strings.Replace(root,"\","/",-1)		fileroot := root		if root = strings.TrimPrefix(root,path); root == "" {			root = baseDir		}		err = compresser.Writehead(root,info)		if err != nil {			return nil		}		F,err := os.Open(fileroot)		if err != nil {			return nil		}		io.copy(compresser,F)		F.Close()		return nil	})	return nil}


package zipimport (	"archive/tar"	"compress/gzip"	"os")type TgzWirter struct {	tar *tar.Writer	gz  *gzip.Writer}func NewTgzWirter(file *os.file) *TgzWirter {	gw := gzip.NewWriter(file)	tw := tar.NewWriter(gw)	return &TgzWirter{tw,gw}}func (self *TgzWirter) Close() error {	self.tar.Close()	return self.gz.Close()}func (self *TgzWirter) Writehead(path string,info os.fileInfo) error {	if path == "." || path == ".." {		return nil	}	head,err := tar.fileInfoheader(info,"")	if err != nil {		return err	}	head.name = path	if info.IsDir() {		head.name += "/"	}	return self.tar.Writeheader(head)}func (self *TgzWirter) Write(p []byte) (int,error) {	return self.tar.Write(p)}func (self *TgzWirter) Walk(source string) error {	return walk(source,self)}


package zipimport (	"archive/zip"	"io"	"os"	"time")func NewZipwriter(file *os.file) *Zipwrite {	zipwrite := zip.NewWriter(file)	return &Zipwrite{zone: 8,zw: zipwrite,file: file}}type Zipwrite struct {	zone   int64	zw     *zip.Writer	writer io.Writer	file   *os.file}func (self *Zipwrite) Close() error {	return self.zw.Close()}func (self *Zipwrite) Writehead(path string,err := zip.fileInfoheader(info)	if err != nil {		return err	}	if !info.IsDir() {		head.Method = zip.Deflate	}	head.name = path	if info.IsDir() {		head.name += "/"	}	head.SetModTime(time.Unix(info.ModTime().Unix()+self.zone*60*60,0))	write,err := self.zw.Createheader(head)	if err != nil {		return err	}	self.writer = write	return nil}func (self *Zipwrite) Write(p []byte) (int,error) {	return self.writer.Write(p)}func (self *Zipwrite) Walk(source string) error {	return walk(source,self)}
总结

以上是内存溢出为你收集整理的通过接口实现压缩文件、目录,修正遍历方式,未添加排除文件 *** 作全部内容,希望文章能够帮你解决通过接口实现压缩文件、目录,修正遍历方式,未添加排除文件 *** 作所遇到的程序开发问题。

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

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

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

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

发表评论

登录后才能评论

评论列表(0条)

保存