To decompress a gzip file in Go, you can use the compress/gzip
package. Here's a step-by-step guide on how to achieve this:
import (
"compress/gzip"
"fmt"
"io"
"os"
)
file, err := os.Open("compressed_file.gzip")
if err != nil {
fmt.Println("Error opening file:", err)
return
}
defer file.Close()
gzipReader, err := gzip.NewReader(file)
if err != nil {
fmt.Println("Error creating gzip reader:", err)
return
}
defer gzipReader.Close()
outputFile, err := os.Create("decompressed_file.txt")
if err != nil {
fmt.Println("Error creating output file:", err)
return
}
defer outputFile.Close()
_, err = io.Copy(outputFile, gzipReader)
if err != nil {
fmt.Println("Error decompressing file:", err)
return
}
fmt.Println("File decompressed successfully.")
That's it! The above code will decompress the gzip file and save the decompressed data to the specified output file. Make sure to replace "compressed_file.gzip"
with the actual path of your gzip file, and "decompressed_file.txt"
with the desired output file path.