forked from zsakvo/dhbooker
-
Notifications
You must be signed in to change notification settings - Fork 0
/
files.go
75 lines (70 loc) · 1.35 KB
/
files.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
72
73
74
75
package main
import (
"fmt"
"io/ioutil"
"os"
"path/filepath"
)
func isFileExist(filePath string) bool {
_, err := os.Stat(filePath) //os.Stat获取文件信息
if err != nil {
if os.IsExist(err) {
return true
}
return false
}
return true
}
//合并缓存
func mergeTemp() {
var content []byte
if !isFileExist(path.out) {
os.MkdirAll(path.out, os.ModePerm)
}
outPath := path.out + pathSeparator + book.name + ".txt"
tmpPath := book.tmpPath
for _, cid := range book.chapterIDs {
d, err := ioutil.ReadFile(tmpPath + cid + ".txt")
if err == nil {
content = append(content, d...)
}
}
err1 := ioutil.WriteFile(outPath, content, 0644)
check(err1)
}
//移除缓存
func destoryTemp(b bool) error {
if b {
// fmt.Println("正在清理临时目录")
}
d, err := os.Open(path.tmp)
if err != nil {
return err
}
defer d.Close()
names, err := d.Readdirnames(-1)
if err != nil {
return err
}
for _, name := range names {
err = os.RemoveAll(filepath.Join(path.tmp, name))
if err != nil {
return err
}
}
if !b {
fmt.Println("下载完毕!")
}
return nil
}
//写出内容
func writeOut(content, dirPath, fileName string) error {
if !isFileExist(dirPath) {
err := os.MkdirAll(dirPath, os.ModePerm)
check(err)
}
outPath := dirPath + fileName
d := []byte(content)
err := ioutil.WriteFile(outPath, d, 0644)
return err
}