Skip to content

Commit

Permalink
Merge pull request #199 from min1974/master
Browse files Browse the repository at this point in the history
Added support for custom mime-types, ie: "application/ods".
  • Loading branch information
dduponchel committed Feb 18, 2015
2 parents bf7ec8c + 7b35405 commit 8e21585
Show file tree
Hide file tree
Showing 4 changed files with 67 additions and 5 deletions.
39 changes: 39 additions & 0 deletions documentation/api_jszip/generate.md
Original file line number Diff line number Diff line change
Expand Up @@ -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` :

Expand Down Expand Up @@ -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;


```


5 changes: 3 additions & 2 deletions lib/object.js
Original file line number Diff line number Diff line change
Expand Up @@ -647,7 +647,8 @@ var out = {
base64: true,
compression: "STORE",
type: "base64",
comment: null
comment: null,
mimeType: 'application/zip'
});

utils.checkSupport(options.type);
Expand Down Expand Up @@ -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;
Expand Down
7 changes: 4 additions & 3 deletions lib/utils.js
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand All @@ -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) {

Expand Down
21 changes: 21 additions & 0 deletions test/test.js
Original file line number Diff line number Diff line change
Expand Up @@ -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");
Expand Down

0 comments on commit 8e21585

Please sign in to comment.