I made a text file, that I then compressed with gzip
. I then run the following go
program to read the contents of that compressed file.
package main
import (
"compress/gzip"
"fmt"
"os"
)
func main() {
handle, err := os.Open("zipfile.gz")
if err != nil {
fmt.Println("[ERROR] File Open:", err)
}
defer handle.Close()
zipReader, err := gzip.NewReader(handle)
if err != nil {
fmt.Println("[ERROR] New gzip reader:", err)
}
defer zipReader.Close()
var fileContents []byte
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)
fmt.Printf("[INFO] Uncompressed contents: '%s'
", fileContents)
}
The response that I get is the following:
$ go run zipRead.go
[INFO] Number of bytes read from the file: 0
[INFO] Uncompressed contents: ''
Why am I not getting any contents from the file?
I have created zip files on both OS X and Ubuntu. I have build this go
program on both OS X and Ubuntu with the same result.