2017-02-10 19:23:01 +00:00
|
|
|
function getdatastr(data)/*:?string*/ {
|
2014-01-28 16:38:02 +00:00
|
|
|
if(!data) return null;
|
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)));
|
2019-08-04 19:50:49 +00:00
|
|
|
if(data.content && data.type) return debom(cc2str(data.content));
|
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 = [];
|
2018-02-28 10:41:49 +00:00
|
|
|
for(var i = 0; i < k.length; ++i) if(k[i].slice(-1) != '/') o.push(k[i]);
|
|
|
|
return o.sort();
|
|
|
|
}
|
|
|
|
|
2019-08-04 19:50:49 +00:00
|
|
|
function zip_add_file(zip, path, content) {
|
|
|
|
if(zip.FullPaths) CFB.utils.cfb_add(zip, path, content);
|
|
|
|
else zip.file(path, content);
|
|
|
|
}
|
|
|
|
|
2018-02-03 20:46:32 +00:00
|
|
|
var jszip;
|
2018-03-06 00:34:04 +00:00
|
|
|
/*:: declare var JSZipSync:any; */
|
|
|
|
/*global JSZipSync:true */
|
|
|
|
if(typeof JSZipSync !== 'undefined') jszip = JSZipSync;
|
2018-02-03 20:46:32 +00:00
|
|
|
if(typeof exports !== 'undefined') {
|
|
|
|
if(typeof module !== 'undefined' && module.exports) {
|
2017-03-05 00:56:31 +00:00
|
|
|
if(typeof jszip === 'undefined') jszip = require('./jszip.js');
|
2014-01-28 16:38:02 +00:00
|
|
|
}
|
|
|
|
}
|
2017-03-27 21:35:15 +00:00
|
|
|
|
2019-08-04 19:50:49 +00:00
|
|
|
function zip_new() {
|
2019-11-01 03:09:14 +00:00
|
|
|
if(!jszip) return CFB.utils.cfb_new();
|
2019-08-04 19:50:49 +00:00
|
|
|
return new jszip();
|
|
|
|
}
|
|
|
|
|
2019-11-01 03:09:14 +00:00
|
|
|
function zip_read(d, o) {
|
|
|
|
var zip;
|
|
|
|
if(jszip) switch(o.type) {
|
|
|
|
case "base64": zip = new jszip(d, { base64:true }); break;
|
|
|
|
case "binary": case "array": zip = new jszip(d, { base64:false }); break;
|
|
|
|
case "buffer": zip = new jszip(d); break;
|
|
|
|
default: throw new Error("Unrecognized type " + o.type);
|
|
|
|
}
|
|
|
|
else switch(o.type) {
|
|
|
|
case "base64": zip = CFB.read(d, { type: "base64" }); break;
|
|
|
|
case "binary": zip = CFB.read(d, { type: "binary" }); break;
|
|
|
|
case "buffer": case "array": zip = CFB.read(d, { type: "buffer" }); break;
|
|
|
|
default: throw new Error("Unrecognized type " + o.type);
|
|
|
|
}
|
|
|
|
return zip;
|
|
|
|
}
|
|
|
|
|
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('/');
|
|
|
|
}
|