func swallow(self string, binary string, paths []string) error { sf, err := os.Open(self) if err != nil { return fmt.Errorf("open %s: %s", self, err) } hzp := binary + ".hzp" f, err := os.OpenFile(hzp, os.O_RDWR|os.O_CREATE, 0755) if err != nil { return fmt.Errorf("open %s: %s", hzp, err) } n, err := io.Copy(f, sf) if err != nil { sf.Close() return fmt.Errorf("copy %s->%s: %s", self, hzp, err) } if err := sf.Close(); err != nil { return fmt.Errorf("close %s: %s", self, err) } zw := patchzip.NewWriterAt(f, n) if err := swallowFile(zw, binary); err != nil { f.Close() return fmt.Errorf("swallow %s: %s", binary, err) } for _, path := range paths { if err := swallowFile(zw, path); err != nil { f.Close() return fmt.Errorf("swallow %s: %s", path, err) } } if err := zw.Close(); err != nil { return err } return f.Close() }
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 }