golang zip 操作示例

u011363248 · · 6280 次点击 · · 开始浏览    
这是一个创建于 的文章,其中的信息可能已经有所发展或是发生改变。

下面的代码,来之golang的示例源码,其中涉及的常见zip文件,修改了部分代码,

在ExampleWriter中添加了如下

fw, err := os.Create("/home/xxx/mem_sub/go/z.zip") if err != nil { //panic(err) fmt.Println(err) return } defer fw.Close()
屏蔽了以下部分

// Create a buffer to write our archive to. //	buf := new(bytes.Buffer)

使之能够创建看的见的zip文件


package main import ( "archive/zip" //	"bytes" "fmt" "io" "log" "os" ) func ExampleWriter() { fw, err := os.Create("/home/xxx/mem_sub/go/z.zip") if err != nil { //panic(err) fmt.Println(err) return } defer fw.Close() // Create a buffer to write our archive to. //	buf := new(bytes.Buffer) // Create a new zip archive. w := zip.NewWriter(fw) // Add some files to the archive. var files = []struct { Name, Body string }{ {"readme.txt", "This archive contains some text files."}, {"gopher.txt", "Gopher names:\nGeorge\nGeoffrey\nGonzo"}, {"todo.txt", "Get animal handling licence.\nWrite more examples."}, } for _, file := range files { f, err := w.Create(file.Name) if err != nil { log.Fatal(err) } _, err = f.Write([]byte(file.Body)) if err != nil { log.Fatal(err) } } // Make sure to check the error on Close. err = w.Close() if err != nil { log.Fatal(err) } } func ExampleReader() { // Open a zip archive for reading. r, err := zip.OpenReader("ss.zip") if err != nil { log.Fatal(err) } defer r.Close() // Iterate through the files in the archive, // printing some of their contents. for _, f := range r.File { fmt.Printf("Contents of %s:\n", f.Name) rc, err := f.Open() if err != nil { log.Fatal(err) } _, err = io.CopyN(os.Stdout, rc, 68) if err != nil { log.Fatal(err) } rc.Close() fmt.Println() } // Output: // Contents of README: // This is the source code repository for the Go programming language. } func main(){ ExampleWriter() //ExampleReader() }



有疑问加站长微信联系(非本文作者)

本文来自:CSDN博客

感谢作者:u011363248

查看原文:golang zip 操作示例

入群交流(和以上内容无关):加入Go大咖交流群,或添加微信:liuxiaoyan-s 备注:入群;或加QQ群:692541889

6280 次点击  
加入收藏 微博
暂无回复
添加一条新回复 (您需要 登录 后才能回复 没有账号 ?)
  • 请尽量让自己的回复能够对别人有帮助
  • 支持 Markdown 格式, **粗体**、~~删除线~~、`单行代码`
  • 支持 @ 本站用户;支持表情(输入 : 提示),见 Emoji cheat sheet
  • 图片支持拖拽、截图粘贴等方式上传