Comments (4)
Could you try changing the code to
if x?.overrideMimeType
x.overrideMimeType 'application/zip'
and see if that helps?
from jszip.
Hi Stuk,
as I understand it, 'text/plain; charset=x-user-defined' is the only way to keep the browser from destroying binary data that comes from an ajax call. Source: https://developer.mozilla.org/en-US/docs/DOM/XMLHttpRequest/Sending_and_Receiving_Binary_Data
And it is the only way I found that data.length is actually equal to the file size on the server. So I guess the source data is alright.
I tried what you proposed anyway. Now Chrome won't even open the link. (error message below) I guess it is because the base4 string is too short. Same with Safari.
This webpage is not available
The webpage at data:application/zip;base64,UEsDBAoAAAAAAIVGMkEAAAAAAAAAAAAAAAAHAAAAYXNzZXRzL1BLAwQKAAAAAACFRjJB+hLo/CoDAAAqAwAAEgAAAGFzc2V0cy9jZWxsXzRfaWNvb1QTkcNChoKAAAADUlIRFIAAAAQAAAAEAgGAAAAH9YQAAAARnQU1BAA9TcF0AAAAZdEVYdFNvZnR3YXJlAEFkb2JlIEltYWdlUmVhZHlxWU8AAACUlEQVQYGQ9S29VR905Vsm1JehlQU01EQ9C9Wi9Z0WF2F1GcCEiSBCd9kQ9cC9YG9Fm9SVoU20gJ0618M9X99HicJS99W1yTsmS9AH9HghmC0Nfe6eG9B0kA9R8ufjYze9bk9IQAAQ99fW0sd9f0fXRmRsAAH90hG1907Bw5kW9K0ic9AA9G1ia399X16VwAAOD90t2K9ZCglLBZE04ch9W91qc9U9X00AG9H0mU9Sn0vf9o9V5OT99WGU3zljR9Um9IyNc9Fz99xUG1218HU9T0PRsvR1+XIWyG9T9U0LP9U9S904W9C0uW9908Ty9Mg9l0wRAeSdYSkcU9UEUcxpQF0FXH9V8fUSpyCgZoX0WV24iLw9IC9JEHR9eSwa0DY99G9R9y9Dw9XQ9cC9Z0t13S5/9R9A99999Q39P99B88VpyS9JA9e9fHZm5wW9fm9Vi9UB30E9ZEIODjc99CQAAFkU+S9T9N18WT9LTYIU9B0EBg9StRd0AAHB/f69W3xVe9B9SAJQMTC1AV9XIzJRdCIBAG99H29S905IRwAJnAIE07TlCcB9QAA9X0fGR99tLQgMAB9BQAk0cW91O0kA1OT8VCz9TsiQcAU9EAAI9X0wT9jX9NS9IwAAAABJRU5EUJgVBLAwQKAAAAAACFRjJBeXXXfJ4AAACeAAAADAAAAGNvbnRlbnQuanNvbnsibm9kZV8xX2NoaWxkcmVuIjp7IjIvMSI6M30sIm5vZGVfMV9jZWxscyI6eyIxLzEiOjAsIjIvMSI6NH0sIm5vZGVfM19jZWxscyI6eyIxLzAiOjJ9LCJjZWxsXzQiOnsiaWNvbiI6Ii4vYXNzZXRzL2NlbGxfNF9pY29uIn0sImNlbGxfMiI6eyJsYWJlbCI6IkVpbiBUZXN0In19UEsBAhQACgAAAAAAhUYyQQAAAAAAAAAAAAAAAAcAAAAAAAAAAAAAAAAAAAAAAGFzc2V0cy9QSwECFAAKAAAAAACFRjJB+hLo/CoDAAAqAwAAEgAAAAAAAAAAAAAAAAAlAAAAYXNzZXRzL2NlbGxfNF9pY29uUEsBAhQACgAAAAAAhUYyQXl113yeAAAAngAAAAwAAAAAAAAAAAAAAAAAfwMAAGNvbnRlbnQuanNvblBLBQYAAAAAAwADAK8AAABHBAAAAAA= might be temporarily down or it may have moved permanently to a new web address.
from jszip.
Ok, I solved it. Turns out that the ajax call returns garbage in the upper byte of each character. The lower bytes of the 16bit unicode character reflect the proper byte value, the upper byte often is zero but sometime is not. ANd that messes up not only JSZip but also jquery.base64 for example.
Her eis a complete test page with a loop to zero out the upper byte of each character. I keep looking for a better solution that does not require touching every byte. (or maybe you can just add a '& 0xff' somewhere to solve this.
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html>
<head>
<script src="jquery.min.js"></script>
<script src="jszip.js"></script>
</head>
<body></body>
<script type="text/javascript">
$(function() {
var assets, url, zip;
zip = new JSZip();
assets = zip.folder('assets');
url = 'http://images.apple.com/global/elements/flags/22x22/usa.png';
$.ajax({
url: url,
dataType: 'text',
beforeSend: function(x) {
if (x != null ? x.overrideMimeType : void 0) {
return x.overrideMimeType('text/plain; charset=x-user-defined');
}
},
success: function(data) {
var code, s2, x, zipFileLink;
console.log("received " + data.length + " bytes");
s2 = "";
for (x = 0; x<data.length; ++x) {
code = data.charCodeAt(x) & 0xff;
s2 += String.fromCharCode(code);
}
data = s2;
assets.file("test.png", data, {
base64: false,
binary: true
});
zipFileLink = "data:application/zip;base64," + zip.generate({
base64: true,
compression: 'STORE'
});
return $("body").append($("<a>").attr("href", zipFileLink).html('zip file'));
}
});
});
</script>
</html>
from jszip.
This is a problem someone else has recently come across. It's a result of using strings to store binary data, and can really only be solved by moving to blobs and typed arrays (a task for another time). Doing an & 0xff internally would still require touching every byte sadly.
from jszip.
Related Issues (20)
- Missing folders when trying to extract files which are compressed using native compression software of Windows HOT 1
- Unable to open docx file in Node
- Same zip different zipContents when loadAsync
- JSZip v3.10+ breaks in some sandboxed browser environment because dependency setimmediate breaks HOT 1
- Can we generate LF instead of CRLF
- Reading Folder Doesn't Work
- Large zip file breaks with loadAsync
- Math.random() usage
- Is jszip unmaintained? HOT 9
- Zipping 1GB+ and splitting to chunks is slow - is there a way to speed it up?
- Can't use ReadableStream from generateNodeStream in the PutObjectCommand of s3
- escape and unescape are deprecated
- Error: End of data reached
- Failed to resolve module specifier.
- ZIP64 Support broken HOT 2
- Add asyncIterator support to NodejsStreamOutputAdapter
- Is there a recommended tool library for parsing rar format compression packages ?
- Can't unzip some zip files HOT 1
- (MIT OR GPL-3.0-or-later) HOT 1
- How to pass the loadAsync method into arraybuffer
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
D3
Bring data to life with SVG, Canvas and HTML. 📊📈🎉
-
Recommend Topics
-
javascript
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
-
web
Some thing interesting about web. New door for the world.
-
server
A server is a program made to process requests and deliver data to clients.
-
Machine learning
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from jszip.