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

Add support for Blob, ArrayBuffer, Uint8Array #28

Merged
merged 15 commits into from Jan 14, 2013
Merged

Conversation

dduponchel
Copy link
Collaborator

These commits add :

  • support for Blob, ArrayBuffer, Uint8Array (when applicable) for loading, updating and generating zip files
  • deprecate generate({base64:true}) for generate({type:"base64"})
  • add examples of how to use these additions

I tested it in :

  • IE 6/7/8/9
  • Firefox 3/3.6/4/10(esr)/17(esr)
  • Chrome latest
  • Safari 5.1/6

This should resolve the issues #23 and #27

Now that we support ArrayBuffer, we can do that very easily.
Bad copy and paste.
Bad news : I added a loop to apply a 0xFF mask. this will slow down a
bit the method.
Good news :
 * JSZip.load should be more robust (sometimes the 0xFF wasn't
   used, when using lastIndexOf for example).
 * Now that we are sure that the string is in the right format, we can
   use String.slice to read a string. The old method was :
   foreach byte :
     charCodeAt, & 0xFF, fromCharCode, append to the result string
   This should compensate for the initial loop.
 * Debugging is less painful : the old method triggered 1 call to
   readByte... per byte. With "big" zip files (let say 10M), this would
   lead to 10M method calls. The "with firebug activated" case was on my
   tests 10 times slower than the "without firebug".
   Now that we only do 1 method call (String.slice) to read to content
   of a file, the debugger overhead has been reduced.
 * JSZip.load(Uint8Array|ArrayBuffer) is faster : we avoid the useless
   charCodeAt, & 0xFF, fromCharCode.
We now support
* file(name, string)
* file(name, Uint8Array)
* file(name, ArrayBuffer)
and
* file(name).asText()
* file(name).asBinary()
* file(name).asUint8Array()
* file(name).asArrayBuffer()
When a file is added with a content as a base64 string (with the base64
attribute), the asText() method didn't decode the text.
Tested browsers :
IE 6/7/8/9
Firefox 3/3.6/4/10(esr)/17(esr)
Chrome latest
Safari 5.1/6

I updated jQuery because it seems to fix an issue with some zip files...

Add JSZip.support because testing support might be hard.
Example : typeof Blob !== "undefined" is not enough : Firefox 4 and
Safari 5 know about Blob but not how to create them (no Blob
constructor, no BlobBuilder).
With a JSZip.support.blob, I can use it in the unit tests and the user
can use generate() or asBlob() without any try/catch blocks.
* use type instead of responseType
* deprecate generate({base64:true})
* improve documentation
@Stuk
Copy link
Owner

Stuk commented Jan 5, 2013

Thanks David. I'll attempt to review the code by Wednesday.

*/
function StreamReader(stream) {
this.stream = stream;
this.stream = "";
if (typeof Uint8Array !== "undefined" && stream instanceof Uint8Array)
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Would it be better to use JSZip.support.uint8array? and JSZip.support.arraybuffer below?

@Stuk
Copy link
Owner

Stuk commented Jan 13, 2013

Sorry, a bit late. All looks good, just a couple of comments :)

@dduponchel
Copy link
Collaborator Author

Thanks for the review, I'll fix that :)

I have a question about the coding style and brackets : should I prefer

if (test)
{
   doStuff();
}

or

if (test) {
   doStuff();
}

? I just found the commit 1459728 which changes some brackets from the former to the latter, but not everywhere. In this pull request, I used the first option but I can update all the files to use the good coding style.

@Stuk
Copy link
Owner

Stuk commented Jan 13, 2013

Thanks for the updates. I now prefer the latter as it's shorter without sacrificing readability.

@dduponchel
Copy link
Collaborator Author

I'll update the code then :)

See pull request 28 :
Change
if (test)
{
   doStuff();
}
to
if (test) {
   doStuff();
}

Also remove an extra (unused) `this` and fix a typo.
Stuk added a commit that referenced this pull request Jan 14, 2013
Add support for Blob, ArrayBuffer, Uint8Array
@Stuk Stuk merged commit 5abc404 into Stuk:master Jan 14, 2013
@Stuk
Copy link
Owner

Stuk commented Jan 14, 2013

Great, thanks David!

@dduponchel dduponchel deleted the issue23 branch January 14, 2013 20:56
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

2 participants