using System.IO.Compression; using System.Text; using System.Xml.Serialization; namespace WordpressEboobScraper2.Scraper; public class GZippedString : IXmlSerializable { public string Value { get; set; } public System.Xml.Schema.XmlSchema GetSchema() { return null; } public void ReadXml(System.Xml.XmlReader reader) { Value = DecompressString(reader.ReadString()); reader.ReadEndElement(); } public void WriteXml(System.Xml.XmlWriter writer) { writer.WriteString(CompressString(Value)); } private string CompressString(string text) { byte[] buffer = Encoding.UTF8.GetBytes(text); var memoryStream = new MemoryStream(); using (var gZipStream = new GZipStream(memoryStream, CompressionMode.Compress, true)) gZipStream.Write(buffer, 0, buffer.Length); memoryStream.Position = 0; var compressedData = new byte[memoryStream.Length]; memoryStream.Read(compressedData, 0, compressedData.Length); var gZipBuffer = new byte[compressedData.Length + 4]; Buffer.BlockCopy(compressedData, 0, gZipBuffer, 4, compressedData.Length); // bytes 4.. are the gzip content Buffer.BlockCopy(BitConverter.GetBytes(buffer.Length), 0, gZipBuffer, 0, 4); // bytes 0..3 are the length of the uncompressed string return Convert.ToBase64String(gZipBuffer); } private string DecompressString(string compressedText) { byte[] gZipBuffer = Convert.FromBase64String(compressedText); using var memoryStream = new MemoryStream(); int dataLength = BitConverter.ToInt32(gZipBuffer[..4], 0); memoryStream.Write(gZipBuffer, 4, gZipBuffer.Length - 4); memoryStream.Position = 0; byte[] buffer = null; using (var gZipStream = new GZipStream(memoryStream, CompressionMode.Decompress)) { buffer = gZipStream.ReadAllToByteArray(); if (dataLength != buffer.Length) throw new Exception("not enough data in gzip"); } return Encoding.UTF8.GetString(buffer); } public static implicit operator GZippedString(string v) => new GZippedString{Value = v}; public static implicit operator string (GZippedString v) => v.Value; }