How can I use the “compress/gzip” package to gzip a file?

前端 未结 5 1247
长情又很酷
长情又很酷 2020-12-13 18:48

I\'m new to Go, and can\'t figure out how to use the compress/gzip package to my advantage. Basically, I just want to write something to a file, gzip it and rea

5条回答
  •  刺人心
    刺人心 (楼主)
    2020-12-13 18:57

    I decided to combine ideas from others answers and just provide a full example program. Obviously there are many different ways to do the same thing. This is just one way:

    package main
    
    import (
        "compress/gzip"
        "fmt"
        "io/ioutil"
        "os"
    )
    
    var zipFile = "zipfile.gz"
    
    func main() {
        writeZip()
        readZip()
    }
    
    func writeZip() {
        handle, err := openFile(zipFile)
        if err != nil {
            fmt.Println("[ERROR] Opening file:", err)
        }
    
        zipWriter, err := gzip.NewWriterLevel(handle, 9)
        if err != nil {
            fmt.Println("[ERROR] New gzip writer:", err)
        }
        numberOfBytesWritten, err := zipWriter.Write([]byte("Hello, World!\n"))
        if err != nil {
            fmt.Println("[ERROR] Writing:", err)
        }
        err = zipWriter.Close()
        if err != nil {
            fmt.Println("[ERROR] Closing zip writer:", err)
        }
        fmt.Println("[INFO] Number of bytes written:", numberOfBytesWritten)
    
        closeFile(handle)
    }
    
    func readZip() {
        handle, err := openFile(zipFile)
        if err != nil {
            fmt.Println("[ERROR] Opening file:", err)
        }
    
        zipReader, err := gzip.NewReader(handle)
        if err != nil {
            fmt.Println("[ERROR] New gzip reader:", err)
        }
        defer zipReader.Close()
    
        fileContents, err := ioutil.ReadAll(zipReader)
        if err != nil {
            fmt.Println("[ERROR] ReadAll:", err)
        }
    
        fmt.Printf("[INFO] Uncompressed contents: %s\n", fileContents)
    
        // ** Another way of reading the file **
        //
        // fileInfo, _ := handle.Stat()
        // fileContents := make([]byte, fileInfo.Size())
        // bytesRead, err := zipReader.Read(fileContents)
        // if err != nil {
        //     fmt.Println("[ERROR] Reading gzip file:", err)
        // }
        // fmt.Println("[INFO] Number of bytes read from the file:", bytesRead)
    
        closeFile(handle)
    }
    
    func openFile(fileToOpen string) (*os.File, error) {
        return os.OpenFile(fileToOpen, openFileOptions, openFilePermissions)
    }
    
    func closeFile(handle *os.File) {
        if handle == nil {
            return
        }
    
        err := handle.Close()
        if err != nil {
            fmt.Println("[ERROR] Closing file:", err)
        }
    }
    
    const openFileOptions int = os.O_CREATE | os.O_RDWR
    const openFilePermissions os.FileMode = 0660
    

    Having a full example like this should be helpful for future reference.

提交回复
热议问题