-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathrar.go
59 lines (48 loc) · 909 Bytes
/
rar.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
package zipman
import (
"fmt"
"io"
"os"
"path/filepath"
"github.com/nwaples/rardecode"
)
func ExtractRar(rarFilename, destFoldername string) error {
rarFile, err := os.Open(rarFilename)
if err != nil {
return err
}
defer rarFile.Close()
archive, err := rardecode.NewReader(rarFile, "")
if err != nil {
return err
}
err = os.MkdirAll(destFoldername, 0755)
if err != nil {
return err
}
for {
header, err := archive.Next()
if err == io.EOF {
break
}
if err != nil {
return err
}
destPath := filepath.Join(destFoldername, header.Name)
if header.IsDir {
os.MkdirAll(destPath, os.FileMode(header.Mode()))
continue
}
writer, err := os.Create(destPath)
if err != nil {
return err
}
defer writer.Close()
_, err = io.Copy(writer, archive)
if err != nil {
return err
}
}
fmt.Println("[SUCCESS] rar extracted successfully")
return nil
}