Repository navigation
Expand file tree
/
Copy pathindex.js
More file actions
144 lines (116 loc) · 3.86 KB
/
Copy pathindex.js
File metadata and controls
144 lines (116 loc) · 3.86 KB
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
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
const fs = require('fs');
const path = require('path');
const crypto = require('crypto');
const AdmZip = require('adm-zip');
async function getFileHash(filePath) {
const hashSum = crypto.createHash('sha256');
const stream = fs.createReadStream(filePath);
return new Promise((resolve, reject) => {
stream.on('data', (chunk) => {
hashSum.update(chunk);
});
stream.on('end', () => {
resolve(hashSum.digest('hex'));
});
stream.on('error', reject);
});
}
async function isSameFile(sourcePath, destPath) {
try {
const sourceStat = await fs.promises.stat(sourcePath);
const destStat = await fs.promises.stat(destPath);
if (sourceStat.size !== destStat.size) {
return false;
}
const sourceHash = await getFileHash(sourcePath);
const destHash = await getFileHash(destPath);
return sourceHash === destHash;
} catch (err) {
return false;
}
}
async function copyFile(sourcePath, destPath) {
const destDir = path.dirname(destPath);
if (!fs.existsSync(destDir)) {
fs.mkdirSync(destDir, { recursive: true });
}
if (fs.existsSync(destPath) && await isSameFile(sourcePath, destPath)) {
console.log(`Skipping ${sourcePath} -> ${destPath} (already exists and identical)`);
return;
}
await fs.promises.copyFile(sourcePath, destPath);
console.log(`Copied ${sourcePath} -> ${destPath}`);
}
async function copyDirectory(sourceDir, destDir) {
if (!fs.existsSync(destDir)) {
fs.mkdirSync(destDir, { recursive: true });
}
const items = fs.readdirSync(sourceDir);
for (const item of items) {
const sourcePath = path.join(sourceDir, item);
const destPath = path.join(destDir, item);
const stat = fs.statSync(sourcePath);
if (stat.isDirectory()) {
await copyDirectory(sourcePath, destPath);
} else {
await copyFile(sourcePath, destPath);
}
}
}
async function unzip(sourceZip, destDir) {
const zip = new AdmZip(sourceZip);
const entries = zip.getEntries();
for (const entry of entries) {
if (entry.isDirectory) continue;
const destPath = path.join(destDir, entry.entryName);
const destDirPath = path.dirname(destPath);
if (!fs.existsSync(destDirPath)) {
fs.mkdirSync(destDirPath, { recursive: true });
}
if (fs.existsSync(destPath)) {
// ZIP 파일의 해시 계산 (entry.getData() 사용, 큰 파일 주의)
const zipData = entry.getData();
const zipHash = crypto.createHash('sha256').update(zipData).digest('hex');
const destHash = await getFileHash(destPath);
if (zipHash === destHash) {
console.log(`Skipping ${entry.entryName} (already exists and identical)`);
continue;
}
}
// ZIP 데이터를 직접 파일로 쓰기
const zipData = entry.getData();
await fs.promises.writeFile(destPath, zipData);
console.log(`Extracted ${entry.entryName} -> ${destPath}`);
}
}
async function main() {
const args = process.argv.slice(2);
if (args.length !== 2) {
console.error('Usage: node index.js <source> <destination>');
process.exit(1);
}
const source = args[0];
const destination = args[1];
if (!fs.existsSync(source)) {
console.error(`Source ${source} does not exist`);
process.exit(1);
}
if (path.extname(source).toLowerCase() === '.zip') {
await unzip(source, destination);
} else {
const sourceStat = fs.statSync(source);
if (sourceStat.isDirectory()) {
await copyDirectory(source, destination);
} else {
const destPath = path.isAbsolute(destination) ? destination : path.join(process.cwd(), destination);
if (fs.existsSync(destPath) && fs.statSync(destPath).isDirectory()) {
const fileName = path.basename(source);
await copyFile(source, path.join(destPath, fileName));
} else {
await copyFile(source, destPath);
}
}
}
console.log('Copy operation completed.');
}
main();