func NewWriterWithTemp(w io.Writer, tempDir string) (*Writer, error) { r := new(Writer) tf, err := ioutil.TempFile(tempDir, "torrentzip") if err != nil { return nil, err } r.tf = tf r.bf = bufio.NewWriter(r.tf) r.sink = w r.uw = czip.NewWriter(r.bf) return r, nil }
func main() { outpath := flag.String("out", "", "zip file") flag.Parse() file, err := os.Create(*outpath) if err != nil { fmt.Fprintf(os.Stderr, "creating zip file %s failed: %v\n", *outpath, err) os.Exit(1) } zw := czip.NewWriter(file) for _, name := range flag.Args() { if filepath.IsAbs(name) { fmt.Fprintf(os.Stderr, "cannot add absolute paths to a zip file: %s\n", name) os.Exit(1) } cf, err := os.Open(name) if err != nil { fmt.Fprintf(os.Stderr, "opening file %s failed: %v\n", name, err) os.Exit(1) } fh, err := zw.Create(name) if err != nil { fmt.Fprintf(os.Stderr, "creating zip entry for file %s failed: %v\n", name, err) os.Exit(1) } _, err = io.Copy(fh, cf) if err != nil { fmt.Fprintf(os.Stderr, "copying to zip entry for file %s failed: %v\n", name, err) os.Exit(1) } cf.Close() } zw.Close() file.Close() }
func main() { flag.Usage = usage help := flag.Bool("help", false, "show this message") version := flag.Bool("version", false, "show version") outpath := flag.String("out", "", "zip file") flag.Parse() if *help { flag.Usage() os.Exit(0) } if *version { fmt.Fprintf(os.Stdout, "%s version %s, Copyright (c) 2013 Uwe Hoffmann. All rights reserved.\n", os.Args[0], versionStr) os.Exit(0) } if *outpath == "" { flag.Usage() os.Exit(0) } file, err := os.Create(*outpath) if err != nil { fmt.Fprintf(os.Stderr, "creating zip file %s failed: %v\n", *outpath, err) os.Exit(1) } defer file.Close() bf := bufio.NewWriter(file) defer bf.Flush() hh := sha1.New() zw := czip.NewWriter(io.MultiWriter(bf, hh)) progress := pb.New(len(flag.Args()) + 1) progress.RefreshRate = 5 * time.Second progress.ShowCounters = false progress.Start() pwdName, err := os.Getwd() if err != nil { fmt.Fprintf(os.Stderr, "cannot establish current working directory %v\n", err) os.Exit(1) } av := &adderVisitor{ zw: zw, pwdName: pwdName, } for _, name := range flag.Args() { if filepath.IsAbs(name) { fmt.Fprintf(os.Stderr, "cannot add absolute paths to a zip file: %s\n", name) os.Exit(1) } av.skipFirstLevel = strings.HasSuffix(name, string(filepath.Separator)) err = filepath.Walk(name, av.visit) if err != nil { fmt.Fprintf(os.Stderr, "adding files from %s failed: %v\n", name, err) os.Exit(1) } progress.Increment() } err = zw.Close() if err != nil { fmt.Fprintf(os.Stderr, "failed to close zip file %s: %v\n", *outpath, err) os.Exit(1) } progress.Increment() progress.Finish() fmt.Fprintf(os.Stdout, "finished creating zip file: %s\n", *outpath) fmt.Fprintf(os.Stdout, "sha1 of created zip file: %s\n", hex.EncodeToString(hh.Sum(nil))) }