func createZipFile(runRoot string, files []string) (string, error) { zipFd, err := ioutil.TempFile(runRoot, ".results_zip") if err != nil { return "", err } defer zipFd.Close() zipPath := zipFd.Name() zip := zip.NewWriter(zipFd) for _, file := range files { f, err := os.Open(path.Join(runRoot, file)) if err != nil { continue } defer f.Close() zf, err := zip.Create(file) if err != nil { zip.Close() return zipPath, err } if _, err := io.Copy(zf, f); err != nil { zip.Close() return zipPath, err } } return zipPath, zip.Close() }
func boomkat(name string, no_cog bool, no_delete bool) { usr, err := user.Current() if err != nil { log.Fatal(err) } dirname := path.Base(name) dirname = strings.Replace(dirname, "flac_", "", 1) dirname = strings.Replace(dirname, "mp3_", "", 1) dirname = strings.Replace(dirname, "_", " ", -1) dirname = dirname[:len(dirname)-4] dirname = strings.TrimRight(dirname, " ") path := usr.HomeDir + "/Music/" + dirname os.Mkdir(path, 0755) zip, err := zip.OpenReader(name) if err != nil { log.Fatal(err) } defer zip.Close() for _, file_in_zip := range zip.File { new_filename := strings.Replace(file_in_zip.Name, "_", " ", -1) if new_filename[1] == "-"[0] { new_filename = "0" + new_filename } log.Println(new_filename) file_path := path + "/" + new_filename file_handler_in_zip, err := file_in_zip.Open() if err != nil { log.Fatal(err) } defer file_handler_in_zip.Close() file_to_write, err := os.Create(file_path) if err != nil { log.Fatal(err) } defer file_to_write.Close() _, err = io.Copy(file_to_write, file_handler_in_zip) if err != nil { log.Fatal(err) } } if !no_delete { os.Remove(name) } if !no_cog { cmd := exec.Command("open", path, "-a", "cog") err := cmd.Start() if err != nil { log.Fatal(err) } err = cmd.Wait() } }
func Zip(zipFile, directory string) error { os.Mkdir(filepath.Dir(zipFile), 0755) zip, err := Create(zipFile) if err != nil { return err } err = zip.AddDirectory("./", directory) if err != nil { return err } err = zip.AddEntry("./.app_store.txt", filepath.Dir(filepath.Dir(zipFile))+"/../source/.app_store.txt") if err != nil { return err } err = zip.Close() if err != nil { return err } return nil }
func streamWorld(w http.ResponseWriter, r *http.Request, worldId string, filename string) { w.Header().Add("Content-Type", "application/zip") w.Header().Add("Content-Disposition", fmt.Sprintf("attachment; filename=%s.zip", filename)) hexId := bson.ObjectIdHex(worldId) url, err := readUrlForServer(hexId) if err != nil { fmt.Println("no url for world", hexId.Hex(), err) http.NotFound(w, r) } tempPath, err := createTempDir(worldId) if err != nil { fmt.Println("failed to create tempdir", err) http.Error(w, "failed to create tempdir", 500) } zip := zip.NewWriter(w) defer zip.Close() // Zip files as they are downloaded watcher := NewWatcher(tempPath, 5*time.Second) go watcher.Watch() var wg sync.WaitGroup wg.Add(1) go zipFilesAsTheyAppear(tempPath, filename, watcher.C, zip, &wg) // start the download err = restoreDir(url, tempPath) if err != nil { fmt.Println("failed to download archive", err) http.Error(w, "failed to download archive", 500) } watcher.Cancel() wg.Wait() }