compress files

This commit is contained in:
Jian Han 2018-01-01 22:07:44 +10:00
parent a216296557
commit 07112012a1
5 changed files with 46 additions and 0 deletions

View File

@ -0,0 +1,2 @@
example1

View File

@ -0,0 +1 @@
example2

View File

@ -0,0 +1 @@
example3

View File

@ -0,0 +1 @@
example4

View File

@ -0,0 +1,41 @@
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
}