From 46360a180a15e6f0c0b3ae9834094198cb86d6ff Mon Sep 17 00:00:00 2001
From: reviewher <24845478+reviewher@users.noreply.github.com>
Date: Sat, 12 Feb 2022 15:26:50 -0800
Subject: [PATCH] CFB zip compression option

fixes #600 and fixes #1638
---
 bits/21_ziputils.js |  2 +-
 bits/88_write.js    |  2 +-
 mini.lst            |  4 +-
 misc/21_ziputils.js | 93 ---------------------------------------------
 misc/mjs.lst        |  2 +-
 5 files changed, 5 insertions(+), 98 deletions(-)
 delete mode 100644 misc/21_ziputils.js

diff --git a/bits/21_ziputils.js b/bits/21_ziputils.js
index e897a38..bf600b5 100644
--- a/bits/21_ziputils.js
+++ b/bits/21_ziputils.js
@@ -61,7 +61,7 @@ function getzipbin(zip, file/*:string*/, safe/*:?boolean*/)/*:any*/ {
 
 function zipentries(zip) {
 	var k = zip.FullPaths || keys(zip.files), o = [];
-	for(var i = 0; i < k.length; ++i) if(k[i].slice(-1) != '/') o.push(k[i]);
+	for(var i = 0; i < k.length; ++i) if(k[i].slice(-1) != '/') o.push(k[i].replace(/^Root Entry[\/]/, ""));
 	return o.sort();
 }
 
diff --git a/bits/88_write.js b/bits/88_write.js
index fc580f6..97db769 100644
--- a/bits/88_write.js
+++ b/bits/88_write.js
@@ -47,7 +47,7 @@ function write_zip_type(wb/*:Workbook*/, opts/*:?WriteOpts*/)/*:any*/ {
 		case "file": oopts.type = has_buf ? "nodebuffer" : "string"; break;
 		default: throw new Error("Unrecognized type " + o.type);
 	}
-	var out = z.FullPaths ? CFB.write(z, {fileType:"zip", type: /*::(*/{"nodebuffer": "buffer", "string": "binary"}/*:: :any)*/[oopts.type] || oopts.type}) : z.generate(oopts);
+	var out = z.FullPaths ? CFB.write(z, {fileType:"zip", type: /*::(*/{"nodebuffer": "buffer", "string": "binary"}/*:: :any)*/[oopts.type] || oopts.type, compression: !!o.compression}) : z.generate(oopts);
 	if(typeof Deno !== "undefined" && typeof out == "string") out = new Uint8Array(s2ab(out));
 /*jshint -W083 */
 	if(o.password && typeof encrypt_agile !== 'undefined') return write_cfb_ctr(encrypt_agile(out, o.password), o); // eslint-disable-line no-undef
diff --git a/mini.lst b/mini.lst
index 8213e20..941d944 100644
--- a/mini.lst
+++ b/mini.lst
@@ -1,6 +1,6 @@
 bits/00_header.js
 bits/01_version.js
-misc/02_codepage.js
+bits/02_codepage.js
 bits/03_consts.js
 bits/04_base64.js
 bits/05_buf.js
@@ -10,7 +10,7 @@ bits/11_ssfutils.js
 bits/18_cfb.js
 bits/19_fsutils.js
 bits/20_jsutils.js
-misc/21_ziputils.js
+bits/21_ziputils.js
 bits/22_xmlutils.js
 bits/23_binutils.js
 bits/24_hoppers.js
diff --git a/misc/21_ziputils.js b/misc/21_ziputils.js
deleted file mode 100644
index dd0cd51..0000000
--- a/misc/21_ziputils.js
+++ /dev/null
@@ -1,93 +0,0 @@
-function getdatastr(data)/*:?string*/ {
-	if(!data) return null;
-	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)));
-	if(data.content && data.type) return debom(cc2str(data.content));
-	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();
-	if(data._data && data._data.getContent) {
-		var o = data._data.getContent();
-		if(typeof o == "string") return char_codes(o);
-		return Array.prototype.slice.call(o);
-	}
-	if(data.content && data.type) return data.content;
-	return null;
-}
-
-function getdata(data) { return (data && data.name.slice(-4) === ".bin") ? getdatabin(data) : getdatastr(data); }
-
-/* Part 2 Section 10.1.2 "Mapping Content Types" Names are case-insensitive */
-/* OASIS does not comment on filename case sensitivity */
-function safegetzipfile(zip, file/*:string*/) {
-	var k = zip.FullPaths || keys(zip.files);
-	var f = file.toLowerCase().replace(/[\/]/g, '\\'), g = f.replace(/\\/g,'\/');
-	for(var i=0; i<k.length; ++i) {
-		var n = k[i].replace(/^Root Entry[\/]/,"").toLowerCase();
-		if(f == n || g == n) return zip.files ? zip.files[k[i]] : zip.FileIndex[i];
-	}
-	return null;
-}
-
-function getzipfile(zip, file/*:string*/) {
-	var o = safegetzipfile(zip, file);
-	if(o == null) throw new Error("Cannot find file " + file + " in zip");
-	return o;
-}
-
-function getzipdata(zip, file/*:string*/, safe/*:?boolean*/)/*:any*/ {
-	if(!safe) return getdata(getzipfile(zip, file));
-	if(!file) return null;
-	try { return getzipdata(zip, file); } catch(e) { return null; }
-}
-
-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; }
-}
-
-function zipentries(zip) {
-	var k = zip.FullPaths || keys(zip.files), o = [];
-	for(var i = 0; i < k.length; ++i) if(k[i].slice(-1) != '/') o.push(k[i].replace(/^Root Entry[\/]/, ""));
-	return o.sort();
-}
-
-function zip_add_file(zip, path, content) {
-	if(zip.FullPaths) CFB.utils.cfb_add(zip, path, typeof content == "string" ? (has_buf ? Buffer_from(content) : s2a(utf8write(content))) : content);
-	else zip.file(path, content);
-}
-
-function zip_new() {
-	return CFB.utils.cfb_new();
-}
-
-function zip_read(d, o) {
-	var zip;
-	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;
-}
-
-function resolve_path(path/*:string*/, base/*:string*/)/*:string*/ {
-	if(path.charAt(0) == "/") return path.slice(1);
-	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('/');
-}
diff --git a/misc/mjs.lst b/misc/mjs.lst
index a95cd2f..8aff53c 100644
--- a/misc/mjs.lst
+++ b/misc/mjs.lst
@@ -10,7 +10,7 @@ bits/11_ssfutils.js
 misc/18_esmcfb.js
 misc/19_mjsfs.js
 bits/20_jsutils.js
-misc/21_ziputils.js
+bits/21_ziputils.js
 bits/22_xmlutils.js
 bits/23_binutils.js
 bits/24_hoppers.js