Skip to content

Commit 694fe27

Browse files
authored
Fix: RAR error on extract with files some special chars (Changed unrar to node-unrar-js)
1 parent 8501194 commit 694fe27

File tree

4 files changed

+55
-92
lines changed

4 files changed

+55
-92
lines changed

CHANGELOG.md

+1
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@ The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.0.0/),
1414
- Error opening some images [`8b97435`](https://github.com/ollm/OpenComic/commit/8b974356dfcbb7222bdef5ace604caeda93e4663)
1515
- Wrong cache folder in windows causing some bugs [`8b97435`](https://github.com/ollm/OpenComic/commit/dd6facaf67343185fa06b2377fdc64e66ad9090d)
1616
- Extract large RAR and ZIP files blocks the app for a while [`adbdced`](https://github.com/ollm/OpenComic/commit/adbdceda278e6184bc477581be9a25b8fc0f166b)
17+
- RAR error on extract with files some special chars (Changed unrar to node-unrar-js)
1718

1819
## [v1.0.0-beta.3](https://github.com/ollm/OpenComic/releases/tag/v1.0.0-beta.3) (09-10-2023)
1920

package.json

+1-1
Original file line numberDiff line numberDiff line change
@@ -80,7 +80,7 @@
8080
"sharp": "^0.30.7",
8181
"shosho": "^1.4.2",
8282
"tar-fs": "^3.0.4",
83-
"unrar": "^0.2.0",
83+
"node-unrar-js": "^2.0.0",
8484
"unzipper": "^0.10.14"
8585
},
8686
"devDependencies": {

scripts/app.js

+8
Original file line numberDiff line numberDiff line change
@@ -156,6 +156,13 @@ function sleep(ms)
156156
});
157157
}
158158

