-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathbzip2.go
71 lines (59 loc) · 1.52 KB
/
bzip2.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
package zipman
import (
"fmt"
"io"
"os"
"github.com/dsnet/compress/bzip2"
)
func CompressToBzip2(filename, compressedFilename string) error {
inputFile, err := os.Open(filename)
if err != nil {
fmt.Println("[ERROR] error opening file: ", filename)
return err
}
defer inputFile.Close()
compressedFile, err := os.Create(compressedFilename)
if err != nil {
fmt.Println("[ERROR] error creating file: ", filename)
return err
}
defer compressedFile.Close()
bz2Writer, err := bzip2.NewWriter(compressedFile, nil)
if err != nil {
fmt.Println("[ERROR] error creating bzip2 writer")
return err
}
defer bz2Writer.Close()
_, err = io.Copy(bz2Writer, inputFile)
if err != nil {
fmt.Println("[ERROR] error copying")
return err
}
fmt.Println("[SUCCESS] bzip2 compression completed succesfully for file: ", filename)
return nil
}
func ExtractFromBzip2(compressedFilename, extractedFilename string) error {
compressedFile, err := os.Open(compressedFilename)
if err != nil {
return err
}
defer compressedFile.Close()
bz2Reader, err := bzip2.NewReader(compressedFile, nil)
if err != nil {
fmt.Println("[ERROR] error creating bzip2 reader")
return err
}
extractedFile, err := os.Create(extractedFilename)
if err != nil {
fmt.Println("[ERROR] error creating extracted file")
return err
}
defer extractedFile.Close()
_, err = io.Copy(extractedFile, bz2Reader)
if err != nil {
fmt.Println("[ERROR] error copying")
return err
}
fmt.Println("[SUCCESS] bzip2 extraction completed successfully")
return nil
}