archive-jszip.jsā¢5.05 kB
"use strict";
var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
return new (P || (P = Promise))(function (resolve, reject) {
function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
step((generator = generator.apply(thisArg, _arguments || [])).next());
});
};
var __importDefault = (this && this.__importDefault) || function (mod) {
return (mod && mod.__esModule) ? mod : { "default": mod };
};
Object.defineProperty(exports, "__esModule", { value: true });
const archive_1 = __importDefault(require("./archive"));
const fs_1 = __importDefault(require("fs"));
const jszip_1 = __importDefault(require("jszip"));
const path_1 = __importDefault(require("path"));
class ArchiveJszip extends archive_1.default {
constructor(filename) {
super(filename);
}
initialize() {
return __awaiter(this, void 0, void 0, function* () {
this.file = yield fs_1.default.promises.readFile(this.filename);
const zip = new jszip_1.default();
this.archive = yield zip.loadAsync(this.file);
return this;
});
}
fileExists(file) {
if (this.archive === undefined || this.archive.files[file] === undefined) {
return false;
}
return true;
}
folder(dir) {
return __awaiter(this, void 0, void 0, function* () {
const files = [];
this.archive.folder(dir).forEach((relativePath, file) => {
if (!relativePath.includes('/')) {
files.push({
name: file.name,
relativePath,
});
}
});
return files;
});
}
read(file, type) {
return __awaiter(this, void 0, void 0, function* () {
if (!this.archive) {
yield this.initialize();
}
if (!this.archive.files[file]) {
throw new Error('Could not find file ' + file + '@' + path_1.default.basename(this.filename));
}
return this.archive.files[file].async(type || 'string');
});
}
write(file, data) {
return __awaiter(this, void 0, void 0, function* () {
this.archive.file(file, data);
return this;
});
}
remove(file) {
return __awaiter(this, void 0, void 0, function* () {
this.archive.remove(file);
});
}
extract(file) {
return __awaiter(this, void 0, void 0, function* () {
const contents = (yield this.read(file, 'nodebuffer'));
const zip = new jszip_1.default();
const newArchive = new ArchiveJszip(file);
newArchive.archive = yield zip.loadAsync(contents);
return newArchive;
});
}
output(location, params) {
return __awaiter(this, void 0, void 0, function* () {
const content = yield this.getContent(params);
yield fs_1.default.promises.writeFile(location, content).catch((err) => {
console.error(err);
throw new Error(`Could not write output file: ${location}`);
});
});
}
stream(params, options) {
return __awaiter(this, void 0, void 0, function* () {
this.setOptions(params);
yield this.writeBuffer(this);
const mergedOptions = Object.assign(Object.assign({}, this.options), options);
return this.archive.generateNodeStream(mergedOptions);
});
}
getFinalArchive() {
return __awaiter(this, void 0, void 0, function* () {
yield this.writeBuffer(this);
return this.archive;
});
}
getContent(params) {
return __awaiter(this, void 0, void 0, function* () {
this.setOptions(params);
yield this.writeBuffer(this);
return (yield this.archive.generateAsync(this.options));
});
}
readXml(file) {
return __awaiter(this, void 0, void 0, function* () {
const isBuffered = this.fromBuffer(file);
if (!isBuffered) {
const xmlString = (yield this.read(file, 'string'));
const XmlDocument = this.parseXml(xmlString);
this.toBuffer(file, XmlDocument);
return XmlDocument;
}
else {
return isBuffered.content;
}
});
}
writeXml(file, XmlDocument) {
this.toBuffer(file, XmlDocument);
}
}
exports.default = ArchiveJszip;
//# sourceMappingURL=archive-jszip.js.map