From 78579e4d7c0b47eb4d560714b699d841ef15b3eb Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Manuel=20Nu=C3=B1ez?= Date: Fri, 23 Jan 2015 13:18:33 -0300 Subject: [PATCH 1/3] Added support for custom mime-types, ie: "application/ods" MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Signed-off-by: Manuel Nuñez --- lib/object.js | 5 +++-- lib/utils.js | 7 ++++--- 2 files changed, 7 insertions(+), 5 deletions(-) diff --git a/lib/object.js b/lib/object.js index c0bde644..51469075 100644 --- a/lib/object.js +++ b/lib/object.js @@ -647,7 +647,8 @@ var out = { base64: true, compression: "STORE", type: "base64", - comment: null + comment: null, + mimeType: 'application/zip' }); utils.checkSupport(options.type); @@ -731,7 +732,7 @@ var out = { case "nodebuffer" : return utils.transformTo(options.type.toLowerCase(), zip); case "blob" : - return utils.arrayBuffer2Blob(utils.transformTo("arraybuffer", zip)); + return utils.arrayBuffer2Blob(utils.transformTo("arraybuffer", zip), options.mimeType); // case "zip is a string" case "base64" : return (options.base64) ? base64.encode(zip) : zip; diff --git a/lib/utils.js b/lib/utils.js index 1e3bb5b6..da54747a 100644 --- a/lib/utils.js +++ b/lib/utils.js @@ -14,13 +14,14 @@ exports.string2binary = function(str) { } return result; }; -exports.arrayBuffer2Blob = function(buffer) { +exports.arrayBuffer2Blob = function(buffer, mimeType) { exports.checkSupport("blob"); + mimeType = mimeType || 'application/zip'; try { // Blob constructor return new Blob([buffer], { - type: "application/zip" + type: mimeType }); } catch (e) { @@ -30,7 +31,7 @@ exports.arrayBuffer2Blob = function(buffer) { var Builder = window.BlobBuilder || window.WebKitBlobBuilder || window.MozBlobBuilder || window.MSBlobBuilder; var builder = new Builder(); builder.append(buffer); - return builder.getBlob('application/zip'); + return builder.getBlob(mimeType); } catch (e) { From 357e71896a3b0dff64255109331c7665a4a59f39 Mon Sep 17 00:00:00 2001 From: min1974 Date: Thu, 29 Jan 2015 20:06:51 -0300 Subject: [PATCH 2/3] Update generate.md Added information for the new option "mimeType" --- documentation/api_jszip/generate.md | 39 +++++++++++++++++++++++++++++ 1 file changed, 39 insertions(+) diff --git a/documentation/api_jszip/generate.md b/documentation/api_jszip/generate.md index 6623f923..95b0ec25 100644 --- a/documentation/api_jszip/generate.md +++ b/documentation/api_jszip/generate.md @@ -15,6 +15,7 @@ options.base64 | boolean | false | **deprecated**, use `type` instead. If options.compression | string | `STORE` (no compression) | the default file compression method to use. Available methods are `STORE` and `DEFLATE`. You can also provide your own compression method. options.type | string | `base64` | The type of zip to return, see below for the other types. options.comment | string | | The comment to use for the zip file. +options.mimeType | string | `application/zip` | mime-type for the generated file. Useful when you need to generate a file with a different extension, ie: ".ods". Possible values for `type` : @@ -60,4 +61,42 @@ var content = zip.generate({type:"nodebuffer"}); require("fs").writeFile("hello.zip", content, function(err){/*...*/}); ``` +```js +//This example will Generate a Open Document Spreasheet, with the correct mime type +var zip = new JSZip(); +zip.file("mimetype", "application/vnd.oasis.opendocument.spreadsheet"); +var conf2 = zip.folder("Configurations2"); +conf2.folder("acceleator"); +conf2.folder("images"); +conf2.folder("popupmenu"); +conf2.folder("statusbar"); +conf2.folder("floater"); +conf2.folder("menubar"); +conf2.folder("progressbar"); +conf2.folder("toolbar"); + +var manifest = "<..."; //xml containing manifest.xml +var styles = "<..."; //xml containing styles.xml +var settings = "<..."; //xml containing settings.xml +var meta = "<..."; //xml containing meta.xml +var content = "<..."; //xml containing content.xml + +var metaInf = zip.folder("META-INF"); +metaInf.file("manifest.xml", manifest); +zip.file("styles.xml", styles); +zip.file("settings.xml", settings); +zip.file("meta.xml", meta); +zip.file("content.xml", content); + +//Generate the file +var odsFile = zip.generate({type: "blob", mimeType: "application/ods", compression: "DEFLATE"}); + +var url = window.URL.createObjectURL(odsFile); +var link = document.getElementById("link"); //I suppose you'll have a link with this id :) +link.download = "testjs.ods"; +link.href = url; + + +``` + From 7b35405a1aafe0036ed9a70d9f2026a2d4a690b2 Mon Sep 17 00:00:00 2001 From: min1974 Date: Thu, 29 Jan 2015 20:31:26 -0300 Subject: [PATCH 3/3] Update test.js Added test for mimeType --- test/test.js | 21 +++++++++++++++++++++ 1 file changed, 21 insertions(+) diff --git a/test/test.js b/test/test.js index 22745d52..69e2a24f 100644 --- a/test/test.js +++ b/test/test.js @@ -832,6 +832,27 @@ if (JSZip.support.blob) { }); } +if (JSZip.support.blob) { + test("generate : type:blob mimeType:application/ods", function() { + var zip = new JSZip(); + zip.file("Hello.txt", "Hello World\n"); + var blob = zip.generate({type:"blob", mimeType: "application/ods"}); + ok(blob instanceof Blob, "The result is a instance of Blob"); + equal(blob.type, "application/ods", "mime-type is application/ods"); + }); +} else { + test("generate : type:blob mimeType:application/ods", function() { + var zip = new JSZip(); + zip.file("Hello.txt", "Hello World\n"); + try { + var blob = zip.generate({type:"blob", mimeType: "application/ods"}); + ok(false, "Blob is not supported, but no exception thrown"); + } catch(e) { + ok(e.message.match("not supported by this browser"), "the error message is useful"); + } + }); +} + test("Filtering a zip", function() { var zip = new JSZip(); zip.file("1.txt", "1\n");