Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Added support for custom mime-types, ie: "application/ods". #199

Merged
merged 3 commits into from
Feb 18, 2015
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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