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
Show file tree
Hide file tree
Changes from all commits
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
34 changes: 34 additions & 0 deletions README.markdown
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,40 @@ images/
smile.gif
*/
```

JSZip中文例子
-------

>API文档:https://stuk.github.io/jszip
```javascript
/* 创建一个压缩文件 */
const zip = new JSZip();
zip.file("Hello.txt", "Hello World\n"); //添加一个文本到压缩文件
const img = zip.folder("images");//添加一个images文件夹
img.file("smile.gif", imgData, {base64: true});//images文件夹添加图片
zip.generateAsync({type:"blob"}).then(function(content) {
saveAs(content, "example.zip");//下载压缩包
});
/* 解压文件 */
let data = await (await fetch('example.zip')).arrayBuffer();//下载example.zip
JSZip.loadAsync(data,{'charset':'gbk'}).then(ZIP=>{
//charset:'gbk' 可以使得某些中文压缩包文件名乱码问题得到解决。
console.log(ZIP.files);
/*
for (var file in ZIP.files) {
ZIP.file(file).async("uint8array").then(data => {
var blob = new Blob([data], {
type: "application/binary"
});
var url = URL.createObjectURL(blob);
//document.querySelector('#img').src=url;
});
}
*/
});

```

License
-------

Expand Down
82 changes: 60 additions & 22 deletions dist/jszip.js
Original file line number Diff line number Diff line change
Expand Up @@ -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)) {
Expand Down Expand Up @@ -4026,30 +4027,67 @@ ZipEntry.prototype = {
/**
* Apply an UTF8 transformation if needed.
*/

decode:function(u8,i){
i = i||0;
var charset = this.loadOptions.charset;
if(charset&&charset!='utf8'){
for(;i<u8.byteLength;i++){
if(u8[i]>127){
//not a ascii
var 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&&charset=='gbk'){
//double byte
//some gbk will erro
//return this.decode(u8,j);
}else{
utf8 = true;
}
}
if(utf8===false)return new TextDecoder(charset).decode(u8);
break;
}
}
}
return new TextDecoder().decode(u8);
},
handleUTF8: function() {
var charset = this.loadOptions.charset,
utf8decode = utf8.utf8decode,
decode = this.loadOptions.decodeFileName||utf8decode;
if(charset&&'TextDecoder' in window&&'Uint8Array' in window){
this.fileNameStr = this.decode(this.fileName);
this.fileCommentStr = this.decode(this.fileComment);
}else if(this.useUTF8()){
this.fileNameStr = utf8decode(this.fileName);
this.fileCommentStr = utf8decode(this.fileComment);
}else{
var decodeParamType = support.uint8array ? "uint8array" : "array";
if (this.useUTF8()) {
this.fileNameStr = utf8.utf8decode(this.fileName);
this.fileCommentStr = utf8.utf8decode(this.fileComment);
var upath = this.findExtraFieldUnicodePath();
if (upath !== null) {
this.fileNameStr = upath;
} else {
var upath = this.findExtraFieldUnicodePath();
if (upath !== null) {
this.fileNameStr = upath;
} else {
// ASCII text or unsupported code page
var fileNameByteArray = utils.transformTo(decodeParamType, this.fileName);
this.fileNameStr = this.loadOptions.decodeFileName(fileNameByteArray);
}

var ucomment = this.findExtraFieldUnicodeComment();
if (ucomment !== null) {
this.fileCommentStr = ucomment;
} else {
// ASCII text or unsupported code page
var commentByteArray = utils.transformTo(decodeParamType, this.fileComment);
this.fileCommentStr = this.loadOptions.decodeFileName(commentByteArray);
}
// ASCII text or unsupported code page
var fileNameByteArray = utils.transformTo(decodeParamType, this.fileName);
this.fileNameStr = decode(fileNameByteArray);
}
var ucomment = this.findExtraFieldUnicodeComment();
if (ucomment !== null) {
this.fileCommentStr = ucomment;
} else {
// ASCII text or unsupported code page
var commentByteArray = utils.transformTo(decodeParamType, this.fileComment);
this.fileCommentStr = decode(commentByteArray);
}
}
},

/**
Expand Down Expand Up @@ -11580,4 +11618,4 @@ module.exports = ZStream;

}).call(this,typeof global !== "undefined" ? global : typeof self !== "undefined" ? self : typeof window !== "undefined" ? window : {})
},{}]},{},[10])(10)
});
});
76 changes: 56 additions & 20 deletions lib/zipEntry.js
Original file line number Diff line number Diff line change
Expand Up @@ -217,30 +217,66 @@ ZipEntry.prototype = {
/**
* Apply an UTF8 transformation if needed.
*/
decode:function(u8,i){
i = i||0;
var charset = this.loadOptions.charset;
if(charset&&charset!='utf8'){
for(;i<u8.byteLength;i++){
if(u8[i]>127){
//not a ascii
var 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&&charset=='gbk'){
//double byte
//some gbk will erro
//return this.decode(u8,j);
}else{
utf8 = true;
}
}
if(utf8===false)return new TextDecoder(charset).decode(u8);
break;
}
}
}
return new TextDecoder().decode(u8);
},
handleUTF8: function() {
var charset = this.loadOptions.charset,
utf8decode = utf8.utf8decode,
decode = this.loadOptions.decodeFileName||utf8decode;
if(charset&&'TextDecoder' in window&&'Uint8Array' in window){
this.fileNameStr = this.decode(this.fileName);
this.fileCommentStr = this.decode(this.fileComment);
}else if(this.useUTF8()){
this.fileNameStr = utf8decode(this.fileName);
this.fileCommentStr = utf8decode(this.fileComment);
}else{
var decodeParamType = support.uint8array ? "uint8array" : "array";
if (this.useUTF8()) {
this.fileNameStr = utf8.utf8decode(this.fileName);
this.fileCommentStr = utf8.utf8decode(this.fileComment);
var upath = this.findExtraFieldUnicodePath();
if (upath !== null) {
this.fileNameStr = upath;
} else {
var upath = this.findExtraFieldUnicodePath();
if (upath !== null) {
this.fileNameStr = upath;
} else {
// ASCII text or unsupported code page
var fileNameByteArray = utils.transformTo(decodeParamType, this.fileName);
this.fileNameStr = this.loadOptions.decodeFileName(fileNameByteArray);
}

var ucomment = this.findExtraFieldUnicodeComment();
if (ucomment !== null) {
this.fileCommentStr = ucomment;
} else {
// ASCII text or unsupported code page
var commentByteArray = utils.transformTo(decodeParamType, this.fileComment);
this.fileCommentStr = this.loadOptions.decodeFileName(commentByteArray);
}
// ASCII text or unsupported code page
var fileNameByteArray = utils.transformTo(decodeParamType, this.fileName);
this.fileNameStr = decode(fileNameByteArray);
}
var ucomment = this.findExtraFieldUnicodeComment();
if (ucomment !== null) {
this.fileCommentStr = ucomment;
} else {
// ASCII text or unsupported code page
var commentByteArray = utils.transformTo(decodeParamType, this.fileComment);
this.fileCommentStr = decode(commentByteArray);
}
}
},

/**
Expand Down