68 lines
1.0 KiB
Go
68 lines
1.0 KiB
Go
|
package main
|
||
|
|
||
|
import (
|
||
|
"compress/gzip"
|
||
|
"fmt"
|
||
|
"io"
|
||
|
"os"
|
||
|
"sync"
|
||
|
)
|
||
|
|
||
|
/*
|
||
|
@Ref: https://livebook.manning.com/book/go-in-practice/chapter-3/65
|
||
|
1. Practice gzip compress
|
||
|
2. How to use wait group
|
||
|
3. os.Args[1:] can stream in multiple files to the program without writing a walk function. Command: go run main.go ./testdata/*
|
||
|
|
||
|
*/
|
||
|
func main() {
|
||
|
|
||
|
// Single thread
|
||
|
// for _, file := range os.Args[1:] {
|
||
|
|
||
|
// err := compress(file)
|
||
|
// if err != nil {
|
||
|
// os.Exit(0)
|
||
|
// }
|
||
|
// }
|
||
|
|
||
|
// Multi threads
|
||
|
wg := sync.WaitGroup{}
|
||
|
var (
|
||
|
i int
|
||
|
file string
|
||
|
)
|
||
|
for i, file = range os.Args[1:] {
|
||
|
wg.Add(1)
|
||
|
go func(filename string) {
|
||
|
compress(filename)
|
||
|
wg.Done()
|
||
|
}(file)
|
||
|
}
|
||
|
|
||
|
wg.Wait()
|
||
|
fmt.Printf("Compressed %d files\n", i+1)
|
||
|
}
|
||
|
|
||
|
func compress(filename string) error {
|
||
|
in, err := os.Open(filename)
|
||
|
if err != nil {
|
||
|
return err
|
||
|
}
|
||
|
|
||
|
defer in.Close()
|
||
|
|
||
|
out, err := os.Create(filename + ".gz")
|
||
|
if err != nil {
|
||
|
return err
|
||
|
}
|
||
|
|
||
|
defer out.Close()
|
||
|
|
||
|
gzout := gzip.NewWriter(out)
|
||
|
_, err = io.Copy(gzout, in)
|
||
|
gzout.Close()
|
||
|
|
||
|
return err
|
||
|
}
|