2018-05-21 20:23:05 +02:00
|
|
|
/**
|
|
|
|
* @author tlwr [toby@toby.codes]
|
|
|
|
* @copyright Crown Copyright 2016
|
|
|
|
* @license Apache-2.0
|
|
|
|
*/
|
|
|
|
|
|
|
|
import Operation from "../Operation";
|
|
|
|
import Utils from "../Utils";
|
2018-12-30 02:36:58 +01:00
|
|
|
import Stream from "../lib/Stream";
|
2018-05-21 20:23:05 +02:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Untar operation
|
|
|
|
*/
|
|
|
|
class Untar extends Operation {
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Untar constructor
|
|
|
|
*/
|
|
|
|
constructor() {
|
|
|
|
super();
|
|
|
|
|
|
|
|
this.name = "Untar";
|
|
|
|
this.module = "Compression";
|
|
|
|
this.description = "Unpacks a tarball and displays it per file.";
|
2018-08-21 20:07:13 +02:00
|
|
|
this.infoURL = "https://wikipedia.org/wiki/Tar_(computing)";
|
2018-05-21 20:23:05 +02:00
|
|
|
this.inputType = "byteArray";
|
|
|
|
this.outputType = "List<File>";
|
|
|
|
this.presentType = "html";
|
|
|
|
this.args = [];
|
|
|
|
this.patterns = [
|
|
|
|
{
|
|
|
|
"match": "^.{257}\\x75\\x73\\x74\\x61\\x72",
|
|
|
|
"flags": "",
|
|
|
|
"args": []
|
|
|
|
}
|
|
|
|
];
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @param {byteArray} input
|
|
|
|
* @param {Object[]} args
|
|
|
|
* @returns {List<File>}
|
|
|
|
*/
|
|
|
|
run(input, args) {
|
|
|
|
const stream = new Stream(input),
|
|
|
|
files = [];
|
|
|
|
|
|
|
|
while (stream.hasMore()) {
|
|
|
|
const dataPosition = stream.position + 512;
|
|
|
|
|
|
|
|
const file = {
|
|
|
|
fileName: stream.readString(100),
|
|
|
|
fileMode: stream.readString(8),
|
|
|
|
ownerUID: stream.readString(8),
|
|
|
|
ownerGID: stream.readString(8),
|
|
|
|
size: parseInt(stream.readString(12), 8), // Octal
|
2018-12-30 02:36:58 +01:00
|
|
|
lastModTime: new Date(1000 * parseInt(stream.readString(12), 8)), // Octal
|
2018-05-21 20:23:05 +02:00
|
|
|
checksum: stream.readString(8),
|
|
|
|
type: stream.readString(1),
|
|
|
|
linkedFileName: stream.readString(100),
|
|
|
|
USTARFormat: stream.readString(6).indexOf("ustar") >= 0,
|
|
|
|
};
|
|
|
|
|
|
|
|
if (file.USTARFormat) {
|
|
|
|
file.version = stream.readString(2);
|
|
|
|
file.ownerUserName = stream.readString(32);
|
|
|
|
file.ownerGroupName = stream.readString(32);
|
|
|
|
file.deviceMajor = stream.readString(8);
|
|
|
|
file.deviceMinor = stream.readString(8);
|
|
|
|
file.filenamePrefix = stream.readString(155);
|
|
|
|
}
|
|
|
|
|
|
|
|
stream.position = dataPosition;
|
|
|
|
|
|
|
|
if (file.type === "0") {
|
|
|
|
// File
|
|
|
|
let endPosition = stream.position + file.size;
|
|
|
|
if (file.size % 512 !== 0) {
|
|
|
|
endPosition += 512 - (file.size % 512);
|
|
|
|
}
|
|
|
|
|
|
|
|
file.bytes = stream.getBytes(file.size);
|
|
|
|
files.push(new File([new Uint8Array(file.bytes)], file.fileName));
|
|
|
|
stream.position = endPosition;
|
|
|
|
} else if (file.type === "5") {
|
|
|
|
// Directory
|
|
|
|
files.push(new File([new Uint8Array(file.bytes)], file.fileName));
|
|
|
|
} else {
|
|
|
|
// Symlink or empty bytes
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return files;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Displays the files in HTML for web apps.
|
|
|
|
*
|
|
|
|
* @param {File[]} files
|
|
|
|
* @returns {html}
|
|
|
|
*/
|
|
|
|
async present(files) {
|
|
|
|
return await Utils.displayFilesAsHTML(files);
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
export default Untar;
|