mirror of
https://github.com/crazybber/awesome-patterns.git
synced 2024-11-25 06:16:03 +03:00
compress files
This commit is contained in:
parent
a216296557
commit
07112012a1
2
concurrency/compressfiles/exampledata/example1.txt
Normal file
2
concurrency/compressfiles/exampledata/example1.txt
Normal file
@ -0,0 +1,2 @@
|
|||||||
|
|
||||||
|
example1
|
1
concurrency/compressfiles/exampledata/example2.txt
Normal file
1
concurrency/compressfiles/exampledata/example2.txt
Normal file
@ -0,0 +1 @@
|
|||||||
|
example2
|
1
concurrency/compressfiles/exampledata/example3.txt
Normal file
1
concurrency/compressfiles/exampledata/example3.txt
Normal file
@ -0,0 +1 @@
|
|||||||
|
example3
|
1
concurrency/compressfiles/exampledata/example4.txt
Normal file
1
concurrency/compressfiles/exampledata/example4.txt
Normal file
@ -0,0 +1 @@
|
|||||||
|
example4
|
41
concurrency/compressfiles/main.go
Normal file
41
concurrency/compressfiles/main.go
Normal 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
|
||||||
|
}
|
Loading…
Reference in New Issue
Block a user