Browse Source

实现基本的解压操作

master
VIVIMAN 3 years ago
parent
commit
f46d5f659d
  1. 61
      golang_learn/data_func/go_to_zip/demo5/main.go

61
golang_learn/data_func/go_to_zip/demo5/main.go

@ -0,0 +1,61 @@
package main
import (
"github.com/yeka/zip"
"io/ioutil"
"log"
"os"
)
var pwd = `Qw^e1@10&++Hzb...?8`
func main() {
dirPath := `D:\hzb2021`
fileName := `2022122235133`
data := `data`
qggwy := `qggwy`
demo := `111`
fileType := `.gz`
os.Mkdir(dirPath+`\`+demo, os.ModePerm)
os.Mkdir(dirPath+`\`+fileName, os.ModePerm)
UnZip(dirPath+`\`+fileName+fileType, dirPath, pwd)
UnZip(dirPath+`\`+fileName+`\`+data+fileType, dirPath+`\`+demo, pwd)
UnZip(dirPath+`\`+fileName+`\`+qggwy+fileType, dirPath+`\`+demo, pwd)
// 移除文件
// os.Remove(dirPath + `\` + fileName + fileType)
os.Remove(dirPath + `\` + fileName + `\` + data + fileType)
os.Remove(dirPath + `\` + fileName + `\` + qggwy + fileType)
}
func UnZip(dst, src, pwd string) {
r, err := zip.OpenReader(dst)
if err != nil {
log.Fatal(err)
}
defer r.Close()
for _, f := range r.File {
if f.IsEncrypted() {
f.SetPassword(pwd)
}
r, err := f.Open()
if err != nil {
log.Fatal(err)
}
buf, err := ioutil.ReadAll(r)
if err != nil {
log.Fatal(err)
}
defer r.Close()
if f.FileInfo().IsDir() {
// Make Folder
os.MkdirAll(src+"/"+f.Name, os.ModePerm)
} else {
log.Print(`输出文件:`, f.FileHeader.Name)
ioutil.WriteFile(src+"/"+f.Name, buf, os.ModePerm)
}
}
}
Loading…
Cancel
Save