-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathzipUtils.js
52 lines (50 loc) · 1.88 KB
/
zipUtils.js
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
/**
* @author https://github.com/cford256
* Requires functions from the utils file.
*/
const path = require('path');
const AdmZip = require('adm-zip'); // https://github.com/cthackers/adm-zip/wiki/ADM-ZIP
const utils = require('./utils');
module.exports = {
getZip: (filepath) => new AdmZip(filepath),
getZipFiles: (filepath) => new AdmZip(filepath).getEntries(),
getZipAndFiles: (filepath) => {
const zip = module.exports.getZip(filepath);
return { zip, files: zip.getEntries() };
},
extractToAndRemoveSubfolder: (zipData, outputPath) => {
zipData.files.forEach((entry) => {
if (!entry.isDirectory) {
zipData.zip.extractEntryTo(entry.name, outputPath, /*maintainEntryPath*/ false, /*overwrite*/ true);
}
});
utils.runForEachFile(utils.getDirFilesInfo(outputPath), (file) => {
utils.renameFile(file.filepath, path.join(file.dirPath, utils.sanitizeFilename(file.filename)));
});
},
hasSubfolders: (zip) => {
let dirFound = false;
const files = zip.getEntries();
files.forEach((entry) => (entry.isDirectory ? (dirFound = true) : null));
return dirFound;
},
removeSubFolder: (filepath, outputPath = filepath) => {
const zip = new AdmZip(filepath);
const outZip = new AdmZip();
const files = zip.getEntries();
const dirFound = module.exports.hasSubfolders(zip);
if (dirFound) {
files.forEach((entry) => (!entry.isDirectory ? outZip.addFile(entry.name, entry.getData()) : null));
}
if (dirFound || outputPath !== filepath) outZip.writeZip(filepath);
},
createZipFile: (dirPath, outputPath) => {
const zip = new AdmZip();
const files = utils.getDirFilesInfo(dirPath);
files.forEach((file) => {
zip.addLocalFile(file.filepath);
});
if (outputPath) zip.writeZip(outputPath);
return { zip, files: zip.getEntries() };
},
};