-
Notifications
You must be signed in to change notification settings - Fork 14
/
Copy pathdemo.go
73 lines (69 loc) · 1.38 KB
/
demo.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
package main
import (
"crypto/md5"
"fmt"
"io"
"log"
"os"
)
const (
dir = "/home/sunlong/图片/"
)
func test(file *os.File) {
md5hash := md5.New()
if _, err := io.Copy(md5hash, file); err != nil {
log.Println(err.Error())
return
}
md5sum := md5hash.Sum(nil)
fmt.Println(md5sum, fmt.Sprintf("%x", md5sum))
}
//go1.11.linux-amd64.tar.gz
func main() {
file, err := os.Open(dir + "1527580104.jpg")
if err != nil {
fmt.Println(err.Error())
return
}
defer file.Close()
var b []byte
a, c := file.Read(b)
fmt.Println(a, b, c)
fileStat, err := file.Stat()
if err != nil {
fmt.Println(err.Error())
return
}
size := int64(16 << 20) // 16*2^20
blocks := make([][]byte, 0, 10)
offset := int64(0)
n := 0
fmt.Println(size)
for i := int64(0); i <= fileStat.Size()/size; i++ {
block := make([]byte, 0, size)
_, err := file.ReadAt(block, 0)
if err != nil {
log.Print(err)
return
}
fmt.Println(block)
blocks = append(blocks, block)
offset += size + 1
n++
}
writeFile, err := os.OpenFile(dir+"test.jpg", os.O_CREATE|os.O_RDWR|os.O_APPEND, 0644)
for i := 0; i < n; i++ {
_, err = writeFile.WriteAt(blocks[i], 0)
if err != nil {
log.Print(err)
return
}
}
defer writeFile.Close()
newFileStat, err := writeFile.Stat()
if err != nil {
log.Print(err)
return
}
fmt.Println(fileStat.Size(), fileStat.Name(), newFileStat.Name(), newFileStat.Size())
}