遇到一个情况,就是get请求了一个url,返回的直接是一个压缩包,如果浏览器直接访问那个url,那么就是把压缩文件下载下载下来了。
所以想用golang实现以下,将get请求获取的压缩包文件,解压缩,然后读取内容。写了个demo
package main
import (
"archive/zip"
"bytes"
"fmt"
"io"
"io/ioutil"
"net/http"
"os"
"path/filepath"
)
func main() {
var durl = "https://test.com.cn/data/spot/daily/test.zip"
resp,err := http.Get(durl)
if err != nil{
return
}
defer resp.Body.Close()
byt,_ := ioutil.ReadAll(resp.Body)
num := int64(len(byt))
zipReader,_ := zip.NewReader(bytes.NewReader(byt),num)
//var codeList []string
for _,_file := range zipReader.File{
fmt.Println(_file.Name)
f,_ := _file.Open()
desfile,err1 := os.OpenFile(_file.Name,os.O_CREATE|os.O_WRONLY,os.ModePerm)
if err1 == nil{
io.CopyN(desfile,f,int64(_file.UncompressedSize64))
desfile.Close()
}else{
defer desfile.Close()
}
//createZip(_file.Name)
}
//err := Zip("ETHUSDT-4h-2021-09-14.csv","test2.zip")
//if err != nil{
// fmt.Println(err)
//}
}
//压缩文件
func Zip(srcFile string, destZip string) error {
zipFile,err := os.Create(destZip)
if err !=nil {
return err
}
defer zipFile.Close()
archive := zip.NewWriter(zipFile)
defer archive.Close()
filepath.Walk(srcFile, func(path string, info os.FileInfo, err error) error {
if err !=nil{
return err
}
header,err := zip.FileInfoHeader(info)
if err !=nil{
return err
}
header.Name = path
if info.IsDir(){
header.Name += "/"
}else{
header.Method = zip.Deflate
}
writer,err := archive.CreateHeader(header)
if err !=nil{
return err
}
if !info.IsDir(){
file,err := os.Open(path)
if err !=nil{
return err
}
defer file.Close()
_,err = io.Copy(writer,file)
}
return err
})
return err
}
//解压缩
func Unzip(zipFile string,destDir string) error {
zipReader ,err :=zip.OpenReader(zipFile)
if err != nil{
return err
}
defer zipReader.Close()
for _,f := range zipReader.File{
fpath := filepath.Join(destDir,f.Name)
if f.FileInfo().IsDir(){
os.MkdirAll(fpath,os.ModePerm)
}else {
if err = os.MkdirAll(filepath.Dir(fpath),os.ModePerm);err !=nil{
return err
}
inFile,err := f.Open() //这个是从压缩文件读取出来的
if err !=nil{
return err
}
defer inFile.Close()
outFile,err := os.OpenFile(fpath,os.O_CREATE|os.O_WRONLY|os.O_TRUNC,f.Mode()) //创建的新文件
if err !=nil{
return err
}
defer outFile.Close()
_,err = io.Copy(outFile,inFile)
}
}
return err
}