Explorar el Código

编解码工具中增加gzip持

zxlie hace 3 meses
padre
commit
b9ee7ecd1c
Se han modificado 3 ficheros con 170 adiciones y 72 borrados
  1. 73 1
      apps/en-decode/endecode-lib.js
  2. 6 0
      apps/en-decode/index.html
  3. 91 71
      apps/en-decode/index.js

+ 73 - 1
apps/en-decode/endecode-lib.js

@@ -523,6 +523,76 @@ let EncodeUtils = (() => {
         // 返回格式化的JSON
         return cookiesArray;
     }
+
+    /**
+     * 使用gzip压缩文本(返回Base64编码的结果)
+     * @param {string} text - 需要压缩的文本
+     * @returns {Promise<string>} 压缩后的Base64字符串
+     */
+    let _gzipEncode = async function(text) {
+        try {
+            // 检查浏览器是否支持CompressionStream API
+            if (typeof CompressionStream === 'undefined') {
+                throw new Error('当前浏览器不支持CompressionStream API,请使用Chrome 80+、Firefox 113+或Safari 16.4+');
+            }
+            
+            // 将文本转换为Uint8Array
+            const encoder = new TextEncoder();
+            const data = encoder.encode(text);
+            
+            // 创建压缩流
+            const compressionStream = new CompressionStream('gzip');
+            const compressedStream = new Response(data).body.pipeThrough(compressionStream);
+            
+            // 读取压缩后的数据
+            const compressedArrayBuffer = await new Response(compressedStream).arrayBuffer();
+            
+            // 将ArrayBuffer转换为Base64字符串
+            const compressedArray = new Uint8Array(compressedArrayBuffer);
+            let binaryString = '';
+            for (let i = 0; i < compressedArray.length; i++) {
+                binaryString += String.fromCharCode(compressedArray[i]);
+            }
+            
+            return btoa(binaryString);
+        } catch (error) {
+            throw new Error('Gzip压缩失败: ' + error.message);
+        }
+    };
+
+    /**
+     * 使用gzip解压缩Base64编码的数据
+     * @param {string} compressedBase64 - Base64编码的压缩数据
+     * @returns {Promise<string>} 解压缩后的文本
+     */
+    let _gzipDecode = async function(compressedBase64) {
+        try {
+            // 检查浏览器是否支持DecompressionStream API
+            if (typeof DecompressionStream === 'undefined') {
+                throw new Error('当前浏览器不支持DecompressionStream API,请使用Chrome 80+、Firefox 113+或Safari 16.4+');
+            }
+            
+            // 将Base64字符串转换为Uint8Array
+            const binaryString = atob(compressedBase64);
+            const compressedArray = new Uint8Array(binaryString.length);
+            for (let i = 0; i < binaryString.length; i++) {
+                compressedArray[i] = binaryString.charCodeAt(i);
+            }
+            
+            // 创建解压缩流
+            const decompressionStream = new DecompressionStream('gzip');
+            const decompressedStream = new Response(compressedArray).body.pipeThrough(decompressionStream);
+            
+            // 读取解压缩后的数据
+            const decompressedArrayBuffer = await new Response(decompressedStream).arrayBuffer();
+            
+            // 将ArrayBuffer转换为文本
+            const decoder = new TextDecoder();
+            return decoder.decode(decompressedArrayBuffer);
+        } catch (error) {
+            throw new Error('Gzip解压缩失败: ' + error.message);
+        }
+    };
     
 
     return {
@@ -541,7 +611,9 @@ let EncodeUtils = (() => {
         urlParamsDecode: _urlParamsDecode,
         sha1Encode: _sha1Encode,
         jwtDecode,
-        formatCookieStringToJson
+        formatCookieStringToJson,
+        gzipEncode: _gzipEncode,
+        gzipDecode: _gzipDecode
     };
 })();
 

+ 6 - 0
apps/en-decode/index.html

@@ -61,6 +61,9 @@
                             <div class="radio ui-d-ib ui-mr-20">
                                 <label><input type="radio" name="codeType" value="html2js" v-model="selectedType" @click="convert()">HTML转JS</label>
                             </div>
+                            <div class="radio ui-d-ib ui-mr-20">
+                                <label><input type="radio" name="codeType" value="gzipEncode" v-model="selectedType" @click="convert()">Gzip压缩</label>
+                            </div>
                         </td>
                     </tr>
                     <tr>
@@ -93,6 +96,9 @@
                             <div class="radio ui-d-ib ui-mr-20">
                                 <label><input type="radio" name="codeType" value="cookieDecode" v-model="selectedType" @click="convert()">Cookie格式化</label>
                             </div>
+                            <div class="radio ui-d-ib ui-mr-20">
+                                <label><input type="radio" name="codeType" value="gzipDecode" v-model="selectedType" @click="convert()">Gzip解压</label>
+                            </div>
                         </td>
                     </tr>
                 </table>

+ 91 - 71
apps/en-decode/index.js

@@ -33,80 +33,100 @@ new Vue({
         this.$refs.srcText.focus();
     },
     methods: {
-        convert: function () {
-            this.$nextTick(() => {
+        convert: async function () {
+            this.$nextTick(async () => {
                 this.urlResult = null;
 
-                if (this.selectedType === 'uniEncode') {
-
-                    this.resultContent = EncodeUtils.uniEncode(this.sourceContent);
-                } else if (this.selectedType === 'uniDecode') {
-
-                    this.resultContent = EncodeUtils.uniDecode(this.sourceContent.replace(/\\U/g, '\\u'));
-                } else if (this.selectedType === 'utf8Encode') {
-
-                    this.resultContent = encodeURIComponent(this.sourceContent);
-                } else if (this.selectedType === 'utf8Decode') {
-
-                    this.resultContent = decodeURIComponent(this.sourceContent);
-                } else if (this.selectedType === 'utf16Encode') {
-
-                    this.resultContent = EncodeUtils.utf8to16(encodeURIComponent(this.sourceContent));
-                } else if (this.selectedType === 'utf16Decode') {
-
-                    this.resultContent = decodeURIComponent(EncodeUtils.utf16to8(this.sourceContent));
-                } else if (this.selectedType === 'base64Encode') {
-
-                    this.resultContent = EncodeUtils.base64Encode(EncodeUtils.utf8Encode(this.sourceContent));
-                } else if (this.selectedType === 'base64Decode') {
-
-                    this.resultContent = EncodeUtils.utf8Decode(EncodeUtils.base64Decode(this.sourceContent));
-                } else if (this.selectedType === 'md5Encode') {
-
-                    this.resultContent = EncodeUtils.md5(this.sourceContent);
-                } else if (this.selectedType === 'hexEncode') {
-
-                    this.resultContent = EncodeUtils.hexEncode(this.sourceContent);
-                } else if (this.selectedType === 'hexDecode') {
-
-                    this.resultContent = EncodeUtils.hexDecode(this.sourceContent);
-                } else if (this.selectedType === 'html2js') {
-
-                    this.resultContent = EncodeUtils.html2js(this.sourceContent);
-                } else if (this.selectedType === 'sha1Encode') {
-
-                    this.resultContent = EncodeUtils.sha1Encode(this.sourceContent);
-                } else if (this.selectedType === 'htmlEntityEncode') {
-
-                    this.resultContent = he.encode(this.sourceContent, {
-                        'useNamedReferences': true,
-                        'allowUnsafeSymbols': true
-                    });
-                } else if (this.selectedType === 'htmlEntityFullEncode') {
-
-                    this.resultContent = he.encode(this.sourceContent, {
-                        'encodeEverything': true,
-                        'useNamedReferences': true,
-                        'allowUnsafeSymbols': true
-                    });
-                } else if (this.selectedType === 'htmlEntityDecode') {
-
-                    this.resultContent = he.decode(this.sourceContent, {
-                        'isAttributeValue': false
-                    });
-                } else if (this.selectedType === 'urlParamsDecode') {
-                    let res = EncodeUtils.urlParamsDecode(this.sourceContent);
-                    if (res.error) {
-                        this.resultContent = res.error;
-                    } else {
-                        this.urlResult = res;
+                try {
+                    if (this.selectedType === 'uniEncode') {
+
+                        this.resultContent = EncodeUtils.uniEncode(this.sourceContent);
+                    } else if (this.selectedType === 'uniDecode') {
+
+                        this.resultContent = EncodeUtils.uniDecode(this.sourceContent.replace(/\\U/g, '\\u'));
+                    } else if (this.selectedType === 'utf8Encode') {
+
+                        this.resultContent = encodeURIComponent(this.sourceContent);
+                    } else if (this.selectedType === 'utf8Decode') {
+
+                        this.resultContent = decodeURIComponent(this.sourceContent);
+                    } else if (this.selectedType === 'utf16Encode') {
+
+                        this.resultContent = EncodeUtils.utf8to16(encodeURIComponent(this.sourceContent));
+                    } else if (this.selectedType === 'utf16Decode') {
+
+                        this.resultContent = decodeURIComponent(EncodeUtils.utf16to8(this.sourceContent));
+                    } else if (this.selectedType === 'base64Encode') {
+
+                        this.resultContent = EncodeUtils.base64Encode(EncodeUtils.utf8Encode(this.sourceContent));
+                    } else if (this.selectedType === 'base64Decode') {
+
+                        this.resultContent = EncodeUtils.utf8Decode(EncodeUtils.base64Decode(this.sourceContent));
+                    } else if (this.selectedType === 'md5Encode') {
+
+                        this.resultContent = EncodeUtils.md5(this.sourceContent);
+                    } else if (this.selectedType === 'hexEncode') {
+
+                        this.resultContent = EncodeUtils.hexEncode(this.sourceContent);
+                    } else if (this.selectedType === 'hexDecode') {
+
+                        this.resultContent = EncodeUtils.hexDecode(this.sourceContent);
+                    } else if (this.selectedType === 'html2js') {
+
+                        this.resultContent = EncodeUtils.html2js(this.sourceContent);
+                    } else if (this.selectedType === 'sha1Encode') {
+
+                        this.resultContent = EncodeUtils.sha1Encode(this.sourceContent);
+                    } else if (this.selectedType === 'htmlEntityEncode') {
+
+                        this.resultContent = he.encode(this.sourceContent, {
+                            'useNamedReferences': true,
+                            'allowUnsafeSymbols': true
+                        });
+                    } else if (this.selectedType === 'htmlEntityFullEncode') {
+
+                        this.resultContent = he.encode(this.sourceContent, {
+                            'encodeEverything': true,
+                            'useNamedReferences': true,
+                            'allowUnsafeSymbols': true
+                        });
+                    } else if (this.selectedType === 'htmlEntityDecode') {
+
+                        this.resultContent = he.decode(this.sourceContent, {
+                            'isAttributeValue': false
+                        });
+                    } else if (this.selectedType === 'urlParamsDecode') {
+                        let res = EncodeUtils.urlParamsDecode(this.sourceContent);
+                        if (res.error) {
+                            this.resultContent = res.error;
+                        } else {
+                            this.urlResult = res;
+                        }
+                    } else if(this.selectedType === 'jwtDecode') {
+                        let {header,payload,sign} = EncodeUtils.jwtDecode(this.sourceContent);
+                        this.resultContent = `Header: ${header}\n\nPayload: ${payload}\n\nSign: ${sign}`;
+                    } else if(this.selectedType === 'cookieDecode') {
+                        let ckJson = EncodeUtils.formatCookieStringToJson(this.sourceContent);
+                        this.resultContent = JSON.stringify(ckJson,null,4);
+                    } else if (this.selectedType === 'gzipEncode') {
+                        // gzip压缩
+                        if (!this.sourceContent.trim()) {
+                            this.resultContent = '请输入需要压缩的文本内容';
+                            return;
+                        }
+                        this.resultContent = '正在压缩...';
+                        this.resultContent = await EncodeUtils.gzipEncode(this.sourceContent);
+                    } else if (this.selectedType === 'gzipDecode') {
+                        // gzip解压缩
+                        if (!this.sourceContent.trim()) {
+                            this.resultContent = '请输入需要解压缩的Base64编码数据';
+                            return;
+                        }
+                        this.resultContent = '正在解压缩...';
+                        this.resultContent = await EncodeUtils.gzipDecode(this.sourceContent);
                     }
-                } else if(this.selectedType === 'jwtDecode') {
-                    let {header,payload,sign} = EncodeUtils.jwtDecode(this.sourceContent);
-                    this.resultContent = `Header: ${header}\n\nPayload: ${payload}\n\nSign: ${sign}`;
-                } else if(this.selectedType === 'cookieDecode') {
-                    let ckJson = EncodeUtils.formatCookieStringToJson(this.sourceContent);
-                    this.resultContent = JSON.stringify(ckJson,null,4);
+                } catch (error) {
+                    this.resultContent = '操作失败: ' + error.message;
                 }
                 this.$forceUpdate();
             });