Extrahieren von files aus Tar-Balls
Format-Beschreibung siehe Wikipdia tar (computing)
Eigene Version (wolfk, 4/2015)
Haupt-Code:
this.processTarChunks = function(tarball) { // 'tarball' ist ein String mit dem Tar-Filetext
console.log("got "+tarball.length/512+" 512k blocks");
var offset = 0;
while (tarball.length >= offset + 512 && tarball.charCodeAt(offset) != 0) {
// Filename beginnt mit '0' -> Tarball-Ende
// ein Tar-ball besteht aus 512-byte blöcken: für jedes File (1*Header-block + n*Daten-blöcke mit dem Filetext) = 1 chunk
// als Ende ein Null-block
// das neue 'gtar' setzt immer noch einen Chunck "./PaxHeaders.31035..." davor (wozu??)
if (tarball.substr(offset,5) == "./Pax") offset += 1024; // überspringen
var file = this.parseChunk(tarball,offset);
this.files.push(file);
offset = file.nextoff;
console.log(file.filename+" "+file.filetext.length);
}
}
this.parseChunk = function(txt, offset) {
var f = {};
function readfield(p,n) { return txt.substr(p+offset,n).split("\0",1)[0]; }
f.filename = readfield(0,100);
var length = parseInt(readfield(124,12),8); // oktal!!
// alle anderen Felder (uid,gid,mtime,...,link, "utar",...) hier oB
f.filetext = txt.substr(offset+512, length); // Kopie anlegen
f.nextoff = offset + 512*(1+Math.ceil(length/512)); // offset für folg. Chunk
return f;
}
}
Beispiel mit einem 'tgz=tar.gz'-File (und 'jsxcompressor.js')
Extraktion des Files
Beispiel: Extraktion der png-flags aus 'flags-png.tgz' (in die Tabelle)
Version 'MultiFile.js' von Ilmari Heikkinen (c) 2010: Original funzt nicht!
(der Parameter 'length' wird nicht oktal konvertiert -> Parsieren versagt)
Test-Version (mit korr. length) funzt so:
MultiFile.load('potato-64.tar', function(xhr) {
this.files.forEach(function(f) { console.log(f.filename + " (" + f.length + " bytes)"); });
});