func Zip(path string, dirs []string) (err error) { if IsZip(path) { return errors.New(path + " is already a zip file") } f, err := os.OpenFile(path, os.O_RDWR|os.O_CREATE|os.O_APPEND, 0644) if err != nil { return } defer f.Close() startoffset, err := f.Seek(0, os.SEEK_END) if err != nil { return } w := patch.NewWriterAt(f, startoffset) for _, dir := range dirs { err = filepath.Walk(dir, func(path string, info os.FileInfo, err error) error { if err != nil { return err } fh, err := patch.FileInfoHeader(info) if err != nil { return err } fh.Name = path p, err := w.CreateHeader(fh) if err != nil { return err } if !info.IsDir() { content, err := ioutil.ReadFile(path) if err != nil { return err } _, err = p.Write(content) if err != nil { return err } } return err }) } err = w.Close() return }
func swallowFile(zw *patchzip.Writer, path string) error { fmt.Printf(" archiving %s ...\n", path) f, err := os.Open(path) if err != nil { return fmt.Errorf("open %s: %s", path, err) } fi, err := f.Stat() if err != nil { return fmt.Errorf("stat %s: %s", path, err) } fh, err := patchzip.FileInfoHeader(fi) if err != nil { return fmt.Errorf("fileinfoheader %s: %s", path, err) } fh.Name = path w, err := zw.CreateHeader(fh) if err != nil { return fmt.Errorf("create header %s: %s", path, err) } if _, err := io.Copy(w, f); err != nil { return fmt.Errorf("archive %s: %s", path, err) } return nil }