diff options
author | Jesse Plamondon-Willard <Pathoschild@users.noreply.github.com> | 2019-08-04 16:37:10 -0400 |
---|---|---|
committer | Jesse Plamondon-Willard <Pathoschild@users.noreply.github.com> | 2019-09-14 19:00:36 -0400 |
commit | c86db64880d52630c372aa24f7aadf0036fb3fcf (patch) | |
tree | a102db0cf57f1ce12ef24ddf8f8d7ee8590bc94a /src/SMAPI.Web/Framework/Compression | |
parent | 8e0ddd1d00e3d0d11fbaa0ffce804b3e7186c254 (diff) | |
download | SMAPI-c86db64880d52630c372aa24f7aadf0036fb3fcf.tar.gz SMAPI-c86db64880d52630c372aa24f7aadf0036fb3fcf.tar.bz2 SMAPI-c86db64880d52630c372aa24f7aadf0036fb3fcf.zip |
encapsulate gzip logic for reuse (#654)
Diffstat (limited to 'src/SMAPI.Web/Framework/Compression')
-rw-r--r-- | src/SMAPI.Web/Framework/Compression/GzipHelper.cs | 89 | ||||
-rw-r--r-- | src/SMAPI.Web/Framework/Compression/IGzipHelper.cs | 17 |
2 files changed, 106 insertions, 0 deletions
diff --git a/src/SMAPI.Web/Framework/Compression/GzipHelper.cs b/src/SMAPI.Web/Framework/Compression/GzipHelper.cs new file mode 100644 index 00000000..cc8f4737 --- /dev/null +++ b/src/SMAPI.Web/Framework/Compression/GzipHelper.cs @@ -0,0 +1,89 @@ +using System; +using System.IO; +using System.IO.Compression; +using System.Text; + +namespace StardewModdingAPI.Web.Framework.Compression +{ + /// <summary>Handles GZip compression logic.</summary> + internal class GzipHelper : IGzipHelper + { + /********* + ** Fields + *********/ + /// <summary>The first bytes in a valid zip file.</summary> + /// <remarks>See <a href="https://en.wikipedia.org/wiki/Zip_(file_format)#File_headers"/>.</remarks> + private const uint GzipLeadBytes = 0x8b1f; + + + /********* + ** Public methods + *********/ + /// <summary>Compress a string.</summary> + /// <param name="text">The text to compress.</param> + /// <remarks>Derived from <a href="https://stackoverflow.com/a/17993002/262123"/>.</remarks> + public string CompressString(string text) + { + // get raw bytes + byte[] buffer = Encoding.UTF8.GetBytes(text); + + // compressed + byte[] compressedData; + using (MemoryStream stream = new MemoryStream()) + { + using (GZipStream zipStream = new GZipStream(stream, CompressionLevel.Optimal, leaveOpen: true)) + zipStream.Write(buffer, 0, buffer.Length); + + stream.Position = 0; + compressedData = new byte[stream.Length]; + stream.Read(compressedData, 0, compressedData.Length); + } + + // prefix length + byte[] zipBuffer = new byte[compressedData.Length + 4]; + Buffer.BlockCopy(compressedData, 0, zipBuffer, 4, compressedData.Length); + Buffer.BlockCopy(BitConverter.GetBytes(buffer.Length), 0, zipBuffer, 0, 4); + + // return string representation + return Convert.ToBase64String(zipBuffer); + } + + /// <summary>Decompress a string.</summary> + /// <param name="rawText">The compressed text.</param> + /// <remarks>Derived from <a href="https://stackoverflow.com/a/17993002/262123"/>.</remarks> + public string DecompressString(string rawText) + { + // get raw bytes + byte[] zipBuffer; + try + { + zipBuffer = Convert.FromBase64String(rawText); + } + catch + { + return rawText; // not valid base64, wasn't compressed by the log parser + } + + // skip if not gzip + if (BitConverter.ToUInt16(zipBuffer, 4) != GzipHelper.GzipLeadBytes) + return rawText; + + // decompress + using (MemoryStream memoryStream = new MemoryStream()) + { + // read length prefix + int dataLength = BitConverter.ToInt32(zipBuffer, 0); + memoryStream.Write(zipBuffer, 4, zipBuffer.Length - 4); + + // read data + byte[] buffer = new byte[dataLength]; + memoryStream.Position = 0; + using (GZipStream gZipStream = new GZipStream(memoryStream, CompressionMode.Decompress)) + gZipStream.Read(buffer, 0, buffer.Length); + + // return original string + return Encoding.UTF8.GetString(buffer); + } + } + } +} diff --git a/src/SMAPI.Web/Framework/Compression/IGzipHelper.cs b/src/SMAPI.Web/Framework/Compression/IGzipHelper.cs new file mode 100644 index 00000000..a000865e --- /dev/null +++ b/src/SMAPI.Web/Framework/Compression/IGzipHelper.cs @@ -0,0 +1,17 @@ +namespace StardewModdingAPI.Web.Framework.Compression +{ + /// <summary>Handles GZip compression logic.</summary> + internal interface IGzipHelper + { + /********* + ** Methods + *********/ + /// <summary>Compress a string.</summary> + /// <param name="text">The text to compress.</param> + string CompressString(string text); + + /// <summary>Decompress a string.</summary> + /// <param name="rawText">The compressed text.</param> + string DecompressString(string rawText); + } +} |