2017-02-10 19:23:01 +00:00
|
|
|
function getdatastr(data)/*:?string*/ {
|
2014-01-28 16:38:02 +00:00
|
|
|
if(!data) return null;
|
2022-02-10 12:40:50 +00:00
|
|
|
if(data.content && data.type) return cc2str(data.content, true);
|
2017-02-24 10:33:01 +00:00
|
|
|
if(data.data) return debom(data.data);
|
|
|
|
if(data.asNodeBuffer && has_buf) return debom(data.asNodeBuffer().toString('binary'));
|
|
|
|
if(data.asBinary) return debom(data.asBinary());
|
|
|
|
if(data._data && data._data.getContent) return debom(cc2str(Array.prototype.slice.call(data._data.getContent(),0)));
|
2017-02-10 19:23:01 +00:00
|
|
|
return null;
|
|
|
|
}
|
|
|
|
|
|
|
|
function getdatabin(data) {
|
|
|
|
if(!data) return null;
|
|
|
|
if(data.data) return char_codes(data.data);
|
|
|
|
if(data.asNodeBuffer && has_buf) return data.asNodeBuffer();
|
2017-02-22 06:57:59 +00:00
|
|
|
if(data._data && data._data.getContent) {
|
|
|
|
var o = data._data.getContent();
|
2017-12-30 05:40:35 +00:00
|
|
|
if(typeof o == "string") return char_codes(o);
|
2017-02-22 06:57:59 +00:00
|
|
|
return Array.prototype.slice.call(o);
|
|
|
|
}
|
2019-08-04 19:50:49 +00:00
|
|
|
if(data.content && data.type) return data.content;
|
2014-01-28 16:38:02 +00:00
|
|
|
return null;
|
|
|
|
}
|
|
|
|
|
2017-02-22 06:57:59 +00:00
|
|
|
function getdata(data) { return (data && data.name.slice(-4) === ".bin") ? getdatabin(data) : getdatastr(data); }
|
2017-02-10 19:23:01 +00:00
|
|
|
|
2017-02-24 10:33:01 +00:00
|
|
|
/* Part 2 Section 10.1.2 "Mapping Content Types" Names are case-insensitive */
|
2017-03-10 01:09:18 +00:00
|
|
|
/* OASIS does not comment on filename case sensitivity */
|
2017-02-10 19:23:01 +00:00
|
|
|
function safegetzipfile(zip, file/*:string*/) {
|
2019-08-04 19:50:49 +00:00
|
|
|
var k = zip.FullPaths || keys(zip.files);
|
2021-09-21 01:31:27 +00:00
|
|
|
var f = file.toLowerCase().replace(/[\/]/g, '\\'), g = f.replace(/\\/g,'\/');
|
2017-02-24 10:33:01 +00:00
|
|
|
for(var i=0; i<k.length; ++i) {
|
2021-09-21 01:31:27 +00:00
|
|
|
var n = k[i].replace(/^Root Entry[\/]/,"").toLowerCase();
|
2021-10-12 08:55:29 +00:00
|
|
|
if(f == n || g == n) return zip.files ? zip.files[k[i]] : zip.FileIndex[i];
|
2017-02-24 10:33:01 +00:00
|
|
|
}
|
2014-10-10 02:22:38 +00:00
|
|
|
return null;
|
|
|
|
}
|
|
|
|
|
2017-02-10 19:23:01 +00:00
|
|
|
function getzipfile(zip, file/*:string*/) {
|
2014-10-10 02:22:38 +00:00
|
|
|
var o = safegetzipfile(zip, file);
|
|
|
|
if(o == null) throw new Error("Cannot find file " + file + " in zip");
|
|
|
|
return o;
|
2014-01-28 16:38:02 +00:00
|
|
|
}
|
|
|
|
|
2017-09-30 06:18:11 +00:00
|
|
|
function getzipdata(zip, file/*:string*/, safe/*:?boolean*/)/*:any*/ {
|
2014-02-15 05:08:18 +00:00
|
|
|
if(!safe) return getdata(getzipfile(zip, file));
|
|
|
|
if(!file) return null;
|
|
|
|
try { return getzipdata(zip, file); } catch(e) { return null; }
|
|
|
|
}
|
|
|
|
|
2017-02-10 19:23:01 +00:00
|
|
|
function getzipstr(zip, file/*:string*/, safe/*:?boolean*/)/*:?string*/ {
|
|
|
|
if(!safe) return getdatastr(getzipfile(zip, file));
|
|
|
|
if(!file) return null;
|
|
|
|
try { return getzipstr(zip, file); } catch(e) { return null; }
|
|
|
|
}
|
|
|
|
|
2022-01-29 02:29:34 +00:00
|
|
|
function getzipbin(zip, file/*:string*/, safe/*:?boolean*/)/*:any*/ {
|
|
|
|
if(!safe) return getdatabin(getzipfile(zip, file));
|
|
|
|
if(!file) return null;
|
|
|
|
try { return getzipbin(zip, file); } catch(e) { return null; }
|
|
|
|
}
|
|
|
|
|
2018-02-28 10:41:49 +00:00
|
|
|
function zipentries(zip) {
|
2019-08-04 19:50:49 +00:00
|
|
|
var k = zip.FullPaths || keys(zip.files), o = [];
|
2022-02-12 23:26:50 +00:00
|
|
|
for(var i = 0; i < k.length; ++i) if(k[i].slice(-1) != '/') o.push(k[i].replace(/^Root Entry[\/]/, ""));
|
2018-02-28 10:41:49 +00:00
|
|
|
return o.sort();
|
|
|
|
}
|
|
|
|
|
2019-08-04 19:50:49 +00:00
|
|
|
function zip_add_file(zip, path, content) {
|
2022-02-10 12:40:50 +00:00
|
|
|
if(zip.FullPaths) {
|
2024-07-04 19:54:34 +00:00
|
|
|
if(Array.isArray(content) && typeof content[0] == "string") {
|
|
|
|
content = content.join("");
|
|
|
|
}
|
2022-02-10 12:40:50 +00:00
|
|
|
if(typeof content == "string") {
|
|
|
|
var res;
|
|
|
|
if(has_buf) res = Buffer_from(content);
|
|
|
|
/* TODO: investigate performance in Edge 13 */
|
|
|
|
//else if(typeof TextEncoder !== "undefined") res = new TextEncoder().encode(content);
|
|
|
|
else res = utf8decode(content);
|
|
|
|
return CFB.utils.cfb_add(zip, path, res);
|
|
|
|
}
|
|
|
|
CFB.utils.cfb_add(zip, path, content);
|
2014-01-28 16:38:02 +00:00
|
|
|
}
|
2022-02-10 12:40:50 +00:00
|
|
|
else zip.file(path, content);
|
2014-01-28 16:38:02 +00:00
|
|
|
}
|
2017-03-27 21:35:15 +00:00
|
|
|
|
2022-02-10 12:40:50 +00:00
|
|
|
function zip_new() { return CFB.utils.cfb_new(); }
|
2019-08-04 19:50:49 +00:00
|
|
|
|
2019-11-01 03:09:14 +00:00
|
|
|
function zip_read(d, o) {
|
2022-02-10 12:40:50 +00:00
|
|
|
switch(o.type) {
|
|
|
|
case "base64": return CFB.read(d, { type: "base64" });
|
|
|
|
case "binary": return CFB.read(d, { type: "binary" });
|
|
|
|
case "buffer": case "array": return CFB.read(d, { type: "buffer" });
|
2019-11-01 03:09:14 +00:00
|
|
|
}
|
2022-02-10 12:40:50 +00:00
|
|
|
throw new Error("Unrecognized type " + o.type);
|
2019-11-01 03:09:14 +00:00
|
|
|
}
|
|
|
|
|
2017-03-27 21:35:15 +00:00
|
|
|
function resolve_path(path/*:string*/, base/*:string*/)/*:string*/ {
|
2019-11-01 03:09:14 +00:00
|
|
|
if(path.charAt(0) == "/") return path.slice(1);
|
2017-03-27 21:35:15 +00:00
|
|
|
var result = base.split('/');
|
|
|
|
if(base.slice(-1) != "/") result.pop(); // folder path
|
|
|
|
var target = path.split('/');
|
|
|
|
while (target.length !== 0) {
|
|
|
|
var step = target.shift();
|
|
|
|
if (step === '..') result.pop();
|
|
|
|
else if (step !== '.') result.push(step);
|
|
|
|
}
|
|
|
|
return result.join('/');
|
|
|
|
}
|