读取写入tar/zip文件(go语言)
来源:互联网 发布:linux与网银 编辑:程序博客网 时间:2024/06/07 08:51
1.读取写入tar文件
package mainimport ( "archive/tar" "fmt" "os" "bufio" "io")func readFile(path string) string{ var content string = "" file,err := os.Open(path) if err != nil{ fmt.Printf("%v",err) return "" } defer file.Close() inputReader := bufio.NewReader(file) for{ inputString,err := inputReader.ReadString('\n') content += inputString if err == io.EOF{ break } } return content}func tarPackage(tarFileName string,tarFileList []string,rootPath string){ fw,err := os.Create(rootPath+"/"+tarFileName) if err != nil{ fmt.Printf("%v",err) return } defer fw.Close() tw := tar.NewWriter(fw) defer tw.Close() for _,fileItem := range tarFileList{ body := readFile(rootPath+"/"+fileItem) hdr := &tar.Header{ Name:fileItem, Mode:0600, Size:int64(len(body)), } if err := tw.WriteHeader(hdr);err != nil{ fmt.Printf("%v",err) return } if _,err := tw.Write([]byte(body));err != nil{ fmt.Printf("%v",err) return } } fmt.Println("done!")}func tarReader(tarFileName string){ fw,err := os.Open(tarFileName) if err != nil{ fmt.Printf("%v",err) return } defer fw.Close() tr := tar.NewReader(fw) for{ hdr,err := tr.Next() if err == io.EOF{ break } if err != nil{ fmt.Printf("%v",err) continue } fmt.Println("filename:"+hdr.Name) }}func main(){ tarFileName := "test.tar" //tarFileList := []string{"1","2","3"} rootPath := "/home/pijing/goworkspace/gocode" //tarPackage(tarFileName,tarFileList,rootPath) tarReader(rootPath+"/"+tarFileName)}
2.读取写入zip文件
package mainimport ( "io" "archive/zip" "fmt" "os" "bufio")func readFile(path string) string{ var content string = "" file,err := os.Open(path) if err != nil{ fmt.Printf("%v",err) return "" } defer file.Close() inputReader := bufio.NewReader(file) for{ inputString,err := inputReader.ReadString('\n') content += inputString if err == io.EOF{ break } } return content}func zipPackage(zipFileName string,zipFileList []string,rootPath string){ fw,err := os.Create(rootPath+"/"+zipFileName) if err != nil{ fmt.Printf("%v",err) return } defer fw.Close() zw := zip.NewWriter(fw) defer zw.Close() for _,fileItem := range zipFileList{ body := readFile(rootPath+"/"+fileItem) f,err := zw.Create(fileItem) if err != nil{ fmt.Printf("%v",err) continue } _,err1 := f.Write([]byte(body)) if err1 != nil{ fmt.Printf("%v",err1) continue } } fmt.Println("done!")}func zipReader(zipFileName string){ r,err := zip.OpenReader(zipFileName) if err != nil{ fmt.Printf("%v",err) return } defer r.Close() for _,f := range r.File{ fmt.Println("file:"+f.Name) }}func main(){ zipFileName := "test.zip" // zipFileList := []string{"1","2","3"} rootPath := "/home/pijing/goworkspace/gocode" // zipPackage(zipFileName,zipFileList,rootPath) zipReader(rootPath+"/"+zipFileName)}
0 0
- 读取写入tar/zip文件(go语言)
- go 语言zip压缩包 读取
- zip,jar,tar.gz无需解压读取文件内容
- Go语言zip解压.
- c语言 读取文件内容 文件写入
- c语言读取文件,写入文件
- 【C语言】文件读取写入信息(二进制方式)
- Golang(Go语言)读取文件基本用法
- Go语言_Go_Golang 逐行读取文件
- Go语言_Go_Golang 逐行读取文件
- JAVA中创建ZIP文件,并向ZIP文件中写入并读取条目和数据
- go 按行读取/写入
- python(文件读取写入)
- zip,jar,tar文件解压
- Ubuntu解压缩zip,tar,tar.gz,tar.bz2文件
- Ubuntu 解压缩zip,tar,tar.gz,tar.bz2文件
- C语言逐行读取文件内容,写入另外一个文件
- R语言 读取文件,一行一行处理, 写入文件
- 设计模式之分析和需求的概念
- Apache ant详解
- Oracle中CHAR类型不足位数时,用空格自动补足位数
- Android中SwipeRefreshLayout和ViewPager左右滑动冲突的原因以及正确的解决方法
- 《Redis官方文档》Redis集群教程
- 读取写入tar/zip文件(go语言)
- hdoj 5625 Clarke and chemistry
- 页面设计一般是叠加而不是拼接
- DB2中自动清理数据库历史文件
- iOS的DeviceToken随生产环境和开发环境变化
- spark学习笔记
- Missing iOS Distribution signing identity for …, 在打包的时候发现证书过期了。
- tinyxml2解析XML文件
- 工作日志-16/2/16