node 复制文件的五种方式
const fs = require("fs");
const path = require("path");
// 方法1 直接使用原生的来
(async () => {
console.time('方式1')
const fileName = path.resolve(__dirname, './fsModule/复制文件1.docx');
const fileCont = await fs.promises.readFile(path.resolve(__dirname, './fsModule/100MB大文件.docx'), 'utf-8');
await fs.promises.writeFile(fileName, fileCont, {encoding: 'utf-8', flag: 'w'});
console.timeEnd('方式1') //方式1: 1318.309ms
})()
// 方法2 通过流的方法来读取
(() => {
console.time('用流的方式读取文件')
const fromFileName = path.resolve(__dirname, './fsModule/100MB大文件.docx');
const toFileName = path.resolve(__dirname, './fsModule/复制文件2.docx');
const rs = fs.createReadStream(fromFileName, {
autoClose: true,
encoding: 'utf-8',
highWaterMark: 64 * 1024 * 1024,
flags: 'r'
})
const ws = fs.createWriteStream(toFileName, {
encoding: 'utf-8',
flags: 'a',
highWaterMark: 16 * 1024 * 1024,
autoClose: true
})
rs.on('data', chunk => {
ws.write(chunk, 'utf-8');
})
rs.on('end', () => {
console.log('文件写入完毕!!')
})
console.timeEnd('用流的方式读取文件') //用流的方式读取文件: 4.218ms
})()
// 方式3 优化用流读取 解决背压问题
(() => {
console.time('优化用流的方式读取文件')
const fromFileName = path.resolve(__dirname, './fsModule/100MB大文件.docx');
const toFileName = path.resolve(__dirname, './fsModule/复制文件3.docx');
const rs = fs.createReadStream(fromFileName, {
autoClose: true,
encoding: 'utf-8',
highWaterMark: 64 * 1024 * 1024,
flags: 'r'
})
const ws = fs.createWriteStream(toFileName, {
encoding: 'utf-8',
flags: 'a',
highWaterMark: 16 * 1024 * 1024,
autoClose: true
})
rs.on('data', chunk => {
const wsFlag = ws.write(chunk, 'utf-8');
if (!wsFlag) {
rs.pause();
}
})
ws.on('drain', () => {
// 继续读取
rs.resume();
})
rs.on('end', () => {
console.log('文件写入完毕!!')
ws.end();
})
console.timeEnd('优化用流的方式读取文件') //用流的方式读取文件: 3.006ms
})()
// 方法四: pipe
(() => {
console.time('优化用流的方式读取文件')
const fromFileName = path.resolve(__dirname, './fsModule/100MB大文件.docx');
const toFileName = path.resolve(__dirname, './fsModule/复制文件4.docx');
const rs = fs.createReadStream(fromFileName, {
autoClose: true,
encoding: 'utf-8',
highWaterMark: 64 * 1024 * 1024,
flags: 'r'
})
const ws = fs.createWriteStream(toFileName, {
encoding: 'utf-8',
flags: 'a',
highWaterMark: 16 * 1024 * 1024,
autoClose: true
})
// 直接解决背压问题
rs.pipe(ws);
console.timeEnd('优化用流的方式读取文件') // 7.967ms
})()
// 方法五 copyFile
const fromFileName = path.resolve(__dirname, './fsModule/100MB大文件.docx');
const toFileName = path.resolve(__dirname, './fsModule/复制文件5.docx');
fs.copyFile(fromFileName, toFileName,0, ()=>{
console.log('复制完成')
})
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
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
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
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
上次更新: 2024/01/30, 00:35:17
- 02
- Node与GLIBC_2.27不兼容解决方案08-19
- 03
- Git清空本地文件跟踪缓存08-13