unzip 解压(还有其他工具也比较好用) https://github.com/EvanOxfeld/node-unzip jszip https://www.jsdelivr.com/package/npm/jszip epubjs https://www.jsdelivr.com/package/npm/epubjs 在移动端 打开4M大小的.epub文件需要35s左右,打开解压好的.opf只需要5s左右 在PC端 相差不远 demo: https://highlights.dev.yitong.com/epub2/list.html
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
| var fs = require('fs'); var path = require('path'); var unzip = require("unzip");
var inputPath = 'EPUBS/' var outputPath = 'src/' var filePath = path.resolve(__dirname + '/EPUBS');
fileDisplay(filePath);
var queue = []; var isUnziping = false;
function fileUnzip(_filePath){ fs.createReadStream(_filePath).pipe(unzip.Extract({ path: _filePath.replace('.epub', '').replace(inputPath, outputPath) })).on('close',function(res){ console.log(_filePath) queue.shift(); if(queue[0]){ fileUnzip(queue[0]); }else{ console.log('结束') } }); }
function fileDisplay(filePath){ fs.readdir(filePath,function(err,files){ if(err){ console.warn(err) }else{ files.forEach(function(filename){ var filedir = path.join(filePath,filename); fs.stat(filedir,function(eror,stats){ if(eror){ console.warn('获取文件stats失败'); }else{ var isFile = stats.isFile(); var isDir = stats.isDirectory(); if(isFile){ if(filedir.indexOf('.epub')>-1){ var _filePath = filedir.replace(__dirname, '').replace(/\\/g,"/").substring(1); queue.push(_filePath); if(!isUnziping){ isUnziping = true; fileUnzip(queue[0]) } } } if(isDir){ fileDisplay(filedir); } } }) }); } }); }
|