有没有一种方法可以通过代码将长字符串压缩为较小的字符串,反之亦然?

问题描述 投票:0回答:3

我正在为现有的 JavaScript 网络工具开发一个插件。 我们正在收集数据并将其存储在如下字符串中: “2.545,3.552,8.568;2.553,9.898,6.542;...”等等。

问题是,我们太快达到了 64k 字符的导出限制。 我想问 - 如果这是一个愚蠢的问题,请原谅 - 如果有人知道 我们可以在导出字符串之前使用它来压缩字符串的算法或方法。我确信这在技术上是可行的,但它肯定超出了我作为程序员的技能。

感谢您的任何提示、链接或建议。

javascript string encryption compression
3个回答
5
投票

lz-string看起来会起作用。

var string = "2.545,3.552,8.568;2.553,9.898,6.542";
alert("Size of sample is: " + string.length);
var compressed = LZString.compress(string);
alert("Size of compressed sample is: " + compressed.length);
string = LZString.decompress(compressed);
alert("Sample is: " + string);
<script src="https://cdn.jsdelivr.net/gh/pieroxy/lz-string/libs/lz-string.js"></script>


0
投票

我建议您为此使用专用的Web Worker。 mozilla 开发者页面上有关于 Using Web Worker 的很好的解释。


0
投票

您可以使用

fflate
来压缩/解压缩字符串。

在您的 HTML 代码中添加:

<script src="https://unpkg.com/[email protected]"></script>

在您的 JavaScript 代码中添加:

function zip_encode(str) {
    const ascii = encodeURIComponent(str)
    const array = new TextEncoder().encode(ascii)
    const zip = fflate.deflateSync(array, {level: 9})
    return window.btoa(String.fromCharCode(...zip))
}

function zip_decode(base64) {
    const raw = window.atob(base64)
    const array = Uint8Array.from(raw, c => c.charCodeAt(0))
    const unzip = fflate.inflateSync(array)
    const ascii = new TextDecoder().decode(unzip)
    return decodeURIComponent(ascii)
}

测试此设置:

// Example usage
const example = "Hello, this is a sample '😎' icon!"
console.log("Original String:", example)
// Original String: Hello, this is a sample '😎' icon!

const compressed = zip_encode(example)
console.log("Compressed Data:", compressed)
// Compressed Data: 80jNyclXNXJWNTIoycgsBlJgIhGIixNzC3JSgQx1VTcDVUs3VUsLVQtXdZCS5Pw8RQA=

const example_decompressed = zip_decode(compressed)
console.log("Decompressed String:", example_decompressed)
// Decompressed String: Hello, this is a sample '😎' icon!

function zip_encode(str) {
    const ascii = encodeURIComponent(str)
    const array = new TextEncoder().encode(ascii)
    const zip = fflate.deflateSync(array, {level: 9})
    return window.btoa(String.fromCharCode(...zip))
}

function zip_decode(base64) {
    const raw = window.atob(base64)
    const array = Uint8Array.from(raw, c => c.charCodeAt(0))
    const unzip = fflate.inflateSync(array)
    const ascii = new TextDecoder().decode(unzip)
    return decodeURIComponent(ascii)
}

// Example usage
const example = "Hello, this is a sample '😎' icon!"
console.log("Original String:", example)
// Original String: Hello, this is a sample '😎' icon!

const compressed = zip_encode(example)
console.log("Compressed Data:", compressed)
// Compressed Data: 80jNyclXNXJWNTIoycgsBlJgIhGIixNzC3JSgQx1VTcDVUs3VUsLVQtXdZCS5Pw8RQA=

const example_decompressed = zip_decode(compressed)
console.log("Decompressed String:", example_decompressed)
// Decompressed String: Hello, this is a sample '😎' icon!
<script src="https://unpkg.com/[email protected]"></script>

© www.soinside.com 2019 - 2024. All rights reserved.