Add an option to set the DEFLATE level. #201

Merged
merged 2 commits into from Feb 20, 2015

Projects

None yet

2 participants

@dduponchel
Collaborator

This commit adds a new option, compressionOptions. If it contains a field
level, it is used by the DEFLATE compression to set the compression level :
6 by default or any level between 1 (best speed) and 9 (best compression).

Fix #165.

@dduponchel dduponchel Add an option to set the DEFLATE level.
This commit adds a new option, `compressionOptions`. If it contains a field
`level`, it is used by the DEFLATE compression to set the compression level :
6 by default or any level between 1 (best speed) and 9 (best compression).

Fix #165.
889d112
@Stuk Stuk and 1 other commented on an outdated diff Feb 19, 2015
documentation/api_jszip/generate.md
@@ -29,6 +30,16 @@ Possible values for `type` :
Note : when using type = "uint8array", "arraybuffer" or "blob", be sure to
check if the browser supports it (you can use [`JSZip.support`]({{site.baseurl}}/documentation/api_jszip/support.html)).
+The `compressionOptions` parameter depends on the compression type. With
+`STORE` (no compression), this parameter is ignored. With `DEFLATE`, you can
+give the compression level with `compressionOptions : {level:6}` (or any level
+between 1 (best speed) and 9 (best compression)).
+
+Note : if the entry is *already* compressed (coming from a compressed zip file),
+calling `generate()` with a different compression level won't update the entry.
+The reason is simple : JSZip doesn't know how was compressed the content and
+how to match the compression level with the implementation we use.
@Stuk
Stuk Feb 19, 2015 Owner

Grammar: "...doesn't know how compressed the content was and how to..."

@dduponchel
dduponchel Feb 19, 2015 Collaborator

Fixed.

@Stuk Stuk commented on the diff Feb 19, 2015
lib/flate.js
@@ -6,8 +6,10 @@ exports.uncompressInputType = USE_TYPEDARRAY ? "uint8array" : "array";
exports.compressInputType = USE_TYPEDARRAY ? "uint8array" : "array";
exports.magic = "\x08\x00";
-exports.compress = function(input) {
- return pako.deflateRaw(input);
+exports.compress = function(input, compressionOptions) {
+ return pako.deflateRaw(input, {
+ level : compressionOptions.level || -1 // default compression
@Stuk
Stuk Feb 19, 2015 Owner

Is there a risk of compressionOptions being undefined?

@Stuk
Owner
Stuk commented Feb 19, 2015

Otherwise looks good 👍

@Stuk Stuk merged commit d329558 into Stuk:master Feb 20, 2015

1 check passed

continuous-integration/travis-ci/pr The Travis CI build passed
Details
@dduponchel dduponchel deleted the dduponchel:deflate_level branch Feb 20, 2015
@dduponchel dduponchel referenced this pull request Mar 10, 2015
Merged

Release v2.5.0 #206

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment