func backupFiles(versionID string, wg *sync.WaitGroup) { defer wg.Done() if _, err := os.Stat(backupDir); err != nil { if os.IsNotExist(err) { makeBackupDir() } } if debug { fmt.Printf("backupFiles - %s\n", versionID) } log.Printf("backupFiles - %s\n", versionID) fileName := fmt.Sprintf("%s%s%s_backup", backupDir, string(os.PathSeparator), versionID) worldDir := fmt.Sprintf("%s", worldDir) if _, err := os.Stat(backupDir); err != nil { if os.IsNotExist(err) { if debug { fmt.Println("Nothing to backup right now.") } log.Println("Nothing to backup right now.") } else { zip := new(archivex.ZipFile) zip.Create(fileName) fmt.Printf("worldDir - %s\n", worldDir) fmt.Printf("fileName - %s\n", fileName) zip.AddAll(worldDir, true) fmt.Printf("after addall\n") zip.Close() } } }
func uploadProject(appInfo *apps.AppInfo, repoPath string) (*api.File, error) { // TODO: ignore files fileDir, err := ioutil.TempDir("", "leanengine") if err != nil { return nil, err } filePath := filepath.Join(fileDir, "leanengine.zip") println(filePath) log.Println("压缩项目文件 ...") zip := new(archivex.ZipFile) func() { defer zip.Close() zip.Create(filePath) zip.AddAll(repoPath, false) }() log.Println("上传项目文件 ...") client := api.NewKeyAuthClient(appInfo.AppID, appInfo.MasterKey) file, err := client.UploadFile(filePath) utils.CheckError(err) return file, nil }
func archiveGame(tempFile string, manifest *apipb.GameManifest) error { archive := new(archivex.ZipFile) err := archive.Create(tempFile) if err != nil { return err } p, _ := os.Getwd() err = addAllFiles(archive, manifest, p, false) if err != nil { return err } return archive.Close() }
func archive(dir string) { zip := new(archivex.ZipFile) pwd, err := os.Getwd() check(err) zip.Create(path.Join(pwd, "slackdump.zip")) zip.AddAll(dir, true) zip.Close() }
func archivePath(path string) (string, error) { var zip archivex.ZipFile var name string var tempPath string var err error tempPath, err = ioutil.TempDir("", "") if err != nil { return "", err } name = filepath.Base(path) tempPath = fmt.Sprintf("%s%s%s.zip", tempPath, string(os.PathSeparator), name) zip.Create(tempPath) zip.AddAll(path, false) zip.Close() return tempPath, nil }
func main() { //获取可执行文件所在的绝对路径 //不同于用户当前shell所在的目录 curDir, _ := osext.ExecutableFolder() fmt.Println("current directory :", curDir) //解析参数 version := flag.String("v", "0.0.1", "version number") dir := flag.String("d", "res,src", "directory to walk") server := flag.String("s", "http://192.168.0.134:8080/uploadfile", "web server address to upload") flag.Parse() //检查zip文件是否已存在 version_name := strings.Replace(*version, ".", "_", -1) if exist(version_name + ".zip") { panic(errors.New("already exist zip file : " + version_name + ".zip")) } fmt.Println("version number :", *version) fmt.Println("directory to walk :", *dir) fmt.Println("web server address to upload :", *server) //md5校验和表 checksum_table := make(map[string]string) f, err := os.OpenFile(path.Join(curDir, "CHECKSUM"), os.O_CREATE|os.O_APPEND|os.O_RDWR, 0660) if err != nil { panic(err) } //zip打包表 zip_table := make([]string, 0, 100) defer func() { json_data, err := json.Marshal(checksum_table) if err != nil { panic(err) } f.Close() ioutil.WriteFile(path.Join(curDir, "CHECKSUM"), json_data, 0660) if len(zip_table) != 0 { //打包 zip := new(archivex.ZipFile) zip.Create(path.Join(curDir, version_name+".zip")) for _, v := range zip_table { zip.AddFile(v) } zip.Close() //上传 extraParams := map[string]string{ "title": "Zip File", "author": "Jackie Sun", "description": "A zip file that generated by assets-builder", } request, err := newfileUploadRequest(*server, extraParams, "file", path.Join(curDir, version_name+".zip")) if err != nil { log.Fatal(err) } client := &http.Client{} resp, err := client.Do(request) if err != nil { log.Fatal(err) } else { body := &bytes.Buffer{} _, err := body.ReadFrom(resp.Body) if err != nil { log.Fatal(err) } resp.Body.Close() //fmt.Println(resp.StatusCode) //fmt.Println(resp.Header) fmt.Print(body) } } else { fmt.Println("nothing to zip") } }() //校验和文件源数据 checksum_data, err := ioutil.ReadAll(f) if len(checksum_data) != 0 { //fmt.Println("Unmarshal CHECKSUM") err = json.Unmarshal(checksum_data, &checksum_table) if err != nil { panic(err) } //fmt.Printf("%#v\n", checksum_table) } for _, v := range strings.Split(*dir, ",") { walkDir := path.Join(curDir, v) //fmt.Println("---------- Walking directory :", walkDir, "----------") filepath.Walk(walkDir, func(path string, f os.FileInfo, err error) error { if f == nil { fmt.Println("FileInfo of path:", path, "is nil") return err } if f.IsDir() { return nil } //fmt.Println(path) //读取文件数据 file_data, err := ioutil.ReadFile(path) if err != nil { panic(err) } //fmt.Println(file_data) //计算md5校验和 h := md5.New() h.Write(file_data) checksum := hex.EncodeToString(h.Sum(nil)) //fmt.Println(checksum) if v, ok := checksum_table[path]; ok { if v == checksum { return nil } else { zip_table = append(zip_table, strings.TrimPrefix(path, curDir+string(rune(filepath.Separator)))) } } else { zip_table = append(zip_table, strings.TrimPrefix(path, curDir+string(rune(filepath.Separator)))) } checksum_table[path] = checksum return nil }) //fmt.Println("---------- Leaving directory :", walkDir, "----------") } }