【问题标题】:golang unzip Response.Bodygolang 解压 Response.Body
【发布时间】:2018-11-05 10:31:50
【问题描述】:

我编写了一个小型网络爬虫,并且知道响应是一个 zip 文件。
在我有限的 golang 编程经验中,我只知道如何解压缩现有文件。
可以不提前将Response.Body解压到内存中吗?

【问题讨论】:

    标签: go zip archive


    【解决方案1】:

    正在更新内存中处理 Zip 文件响应正文的答案。

    注意:确保您有足够的内存来处理 zip 文件。

    package main
    
    import (
        "archive/zip"
        "bytes"
        "fmt"
        "io/ioutil"
        "log"
        "net/http"
    )
    
    func main() {
        resp, err := http.Get("zip file url")
        if err != nil {
            log.Fatal(err)
        }
        defer resp.Body.Close()
    
        body, err := ioutil.ReadAll(resp.Body)
        if err != nil {
            log.Fatal(err)
        }
    
        zipReader, err := zip.NewReader(bytes.NewReader(body), int64(len(body)))
        if err != nil {
            log.Fatal(err)
        }
    
        // Read all the files from zip archive
        for _, zipFile := range zipReader.File {
            fmt.Println("Reading file:", zipFile.Name)
            unzippedFileBytes, err := readZipFile(zipFile)
            if err != nil {
                log.Println(err)
                continue
            }
    
            _ = unzippedFileBytes // this is unzipped file bytes
        }
    }
    
    func readZipFile(zf *zip.File) ([]byte, error) {
        f, err := zf.Open()
        if err != nil {
            return nil, err
        }
        defer f.Close()
        return ioutil.ReadAll(f)
    }
    

    默认情况下,Go HTTP 客户端会自动处理 Gzip 响应。响应正文的典型读取和关闭也是如此。

    但是有一个问题。

    // Reference https://github.com/golang/go/blob/master/src/net/http/transport.go
    //
    // DisableCompression, if true, prevents the Transport from
    // requesting compression with an "Accept-Encoding: gzip"
    // request header when the Request contains no existing
    // Accept-Encoding value. If the Transport requests gzip on
    // its own and gets a gzipped response, it's transparently
    // decoded in the Response.Body. However, if the user
    // explicitly requested gzip it is not automatically
    // uncompressed.
    DisableCompression bool
    

    它的意思是;如果您在请求中手动添加标头Accept-Encoding: gzip,则您必须自己处理 Gzip 响应正文。

    例如-

    reader, err := gzip.NewReader(resp.Body)
    if err != nil {
        log.Fatal(err)
    }
    defer reader.Close()
    
    body, err := ioutil.ReadAll(reader)
    if err != nil {
        log.Fatal(err)
    }
    
    fmt.Println(string(body))
    

    【讨论】:

    • 感谢您的回答。我知道如何处理 gzip。但我不知道处理 .zip 文件。它可能需要archive/zip,但我不知道如何使用archive/zip unzip .zip Response.Body。 gzip 和 zip 一样吗?
    • 没有 gzip != zip。看来你要求.zip 文件,我的错。一会儿我会更新代码sn-p。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2019-03-13
    • 1970-01-01
    • 2013-12-19
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多