Untar works with example
This commit is contained in:
parent
6c93bc1258
commit
f34c1964dd
2 changed files with 134 additions and 0 deletions
|
@ -7,6 +7,7 @@
|
|||
<script src='../lib/utils.js'></script>
|
||||
<script src='../lib/header.js'></script>
|
||||
<script src='../lib/tar.js'></script>
|
||||
<script src='../lib/untar.js'></script>
|
||||
|
||||
<script>
|
||||
(function () {
|
||||
|
@ -14,19 +15,35 @@
|
|||
|
||||
var Tar = require('tar'),
|
||||
utils = require('utils'),
|
||||
Untar = require('untar'),
|
||||
tape = new Tar(),
|
||||
out,
|
||||
url,
|
||||
base64;
|
||||
|
||||
function uint8ToString(buf) {
|
||||
var i, length, out = '';
|
||||
for (i = 0, length = buf.length; i < length; i += 1) {
|
||||
out += String.fromCharCode(buf[i]);
|
||||
}
|
||||
|
||||
return out;
|
||||
}
|
||||
|
||||
out = tape.append('output.txt', 'This is test1!');
|
||||
out = tape.append('dir/out.txt', 'This is test2! I changed up the directory');
|
||||
out = tape.append('arr.txt', utils.stringToUint8('This is a Uint8Array!'));
|
||||
|
||||
Untar(out, function (header, data) {
|
||||
var contents = uint8ToString(data);
|
||||
console.log(header.fileName, contents);
|
||||
});
|
||||
/*
|
||||
base64 = utils.uint8ToBase64(out);
|
||||
|
||||
url = "data:application/tar;base64," + base64;
|
||||
window.open(url);
|
||||
*/
|
||||
}());
|
||||
</script>
|
||||
</head>
|
||||
|
|
117
lib/untar.js
Normal file
117
lib/untar.js
Normal file
|
@ -0,0 +1,117 @@
|
|||
(function () {
|
||||
"use strict";
|
||||
|
||||
var recordSize = 512;
|
||||
|
||||
function removePadding(buf) {
|
||||
var text = '',
|
||||
i, length;
|
||||
|
||||
for (i = 0, length = buf.length; i < buf.length; i += 1) {
|
||||
text += String.fromCharCode(buf[i]);
|
||||
}
|
||||
|
||||
return parseInt(text.replace(/^0*/, ''), 8) || 0;
|
||||
}
|
||||
|
||||
function readString(buf) {
|
||||
var text = '',
|
||||
i, length;
|
||||
|
||||
for (i = 0, length = buf.length; i < length, buf[i] !== 0; i += 1) {
|
||||
text += String.fromCharCode(buf[i]);
|
||||
}
|
||||
|
||||
return text;
|
||||
}
|
||||
|
||||
function doHeader(buf) {
|
||||
var data = {}, offset = 0;
|
||||
|
||||
data.fileName = readString(buf.subarray(offset, offset + 100));
|
||||
offset += 100;
|
||||
|
||||
data.fileMode = removePadding(buf.subarray(offset, offset + 8));
|
||||
offset += 8;
|
||||
|
||||
data.uid = removePadding(buf.subarray(offset, offset + 8));
|
||||
offset += 8;
|
||||
|
||||
data.gid = removePadding(buf.subarray(offset, offset + 8));
|
||||
offset += 8;
|
||||
|
||||
data.fileSize = removePadding(buf.subarray(offset, offset + 12));
|
||||
offset += 12;
|
||||
|
||||
data.mtime = removePadding(buf.subarray(offset, offset + 12));
|
||||
offset += 12;
|
||||
|
||||
data.checksum = removePadding(buf.subarray(offset, offset + 8));
|
||||
offset += 8;
|
||||
|
||||
data.type = removePadding(buf.subarray(offset, offset + 1));
|
||||
offset += 1;
|
||||
|
||||
data.linkname = readString(buf.subarray(offset, offset + 100));
|
||||
offset += 100;
|
||||
|
||||
data.ustar = readString(buf.subarray(offset, offset + 8));
|
||||
if (/ustar/.test(data.ustar)) {
|
||||
offset += 8;
|
||||
|
||||
data.owner = readString(buf.subarray(offset, offset + 32));
|
||||
offset += 32;
|
||||
|
||||
data.group = readString(buf.subarray(offset, offset + 32));
|
||||
offset += 32;
|
||||
|
||||
data.majorNumber = removePadding(buf.subarray(offset, offset + 8));
|
||||
offset += 8;
|
||||
|
||||
data.minorNumber = removePadding(buf.subarray(offset, offset + 8));
|
||||
offset += 8;
|
||||
|
||||
data.filenamePrefix = readString(buf.subarray(offset, offset + 155));
|
||||
}
|
||||
|
||||
return data;
|
||||
}
|
||||
|
||||
function doFile(buf, cb) {
|
||||
var header, offset = 0, numBlocks, fileData;
|
||||
|
||||
header = doHeader(buf);
|
||||
|
||||
offset += recordSize;
|
||||
|
||||
numBlocks = Math.ceil(header.fileSize / recordSize);
|
||||
fileData = buf.subarray(offset, offset + header.fileSize);
|
||||
|
||||
offset += numBlocks * recordSize;
|
||||
|
||||
cb(header, fileData, offset);
|
||||
}
|
||||
|
||||
/*
|
||||
* Extract data from an input.
|
||||
*
|
||||
* @param input- must be a buffer
|
||||
* @param opts- object of options
|
||||
* @param cb- callback to call on each file- params: header, fileData (uint8Array)
|
||||
*/
|
||||
function Untar (input, cb) {
|
||||
var offset = 0;
|
||||
|
||||
while(input[offset]) {
|
||||
doFile(input.subarray(offset), function (header, fileData, iOffset) {
|
||||
offset += iOffset;
|
||||
|
||||
cb(header, fileData);
|
||||
});
|
||||
}
|
||||
}
|
||||
|
||||
module.exports = Untar;
|
||||
|
||||
provide('untar', module.exports);
|
||||
}());
|
Loading…
Reference in a new issue