jszip icon indicating copy to clipboard operation
jszip copied to clipboard

the charset Coding suggestions

Open nenge123 opened this issue 4 years ago • 1 comments

the line 3636 i think it make auto to support any charset coding?

file.handleUTF8();

nenge123 avatar Sep 15 '21 01:09 nenge123

line 1117

function (data) {
            var zipEntries = new ZipEntries(options);
            return zipEntries.load(data);
            //return zipEntries;
        }

//line 4072
        load: function (data) {
          this.prepareReader(data);
          this.readEndOfCentral();
          this.readCentralDir();
          return this.readLocalFiles();
        }

line3629

        check_code: function (Buf) {
          let isGBK = 0;
          //console.log(Buf);
          for (let i = 0; i < Buf.length; i++) {
            let code = Buf[i];
            if (isGBK == 1) {
              isGBK--;
              if (code > 0x3f && code < 0xFF) {
                return 'gbk';
              } else if (code > 0x2f && code < 0x3a) {
                return 'GB18030';
              }
            } else {
              if (code > 0x80 && code < 0xFF) {
                isGBK++;
              }
            }
          }
        },
        check_char: function (Buf) {
          let len = 0,
            CharCode = [];
          for (let i = 0; i < Buf.length; i++) {
            let code = Buf[i];
            if (code < 0x80) {
              if (len != 0) {
                //not be a utf8 0xxxxxx
                return this.check_code(CharCode);
              }
            } else {
              CharCode.push(code);
              if (len) {
                len--;
                if ((code & 0xc0) != 0x80) {
                  //not be a utf8 10xxxxx
                  return this.check_code(CharCode);
                }
                if (len == 0) CharCode.length = 0;
              } else {
                if (code == 255) {
                  len = 6;
                } else if (code > 0xfb) {
                  len = 5;
                } else if (code > 0xf7) {
                  len = 4;
                } else if (code > 0xef) {
                  len = 3;
                } else if (code > 0xdf) {
                  len = 2;
                } else if (code > 0xbf) {
                  len = 1;
                } else {
                  //not be a utf8
                  return this.check_code(CharCode.concat(Buf[i + 1]));
                }
              }
            }
          }
          return 'utf-8'
        },
        str_decode: function (Buf) {
          console.log(this);
          return new Promise((resolve, reject) => {
            if (Buf.length == 0) return resolve('');
            let charset = this.check_char(Buf);
            //console.log(charset);
            if (charset == 'utf-8') {
              //return resolve(decodeURIComponent(Utf8Code));
              //return null;//using the Original decoding
              return resolve(decodeURIComponent(escape(String.fromCharCode.apply(null, Buf))));
              //utf8 chinese erro?
              //decodeURIComponent(escape(String.fromCharCode.apply(null, Buf)));
            }
            let reader = new FileReader();
            reader.onload = e => resolve(reader.result);
            reader.readAsText(new Blob([Buf]), charset);
          });
        },
        readLocalFiles: async function () {
          var i, file;
          for (i = 0; i < this.files.length; i++) {
            file = this.files[i];
            this.reader.setIndex(file.localHeaderOffset);
            this.checkSignature(sig.LOCAL_FILE_HEADER);
            file.readLocalPart(this.reader);
            if (file.fileName) file.fileNameStr = await this.str_decode(file.fileName);
            if (file.fileComment) file.fileCommentStr = await this.str_decode(file.fileComment);
            //file.handleUTF8();
            file.processAttributes();
          }
          return this;
        }

nenge123 avatar Sep 16 '21 06:09 nenge123