diff --git a/README.markdown b/README.markdown index 2f2ea2ed..fb7d6728 100644 --- a/README.markdown +++ b/README.markdown @@ -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 ------- diff --git a/dist/jszip.js b/dist/jszip.js index 473c7ab0..b6aca5a7 100644 --- a/dist/jszip.js +++ b/dist/jszip.js @@ -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,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(;i127){ + //not a ascii + var utf8 = false; + var k=0; + for(var j=1;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); } + } }, /** @@ -11580,4 +11618,4 @@ module.exports = ZStream; }).call(this,typeof global !== "undefined" ? global : typeof self !== "undefined" ? self : typeof window !== "undefined" ? window : {}) },{}]},{},[10])(10) -}); \ No newline at end of file +}); diff --git a/lib/zipEntry.js b/lib/zipEntry.js index f204524e..6394b81f 100644 --- a/lib/zipEntry.js +++ b/lib/zipEntry.js @@ -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(;i127){ + //not a ascii + var utf8 = false; + var k=0; + for(var j=1;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); } + } }, /**