Skip to content

Instantly share code, notes, and snippets.

@yinheli
Created December 24, 2015 10:00
Show Gist options
  • Save yinheli/3798bfe30b966b930218 to your computer and use it in GitHub Desktop.
Save yinheli/3798bfe30b966b930218 to your computer and use it in GitHub Desktop.
zip 打包文件, 向 zip 文件追加文件: 方式是创建新的, 拷贝旧的, 同时增加新的
// zip_file
package main
import (
"archive/zip"
"os"
"io"
)
func main() {
file, _ := os.OpenFile("/tmp/zip_file.zip", os.O_APPEND|os.O_WRONLY, 0600)
zw := zip.NewWriter(file)
defer zw.Close()
zf, _ := zw.Create("new_dir/content.txt")
zf.Write([]byte("content append"))
zw.Flush()
}
func copy() {
file, _ := os.Create("/tmp/zip_file_new.zip")
zr, _:= zip.OpenReader("/tmp/zip_file.zip")
zw := zip.NewWriter(file)
defer zr.Close()
defer zw.Close()
for _, f := range zr.File {
_w, _ := zw.Create(f.Name)
_r, _ := f.Open()
io.Copy(_w, _r)
}
zf, _ := zw.Create("dir_new/file_new.txt")
zf.Write([]byte("hello NEW world"))
zw.Flush()
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment