Skip to content

Commit

Permalink
fix(plugin-compress): Lzstring 编码单独处理
Browse files Browse the repository at this point in the history
  • Loading branch information
Leon406 committed May 6, 2024
1 parent 225a711 commit ec12549
Show file tree
Hide file tree
Showing 6 changed files with 69 additions and 32 deletions.
2 changes: 1 addition & 1 deletion plugin-compress/build.gradle.kts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
group = "me.leon.toolsfx"
version = "1.2.1"
version = "1.2.2"

plugins {
`java-library`
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,12 @@ class CompressController : Controller() {
alg: Compression = Compression.GZIP,
inputEncode: String = "raw",
outputEncode: String = "base64",
): String = alg.compress(raw.decodeToByteArray(inputEncode)).encodeTo(outputEncode)
): String =
if (alg == Compression.LZString) {
Compression.LZString.compress(raw, inputEncode, outputEncode)
} else {
alg.compress(raw.decodeToByteArray(inputEncode)).encodeTo(outputEncode)
}

fun decompress(
raw: String,
Expand All @@ -49,5 +54,10 @@ class CompressController : Controller() {
alg: Compression = Compression.GZIP,
inputEncode: String = "raw",
outputEncode: String = "base64",
) = alg.decompress(raw.decodeToByteArray(inputEncode)).encodeTo(outputEncode)
) =
if (alg == Compression.LZString) {
Compression.LZString.decompress(raw, inputEncode, outputEncode)
} else {
alg.decompress(raw.decodeToByteArray(inputEncode)).encodeTo(outputEncode)
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -13,8 +13,8 @@ import tornadofx.*
import tornadofx.FX.Companion.messages

class CompressView : PluginFragment(messages["compression"]) {
override val version = "v1.2.1"
override val date: String = "2024-04-29"
override val version = "v1.2.2"
override val date: String = "2024-05-06"
override val author = "Leon406"
override val description: String = FX.messages["compression"]

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,10 @@ package me.leon.toolsfx.plugin.compress

import java.io.ByteArrayInputStream
import java.io.ByteArrayOutputStream
import me.leon.encode.base.base64
import me.leon.encode.base.base64Decode
import me.leon.ext.decodeToByteArray
import me.leon.ext.encodeTo
import me.leon.ext.hex2String
import me.leon.ext.toHex
import org.apache.commons.compress.compressors.brotli.BrotliCompressorInputStream
import org.apache.commons.compress.compressors.bzip2.BZip2CompressorInputStream
import org.apache.commons.compress.compressors.bzip2.BZip2CompressorOutputStream
Expand Down Expand Up @@ -121,22 +123,35 @@ enum class Compression(val alg: String) : ICompress {
}
},
LZString("lzString") {
override fun compress(bytes: ByteArray) =
LzString.compressToBase64(bytes.decodeToString()).base64Decode()

override fun decompress(bytes: ByteArray) =
LzString.decompressFromBase64(
bytes.base64().let {
// 补充额外 'A===' padding
if (it.endsWith("=")) {
it
} else {
it + "A==="
}
}
)
?.toByteArray()
?: byteArrayOf()
override fun compress(bytes: ByteArray) = error("Unsupported")

override fun decompress(bytes: ByteArray) = error("Unsupported")

override fun compress(raw: String, inputEncode: String, outputEncode: String) =
when (outputEncode) {
"raw" -> LzString.compress(raw.decodeToByteArray(inputEncode).encodeTo("raw"))
"base64" ->
LzString.compressToBase64(raw.decodeToByteArray(inputEncode).encodeTo("raw"))
"hex" ->
LzString.compress(raw.decodeToByteArray(inputEncode).encodeTo("raw")).toHex()
else -> error("Wrong Encoding!!!")
}

override fun decompress(raw: String, inputEncode: String, outputEncode: String) =
when (inputEncode) {
"raw" -> LzString.decompress(raw).orEmpty().toByteArray().encodeTo(outputEncode)
"base64" ->
LzString.decompressFromBase64(raw)
.orEmpty()
.toByteArray()
.encodeTo(outputEncode)
"hex" ->
LzString.decompress(raw.hex2String())
.orEmpty()
.toByteArray()
.encodeTo(outputEncode)
else -> error("Wrong Encoding!!!")
}
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,4 +5,16 @@ interface ICompress {
fun compress(bytes: ByteArray): ByteArray

fun decompress(bytes: ByteArray): ByteArray

fun compress(
raw: String,
inputEncode: String = "raw",
outputEncode: String = "base64"
): String = ""

fun decompress(
raw: String,
inputEncode: String = "raw",
outputEncode: String = "base64"
): String = ""
}
Original file line number Diff line number Diff line change
Expand Up @@ -118,20 +118,20 @@ class StringCompression {

@Test
fun lzString() {
val compressed =
Compression.LZString.compress(data.toByteArray()).base64().also { println(it) }
val compressed = Compression.LZString.compress(data, "raw", "base64").also { println(it) }
val decompressed =
Compression.LZString.decompress(compressed.base64Decode()).decodeToString().also {
println(it)
}
Compression.LZString.decompress(compressed, "base64", "raw").also { println(it) }
assertEquals(data, decompressed)

val bytes =
"IIEwbghgdgxgpiABASyiOUAuiDmBXZdAZ0QBtU5EALZHK8uzVHRGaRAI0owg9IUQAzAPYAnIA==="
.base64Decode()
val bytes = "IIEwbghgdgxgpiABASyiOUAuiDmBXZdAZ0QBtU5EALZHK8uzVHRGaRAI0owg9IUQAzAPYAnIA==="

assertEquals(
"Advanced indent guides line highlighting can be enabled for",
Compression.LZString.decompress(bytes).decodeToString()
Compression.LZString.decompress(bytes, "base64", "raw")
)
assertEquals(
"nasller",
Compression.LZString.decompress("HYQwzgNhCmBOQ===", "base64", "raw")
)

println(LzString.compressToBase64("ba"))
Expand Down

0 comments on commit ec12549

Please sign in to comment.