Compare commits
3 Commits
Author | SHA1 | Date |
---|---|---|
Jameson Little | f6cc4e2b9c | |
Jameson Little | 56aeaf729b | |
Jameson Little | f34c1964dd |
|
@ -17,14 +17,14 @@ Dependencies
|
|||
|
||||
Tar needs an HTML5 compliant browser. More specifically it needs `Uint8Array` to work.
|
||||
|
||||
The examples depend on pakmanager, a package manager for the browser to make code written for node run in the browser. Install it as such:
|
||||
The only external module is require-kiss, which makes browser JS much more Node-like.
|
||||
|
||||
`pakmanager build`
|
||||
This module can be installed from npm (`npm install require-kiss`) or directly downloaded from github (https://github.com/coolaj86/require-kiss-js).
|
||||
|
||||
Usage Guide
|
||||
===========
|
||||
|
||||
The easiest way to interface with it is by using pakmanager. Include the package from pakmanager in your html, and then in you javascript:
|
||||
In your HTML file, make sure that require-kiss is included first. Then, to use it, do something like this:
|
||||
|
||||
var Tar = require('tar'),
|
||||
tape = new Tar();
|
||||
|
|
|
@ -3,7 +3,50 @@
|
|||
<head>
|
||||
<title>Browser Tar</title>
|
||||
|
||||
<script src='./pakmanaged.js'></script>
|
||||
<script src='../node_modules/require-kiss/require-kiss.js'></script>
|
||||
<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 () {
|
||||
"use strict";
|
||||
|
||||
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 = btoa(uint8ToString(out));
|
||||
|
||||
url = "data:application/tar;base64," + base64;
|
||||
window.open(url);
|
||||
*/
|
||||
}());
|
||||
</script>
|
||||
</head>
|
||||
|
||||
<body>
|
||||
|
|
|
@ -1,38 +0,0 @@
|
|||
(function () {
|
||||
"use strict";
|
||||
|
||||
var Tar = require('tar-js'),
|
||||
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;
|
||||
}
|
||||
|
||||
function stringToUint8 (input) {
|
||||
var out = new Uint8Array(input.length), i;
|
||||
|
||||
for (i = 0; i < input.length; i += 1) {
|
||||
out[i] = input.charCodeAt(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', stringToUint8('This is a Uint8Array!'));
|
||||
|
||||
base64 = btoa(uint8ToString(out));
|
||||
|
||||
url = "data:application/tar;base64," + base64;
|
||||
window.open(url);
|
||||
}());
|
|
@ -1,10 +0,0 @@
|
|||
{
|
||||
"author": "",
|
||||
"name": "tar-example",
|
||||
"engines": {
|
||||
"node": "~0.4.7"
|
||||
},
|
||||
"main": "./main.js",
|
||||
"dependencies": {
|
||||
}
|
||||
}
|
|
@ -6,6 +6,8 @@
|
|||
(function () {
|
||||
"use strict";
|
||||
|
||||
require('require-kiss');
|
||||
|
||||
/*
|
||||
struct posix_header { // byte offset
|
||||
char name[100]; // 0
|
||||
|
@ -122,4 +124,6 @@ struct posix_header { // byte offset
|
|||
|
||||
module.exports.structure = headerFormat;
|
||||
module.exports.format = formatHeader;
|
||||
|
||||
provide('header', module.exports);
|
||||
}());
|
||||
|
|
13
lib/tar.js
13
lib/tar.js
|
@ -6,6 +6,8 @@
|
|||
(function () {
|
||||
"use strict";
|
||||
|
||||
require('require-kiss');
|
||||
|
||||
var header = require("./header"),
|
||||
utils = require("./utils"),
|
||||
recordSize = 512,
|
||||
|
@ -78,10 +80,9 @@
|
|||
|
||||
this.written += headerArr.length;
|
||||
|
||||
// If there is not enough space in this.out, we need to expand it to
|
||||
// fit the new input.
|
||||
if (this.written + input.length > this.out.length) {
|
||||
this.out = utils.extend(this.out, this.written, input.length, blockSize);
|
||||
// this makes sense if the input is greater than 512 bytes
|
||||
if (headerArr.length + input.length > this.out.length) {
|
||||
this.out = utils.extend(out, headerArr.length, input.length, blockSize);
|
||||
}
|
||||
|
||||
this.out.set(input, this.written);
|
||||
|
@ -91,7 +92,7 @@
|
|||
|
||||
// make sure there's at least 2 empty records worth of extra space
|
||||
if (this.out.length - this.written < recordSize * 2) {
|
||||
this.out = utils.extend(this.out, this.written, recordSize * 2, blockSize);
|
||||
this.out = utils.extend(out, this.written, recordSize * 2, blockSize);
|
||||
}
|
||||
|
||||
if (typeof callback === 'function') {
|
||||
|
@ -107,4 +108,6 @@
|
|||
};
|
||||
|
||||
module.exports = Tar;
|
||||
|
||||
provide('tar', module.exports);
|
||||
}());
|
||||
|
|
|
@ -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);
|
||||
}());
|
|
@ -6,6 +6,8 @@
|
|||
(function () {
|
||||
"use strict";
|
||||
|
||||
require('require-kiss');
|
||||
|
||||
var lookup = [
|
||||
'A', 'B', 'C', 'D', 'E', 'F', 'G', 'H',
|
||||
'I', 'J', 'K', 'L', 'M', 'N', 'O', 'P',
|
||||
|
@ -88,4 +90,6 @@
|
|||
module.exports.extend = extend;
|
||||
module.exports.stringToUint8 = stringToUint8;
|
||||
module.exports.uint8ToBase64 = uint8ToBase64;
|
||||
|
||||
provide('utils');
|
||||
}());
|
||||
|
|
|
@ -1,7 +1,7 @@
|
|||
{
|
||||
"name": "tar-js",
|
||||
"description": "Tar implemented in the browser",
|
||||
"version": "0.2.0",
|
||||
"version": "0.1.1",
|
||||
"homepage": "http://github.com/beatgammit/tar-js",
|
||||
"repository": {
|
||||
"type": "git",
|
||||
|
@ -14,5 +14,6 @@
|
|||
"lib": "lib"
|
||||
},
|
||||
"dependencies": {
|
||||
"require-kiss": "*"
|
||||
}
|
||||
}
|
||||
|
|
Loading…
Reference in New Issue