本文整理汇总了Golang中github.com/jhoonb/archivex.ZipFile.AddFile方法的典型用法代码示例。如果您正苦于以下问题:Golang ZipFile.AddFile方法的具体用法?Golang ZipFile.AddFile怎么用?Golang ZipFile.AddFile使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类github.com/jhoonb/archivex.ZipFile
的用法示例。
在下文中一共展示了ZipFile.AddFile方法的1个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Golang代码示例。
示例1: main
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
}
//.........这里部分代码省略.........