forked from go/golangs_learn
VIVIMAN
2 years ago
5 changed files with 103 additions and 2 deletions
@ -0,0 +1,9 @@ |
|||||
|
<?xml version="1.0" encoding="UTF-8" standalone="yes"?> |
||||
|
<assembly xmlns="urn:schemas-microsoft-com:asm.v1" manifestVersion="1.0"> |
||||
|
<assemblyIdentity version="1.0.0.0" processorArchitecture="*" name="SomeFunkyNameHere" type="win32"/> |
||||
|
<dependency> |
||||
|
<dependentAssembly> |
||||
|
<assemblyIdentity type="win32" name="Microsoft.Windows.Common-Controls" version="6.0.0.0" processorArchitecture="*" publicKeyToken="6595b64144ccf1df" language="*"/> |
||||
|
</dependentAssembly> |
||||
|
</dependency> |
||||
|
</assembly> |
@ -0,0 +1,91 @@ |
|||||
|
package main |
||||
|
|
||||
|
import ( |
||||
|
"archive/zip" |
||||
|
"io" |
||||
|
"log" |
||||
|
"net/http" |
||||
|
"os" |
||||
|
"path/filepath" |
||||
|
) |
||||
|
|
||||
|
func main() { |
||||
|
|
||||
|
if err := UnZip("help.7z", "./web/static/help"); err != nil { |
||||
|
log.Fatalln(err) |
||||
|
} |
||||
|
if err := UnZip("jsmap.7z", "./src/main/resources/static/jsmap"); err != nil { |
||||
|
log.Fatalln(err) |
||||
|
} |
||||
|
if err := UnZip("template.7z", "./src/main/resources/static/template"); err != nil { |
||||
|
log.Fatalln(err) |
||||
|
} |
||||
|
|
||||
|
} |
||||
|
|
||||
|
func getFile(url, fileName string) { |
||||
|
res, err := http.Get(url) |
||||
|
if err != nil { |
||||
|
panic(err) |
||||
|
} |
||||
|
f, err := os.Create(fileName) |
||||
|
if err != nil { |
||||
|
panic(err) |
||||
|
} |
||||
|
_, _ = io.Copy(f, res.Body) |
||||
|
} |
||||
|
|
||||
|
func UnZip(dst, src string) (err error) { |
||||
|
// 打开压缩文件,这个 zip 包有个方便的 ReadCloser 类型
|
||||
|
// 这个里面有个方便的 OpenReader 函数,可以比 tar 的时候省去一个打开文件的步骤
|
||||
|
zr, err := zip.OpenReader(src) |
||||
|
defer zr.Close() |
||||
|
if err != nil { |
||||
|
return |
||||
|
} |
||||
|
|
||||
|
// 如果解压后不是放在当前目录就按照保存目录去创建目录
|
||||
|
if dst != "" { |
||||
|
if err := os.MkdirAll(dst, 0755); err != nil { |
||||
|
return err |
||||
|
} |
||||
|
} |
||||
|
|
||||
|
// 遍历 zr ,将文件写入到磁盘
|
||||
|
for _, file := range zr.File { |
||||
|
path := filepath.Join(dst, file.Name) |
||||
|
|
||||
|
// 如果是目录,就创建目录
|
||||
|
if file.FileInfo().IsDir() { |
||||
|
if err := os.MkdirAll(path, file.Mode()); err != nil { |
||||
|
return err |
||||
|
} |
||||
|
// 因为是目录,跳过当前循环,因为后面都是文件的处理
|
||||
|
continue |
||||
|
} |
||||
|
|
||||
|
// 获取到 Reader
|
||||
|
fr, err := file.Open() |
||||
|
if err != nil { |
||||
|
return err |
||||
|
} |
||||
|
|
||||
|
// 创建要写出的文件对应的 Write
|
||||
|
fw, err := os.OpenFile(path, os.O_CREATE|os.O_RDWR|os.O_TRUNC, file.Mode()) |
||||
|
if err != nil { |
||||
|
return err |
||||
|
} |
||||
|
|
||||
|
_, err = io.Copy(fw, fr) |
||||
|
if err != nil { |
||||
|
return err |
||||
|
} |
||||
|
|
||||
|
// 因为是在循环中,无法使用 defer ,直接放在最后
|
||||
|
// 不过这样也有问题,当出现 err 的时候就不会执行这个了,
|
||||
|
// 可以把它单独放在一个函数中,这里是个实验,就这样了
|
||||
|
fw.Close() |
||||
|
fr.Close() |
||||
|
} |
||||
|
return nil |
||||
|
} |
After Width: | Height: | Size: 4.2 KiB |
Binary file not shown.
Loading…
Reference in new issue