mirror of
https://github.com/crazybber/awesome-patterns.git
synced 2024-11-29 00:25:20 +03:00
42 lines
645 B
Go
42 lines
645 B
Go
|
package main
|
||
|
|
||
|
import (
|
||
|
"compress/gzip"
|
||
|
"fmt"
|
||
|
"io"
|
||
|
"os"
|
||
|
"sync"
|
||
|
)
|
||
|
|
||
|
func main() {
|
||
|
var wg sync.WaitGroup
|
||
|
var i int = -1
|
||
|
var 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 {
|
||
|
// Unchanged from above
|
||
|
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
|
||
|
}
|