159+
function setImmediate(ms)
160+
{
161+
return new Promise(function(resolve){
162+
window.setImmediate(resolve);
163+
});
164+
}
165+
159166
module.exports = {
160167
event: event,
161168
eventOff: eventOff,
@@ -171,4 +178,5 @@ module.exports = {
171178
clientX: clientX,
172179
rand: rand,
173180
sleep: sleep,
181+
setImmediate: setImmediate,
174182
};

scripts/file-manager.js

+45-91
Original file line numberDiff line numberDiff line change
@@ -1350,19 +1350,9 @@ var fileCompressed = function(path, _realPath = false) {
13501350

13511351
if(this.rar) return this.rar;
13521352

1353-
if(unrar === false) unrar = require('unrar');
1353+
if(unrar === false) unrar = require('node-unrar-js');
13541354

1355-
let bin = false;
1356-
1357-
if(process.platform == 'win32' || process.platform == 'win64')
1358-
bin = asarToAsarUnpacked(p.join(appDir, 'unrar/UnRAR.exe'));
1359-
else if(process.platform == 'darwin')
1360-
bin = asarToAsarUnpacked(p.join(appDir, 'unrar/unrar_MacOSX_10.13.2_64bit'));
1361-
1362-
this.rar = new unrar({
1363-
path: this.realPath,
1364-
bin: bin,
1365-
});
1355+
this.rar = await unrar.createExtractorFromFile({filepath: this.realPath, targetPath: this.tmp});
13661356

13671357
return this.rar;
13681358

@@ -1375,36 +1365,30 @@ var fileCompressed = function(path, _realPath = false) {
13751365
console.time('readRar');
13761366
let _this = this;
13771367

1378-
let rar = await this.openRar();
1379-
1380-
return new Promise(function(resolve, reject) {
1381-
1382-
rar.list(async function(error, entries) {
1383-
1384-
if(!error)
1385-
{
1386-
for(let i = 0, len = entries.length; i < len; i++)
1387-
{
1388-
let entry = entries[i];
1389-
let name = _this.removeTmp(p.normalize(entry.name));
1390-
1391-
files.push({name: name, path: p.join(_this.path, name), folder: (entry.type === 'Directory' ? true : false)});
1392-
_this.setFileStatus(name, {extracted: false});
1393-
}
1368+
try
1369+
{
1370+
let rar = await this.openRar();
1371+
let list = rar.getFileList();
1372+
list = [...list.fileHeaders];
13941373

1395-
console.timeEnd('readRar');
1374+
for(let i = 0, len = list.length; i < len; i++)
1375+
{
1376+
let file = list[i];
1377+
let name = _this.removeTmp(p.normalize(file.name));
13961378

1397-
_this.files = _this.filesToMultidimension(files);
1398-
resolve(_this.files);
1399-
}
1400-
else
1401-
{
1402-
resolve(_this.readIfTypeFromBinaryIsDifferent(error));
1403-
}
1379+
files.push({name: name, path: p.join(_this.path, name), folder: !!file.flags.directory});
1380+
_this.setFileStatus(name, {extracted: false});
1381+
}
14041382

1405-
});
1383+
console.timeEnd('readRar');
14061384

1407-
});
1385+
_this.files = _this.filesToMultidimension(files);
1386+
return _this.files;
1387+
}
1388+
catch(error)
1389+
{
1390+
return _this.readIfTypeFromBinaryIsDifferent(error);
1391+
}
14081392

14091393
}
14101394

@@ -1417,68 +1401,38 @@ var fileCompressed = function(path, _realPath = false) {
14171401
let only = this.config.only;
14181402
let _this = this;
14191403

1420-
let rar = await this.openRar();
1421-
1422-
return new Promise(function(resolve, reject) {
1423-
1424-
rar.list(async function(error, entries) {
1425-
1426-
if(!error)
1427-
{
1428-
for(let i = 0, len = entries.length; i < len; i++)
1429-
{
1430-
let entry = entries[i];
1431-
let name = p.normalize(entry.name);
1432-
let extract = !only || only[name] ? true : false;
1433-
1434-
if(extract)
1435-
{
1436-
let path = p.join(_this.tmp, name);
1437-
let virtualPath = p.join(_this.path, name);
1438-
1439-
if(entry.type === 'Directory')
1440-
{
1441-
if(!fs.existsSync(path))
1442-
fs.mkdirSync(path);
1443-
}
1444-
else
1445-
{
1446-
let folderPath = _this.folderPath(path);
1447-
1448-
if(!fs.existsSync(folderPath))
1449-
fs.mkdirSync(folderPath, {recursive: true});
1450-
1451-
_this.setFileStatus(name, {extracted: true});
1404+
try
1405+
{
1406+
let rar = await this.openRar();
14521407

1453-
await new Promise(function(resolve, reject) {
1454-
rar.stream(name).on('error', reject).on('end', function() {
1408+
let regexp = new RegExp(pregQuote(p.sep, '/'), 'g');
14551409

1456-
_this.setProgress(_this.progressIndex++ / len);
1457-
_this.whenExtractFile(virtualPath);
1410+
for(let i = 0, len = this.config._only.length; i < len; i++)
1411+
{
1412+
let _name = this.config._only[i];
14581413

1459-
resolve();
1414+
let extracted = rar.extract({files: [_name.replace(regexp, '/')]});
1415+
extracted = [...extracted.files];
14601416

1461-
}).pipe(fs.createWriteStream(path));
1462-
});
1463-
}
1464-
}
1465-
}
1417+
await app.setImmediate();
14661418

1467-
_this.setProgress(1);
1419+
let virtualPath = p.join(this.path, _name);
14681420

1469-
console.timeEnd('extractRar');
1421+
this.setProgress(_this.progressIndex++ / len);
1422+
this.setFileStatus(_name, {extracted: true});
1423+
this.whenExtractFile(virtualPath);
1424+
}
14701425

1471-
resolve();
1472-
}
1473-
else
1474-
{
1475-
resolve(_this.extractIfTypeFromBinaryIsDifferent(error));
1476-
}
1426+
_this.setProgress(1);
14771427

1478-
});
1428+
console.timeEnd('extractRar');
14791429

1480-
});
1481-
1430+
return;
1431+
}
1432+
catch(error)
1433+
{
1434+
return _this.readIfTypeFromBinaryIsDifferent(error);
1435+
}
14821436
}
14831437

14841438

0 commit comments

Comments
 (0)