Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Browse files

correctly extract .dat files

  • Loading branch information...
commit 94bc7f748ca6dda692dccb2af1b9087229b3f57d 1 parent 8afbe54
@JeanSebTr authored
Showing with 45 additions and 22 deletions.
  1. +42 −22 extract.js
  2. +3 −0  package.json
View
64 extract.js
@@ -2,6 +2,8 @@
var fs = require('fs');
var path = require('path');
+var Step = require('step');
+
module.exports = function(argv) {
if(!argv.length) {
this.usage();
@@ -16,34 +18,51 @@ module.exports = function(argv) {
fs.readSync(fd, nBuf, 0, 4, 0);
var entBuf = new Buffer(nBuf.readUInt32LE(0)*4);
fs.readSync(fd, entBuf, 0, entBuf.length, 4);
- var fRead = 0;
+ var fRead = [];
for(var i=0; i<entBuf.length; i+=4) {
var offset = entBuf.readUInt32LE(i);
if(offset>0) {
- process.nextTick(readFileEntry.bind(null, filename, offset));
- fRead++;
+ fRead.push(wrap(filename, fd, offset));
}
}
- console.log('Going to extract %d files from %s...', fRead, filename);
+ console.log('Going to extract %d files from %s...', fRead.length, filename);
+ fRead.push(function(err) {
+ console.log('Success!');
+ });
+ Step.apply(Step, fRead);
}
-function readFileEntry(archive, offset) {
- fs.open(archive, 'r', function(err, fd) {
- // TODO cleaning + Error management ?
- var nBuf = new Buffer(8);
- fs.read(fd, nBuf, 0, 8, offset, function() {
- var fLength = nBuf.readUInt32LE(0);
- var nLength = nBuf.readUInt32LE(4);
- nBuf = new Buffer(nLength);
- fs.read(fd, nBuf, 0, nLength, offset+8, function() {
+function wrap(filename, fd, offset) {
+ return function(err) {
+ if(err) {
+ return console.error('Error processing archive %s :', filename, err);
+ }
+ else {
+ readFileEntry(filename, fd, offset, this);
+ }
+ }
+}
+
+function readFileEntry(filename, fd, offset, cb) {
+ fs.read(fd, new Buffer(8), 0, 8, offset, function(err, bytesRead, nBuf) {
+ if(err) {
+ return console.error('Error reading entry length:', err);
+ }
+ var fLength = nBuf.readUInt32LE(0);
+ var nLength = nBuf.readUInt32LE(4);
+ fs.read(fd, new Buffer(nLength), 0, nLength, offset+8,
+ function(err, bytesRead, nBuf) {
+ if(err) {
+ return console.log('Error reading entry name:', err);
+ }
process.nextTick(extractFileEntry.bind(null,
- nBuf.toString('utf8'), fd, offset+8+nLength, fLength));
- });
+ nBuf.toString('utf8'), filename, offset+8+nLength, fLength, cb));
});
});
}
-function extractFileEntry(filename, fd, from, length) {
+function extractFileEntry(filename, source, from, length, cb) {
+ console.log('Extracting %s from %s @ %d', filename, source, from);
var nParts = filename.split('/');
name = process.cwd();
for(var i=0; i<nParts.length-1; i++) {
@@ -52,13 +71,14 @@ function extractFileEntry(filename, fd, from, length) {
fs.mkdirSync(name);
}
}
- var r = fs.createReadStream(null, {
- fd: fd,
+ var r = fs.createReadStream(source, {
start: from,
end: from+length-1
});
- r.pipe(fs.createWriteStream(filename, {
- flags: 'w+'
- }));
- r.on('end', console.log.bind(console, '\tExtracted %s', filename));
+ r.pipe(
+ fs.createWriteStream(filename, { flags: 'w+' })
+ ).on('close', function() {
+ //console.log('\tExtracted %s', filename);
+ cb(null);
+ });
}
View
3  package.json
@@ -4,6 +4,9 @@
"author": "JeanSebTr <jeansebtr@xpensia.com>",
"description": "Tools for manipulating binary files",
"main": "index.js",
+ "dependencies": {
+ "step": "0.0.5"
+ },
"scripts": {
"test": "echo \"Error: no test specified\" && exit 1"
},
Please sign in to comment.
Something went wrong with that request. Please try again.