golang zip 操作示例
2014-02-14 09:44
411 查看
下面的代码,来之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()
}
在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()
}
相关文章推荐
- golang连接mysql操作示例增删改查(已经测试)
- golang连接mysql操作示例增删改查
- apache ant进行zip解压缩操作示例分享
- golang针对map的判断,删除操作示例
- Python常见文件操作的函数示例
- Golang redis 入门操作
- Docker入门实践(六) 常用操作命令示例汇总
- python for data analysis 操作usagov_bitly_data示例
- golang:闲谈数据库操作
- 文件目录操作小示例
- AD帐户操作C#示例代码(二)——检查密码将过期的用户
- POI操作Excel示例
- SpringBoot中利用MyBatis进行数据操作的示例
- PHPExcel操作示例
- 推荐一个操作Zip文件的开源类库:DotNetZip (转)
- hbase做简单操作练习示例
- python ElementTree 基本读操作示例
- 使用golang写一个redis-cli的方法示例
- Python常见文件操作的函数示例
- js中的cookie的读写操作示例详解