Golang - Compress a file Code Example

The code for Compress a file

package main
 
// Declare all the libraries needed
import (
    "bufio"
    "compress/gzip"
    "fmt"
    "io/ioutil"
    "os"
    "strings"
)
 
func main() {
    name_of_file := "sample.txt"

    f, _ := os.Open("C://ProgramData//" + name_of_file)
 
    read := bufio.NewReader(f)

    data, _ := ioutil.ReadAll(read)
 
    name_of_file = strings.Replace(name_of_file, ".txt", ".gz", -1)

    f, _ = os.Create("C://ProgramData//" + name_of_file)

    w := gzip.NewWriter(f)

    w.Write(data)
 
    w.Close()
}
Code by IncludeHelp, on February 28, 2023 15:58

Comments and Discussions!

Load comments ↻






Copyright © 2024 www.includehelp.com. All rights reserved.