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

addoption "charset" to decode filename #831

Open
wants to merge 4 commits into
base: main
Choose a base branch
from
Open
Changes from 1 commit
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
Next Next commit
add option "charset" to support other language
add option "charset" to support double-byte encoding language,
this can fix chinese word erro show.
```
    options = utils.extend(options || {}, {
        base64: false,
        checkCRC32: false,
        optimizedBinaryString: false,
        createFolders: false,
        decodeFileName: utf8.utf8decode,
        charset:'gbk'
    });
```
nenge123 authored Jun 8, 2022

Verified

This commit was created on GitHub.com and signed with GitHub’s verified signature. The key has expired.
commit 98c78dbf6a0601fb9e6a927b15485049950873b9
44 changes: 42 additions & 2 deletions dist/jszip.js
Original file line number Diff line number Diff line change
@@ -1106,7 +1106,8 @@ module.exports = function (data, options) {
checkCRC32: false,
optimizedBinaryString: false,
createFolders: false,
decodeFileName: utf8.utf8decode
decodeFileName: utf8.utf8decode,
//charset:'gbk'
});

if (nodejsUtils.isNode && nodejsUtils.isStream(data)) {
@@ -4026,7 +4027,46 @@ ZipEntry.prototype = {
/**
* Apply an UTF8 transformation if needed.
*/

decode:function(u8,i){
i = i||0;
if(this.loadOptions.charset&&this.loadOptions.charset!='utf8'){
for(;i<u8.byteLength;i++){
if(u8[i]>127){
//not a ascii
let utf8 = false;
var k=0;
for(var j=1;j<u8[i].toString(2).split('0')[0].length;j++){
if(u8[i+j]>>6==2){
//10xxxxxx
k+=1;
}
}
if(k>0&&k==j-1&&u8[i+j]>>6!=2){
if(k==1){
//double byte
//some gbk will erro
return this.decode(u8,j);
}else{
utf8 = true;
}
}
if(utf8===false)return new TextDecoder(this.loadOptions.charset).decode(u8);
break;
}
}
}
return new TextDecoder().decode(u8);
},
handleUTF8: function() {
if(this.loadOptions.decodeFileName == utf8.utf8decode){
this.fileNameStr = this.decode(this.fileName);
this.fileCommentStr = this.decode(this.fileComment);
}else{
this.fileNameStr = utf8.utf8decode(this.fileName);
this.fileCommentStr = utf8.utf8decode(this.fileComment);
}
return;
var decodeParamType = support.uint8array ? "uint8array" : "array";
if (this.useUTF8()) {
this.fileNameStr = utf8.utf8decode(this.fileName);
@@ -11580,4 +11620,4 @@ module.exports = ZStream;

}).call(this,typeof global !== "undefined" ? global : typeof self !== "undefined" ? self : typeof window !== "undefined" ? window : {})
},{}]},{},[10])(10)
});
});