title | layout | section |
---|---|---|
Upgrade Guide |
default |
main |
- Deprecated objects/methods has been removed:
options.base64
ingenerate()
(the base64 type is still valid)options.base64
,options.binary
,options.dir
,options.date
onZipObject
(see the 2.3 upgrade section)JSZip.utils
JSZip.prototype.crc32
,JSZip.prototype.utf8encode
,JSZip.prototype.utf8decode
JSZip.base64
(you can get the content of a file directly as a base64 string)
JSZip.compressions
has been removed.- On
ZipObject
, the synchronous getters has been replaced byasync()
andnodeStream()
. - The
generate()
method has been replaced bygenerateAsync()
andgenerateNodeStream()
. - The
type
option ingenerate()
is now mandatory. - The "text" type has been replaced by the "string" type, a binary string is named "binarystring".
- The
load()
method and the constructor with data (new JSZip(data)
) have been replaced byloadAsync()
. - When adding a file, the option
createFolders
now defaults totrue
. If you don't want to create sub folders, set it to false. zip.generateAsync()
andzip.generateNodeStream()
now depend on the current folder level.
// 2.x
zip.file("test.txt").asText();
// 3.x
zip.file("test.txt").async("string")
.then(function (content) {
// use content
});
// 2.x
zip.generate();
// 3.x
zip.generateAsync({type:"uint8array"})
.then(function (content) {
// use content
});
// 2.x
new JSZip(data);
zip.load(data);
// zip.file(...)
// 3.x
JSZip.loadAsync(data).then(zip) {...};
zip.loadAsync(data).then(zip) {...};
// here, zip won't have (yet) the updated content
// 2.x
var data = zip.file("img.jpg").asBinary();
var dataURI = "data:image/jpeg;base64," + JSZip.base64.encode(data);
// 3.x
zip.file("img.jpg").async("base64")
.then(function (data64) {
var dataURI = "data:image/jpeg;base64," + data64;
});
async
and loadAsync
use (a polyfill of) promises, you can find
the documentation here
and a tutorial here.
It is worth noting that:
/*
* JSZip accepts these promise as input
*/
// replace a content with JSZip v2
var content = zip.file("my_file").asText();
content = content.replace(/apples/, 'oranges');
zip.file("my_file", content);
// replace a content with JSZip v3
var contentPromise = zip.file("my_file").async("text").then(function (content) {
return content.replace(/apples/, 'oranges');
});
zip.file("my_file", contentPromise);
/*
* Promises are chainable
*/
// read, update, generate a zip file with JSZip v2
var zip = new JSZip(content);
zip.file("new_file", "new_content");
var blob = zip.generate({type: "blob"});
saveAs(blob, "result.zip");
// read, update, generate a zip file with JSZip v3
JSZip.loadAsync(content)
.then(function (zip) {
zip.file("new_file", "new_content");
// if you return the zip object, it will be available in the next "then"
return zip;
.then(function (zip) {
// if you return a promise of a blob, promises will "merge": the current
// promise will wait for the other and the next "then" will get the
// blob
return zip.generateAsync({type: "blob"});
.then(function (blob) {
saveAs(blob, "result.zip");
});
- On
ZipObject#options
, the attributesdate
anddir
have been deprecated and are now onZipObject
. - On
ZipObject#options
, the attributesbase64
andbinary
have been deprecated. JSZip.base64
,JSZip.prototype.crc32
,JSZip.prototype.utf8decode
,JSZip.prototype.utf8encode
andJSZip.utils
have been deprecated.
// deprecated
zip.file("test.txt").options.date
zip.file("test.txt").options.dir
// new API
zip.file("test.txt").date
zip.file("test.txt").dir
- The packaging changed : instead of loading jszip.js, jszip-load.js, jszip-inflate.js, jszip-deflate.js, just include dist/jszip.js or dist/jszip.min.js. For AMD loader users : JSZip now registers itself. You just have to put the file at the right place or configure your loader.
JSZipBase64
has been renamed toJSZip.base64
.- The
data
attribute doesn't exist anymore : use the gettersasText()
,asBinary()
, etc - The compression/decompression methods now give their input type with the
compressInputType
anduncompressInputType
attributes.
Example for the data attribute :
// before
zip.file("test.txt").data;
zip.files["test.txt"].data;
zip.file("image.png").data;
zip.files["image.png"].data;
// after
zip.file("test.txt").asText();
zip.files["test.txt"].asText();
zip.file("image.png").asBinary();
zip.files["image.png"].asBinary();