using System; using K4os.Compression.LZ4; #if UNITY_5_3_OR_NEWER using UnityEngine; #endif namespace CommonTools { public static class LZ4Utility { public static (string, int) CompressString(string input) { byte[] inputBytes = System.Text.Encoding.UTF8.GetBytes(input); var encodedBytes = new byte[LZ4Codec.MaximumOutputSize(inputBytes.Length)]; var encodedLength = LZ4Codec.Encode( inputBytes, 0, inputBytes.Length, encodedBytes, 0, encodedBytes.Length); var resultArray = new byte[encodedLength]; Array.Copy(encodedBytes, 0, resultArray, 0, encodedLength); return (Convert.ToBase64String(resultArray), inputBytes.Length); } public static string DecompressString(string compressedInput, int encodedLength) { var source = Convert.FromBase64String(compressedInput); var decompressedBytes = new byte[encodedLength]; var decoded = LZ4Codec.Decode( source, 0, source.Length, decompressedBytes, 0, decompressedBytes.Length); #if UNITY_5_3_OR_NEWER if (decoded < 0) Debug.LogWarning("LZ4 Decompress function did not decompress."); #endif return System.Text.Encoding.UTF8.GetString(decompressedBytes); } } }