Compresión / Descompresión cadena con C #

Soy novato en .net. Estoy haciendo cadenas de compresión y descompresión en C #. Hay un XML y estoy convirtiendo en cadena y después estoy haciendo compresión y descompresión. No hay ningún error de comstackción en mi código, excepto cuando descomprimo mi código y devuelvo mi cadena, devolviendo solo la mitad del XML.

A continuación está mi código, por favor corrígeme donde estoy equivocado.

Código:

class Program { public static string Zip(string value) { //Transform string into byte[] byte[] byteArray = new byte[value.Length]; int indexBA = 0; foreach (char item in value.ToCharArray()) { byteArray[indexBA++] = (byte)item; } //Prepare for compress System.IO.MemoryStream ms = new System.IO.MemoryStream(); System.IO.Compression.GZipStream sw = new System.IO.Compression.GZipStream(ms, System.IO.Compression.CompressionMode.Compress); //Compress sw.Write(byteArray, 0, byteArray.Length); //Close, DO NOT FLUSH cause bytes will go missing... sw.Close(); //Transform byte[] zip data to string byteArray = ms.ToArray(); System.Text.StringBuilder sB = new System.Text.StringBuilder(byteArray.Length); foreach (byte item in byteArray) { sB.Append((char)item); } ms.Close(); sw.Dispose(); ms.Dispose(); return sB.ToString(); } public static string UnZip(string value) { //Transform string into byte[] byte[] byteArray = new byte[value.Length]; int indexBA = 0; foreach (char item in value.ToCharArray()) { byteArray[indexBA++] = (byte)item; } //Prepare for decompress System.IO.MemoryStream ms = new System.IO.MemoryStream(byteArray); System.IO.Compression.GZipStream sr = new System.IO.Compression.GZipStream(ms, System.IO.Compression.CompressionMode.Decompress); //Reset variable to collect uncompressed result byteArray = new byte[byteArray.Length]; //Decompress int rByte = sr.Read(byteArray, 0, byteArray.Length); //Transform byte[] unzip data to string System.Text.StringBuilder sB = new System.Text.StringBuilder(rByte); //Read the number of bytes GZipStream red and do not a for each bytes in //resultByteArray; for (int i = 0; i < rByte; i++) { sB.Append((char)byteArray[i]); } sr.Close(); ms.Close(); sr.Dispose(); ms.Dispose(); return sB.ToString(); } static void Main(string[] args) { XDocument doc = XDocument.Load(@"D:\RSP.xml"); string val = doc.ToString(SaveOptions.DisableFormatting); val = Zip(val); val = UnZip(val); } } 

Mi tamaño de XML es de 63 KB.

El código para comprimir / descomprimir una cadena

 public static void CopyTo(Stream src, Stream dest) { byte[] bytes = new byte[4096]; int cnt; while ((cnt = src.Read(bytes, 0, bytes.Length)) != 0) { dest.Write(bytes, 0, cnt); } } public static byte[] Zip(string str) { var bytes = Encoding.UTF8.GetBytes(str); using (var msi = new MemoryStream(bytes)) using (var mso = new MemoryStream()) { using (var gs = new GZipStream(mso, CompressionMode.Compress)) { //msi.CopyTo(gs); CopyTo(msi, gs); } return mso.ToArray(); } } public static string Unzip(byte[] bytes) { using (var msi = new MemoryStream(bytes)) using (var mso = new MemoryStream()) { using (var gs = new GZipStream(msi, CompressionMode.Decompress)) { //gs.CopyTo(mso); CopyTo(gs, mso); } return Encoding.UTF8.GetString(mso.ToArray()); } } static void Main(string[] args) { byte[] r1 = Zip("StringStringStringStringStringStringStringStringStringStringStringStringStringString"); string r2 = Unzip(r1); } 

Recuerde que Zip devuelve un byte[] , mientras que Unzip devuelve una string . Si quieres una cadena de Zip , puedes codificarla Base64 (por ejemplo, usando Convert.ToBase64String(r1) ) (¡el resultado de Zip es MUY binario! No es algo que puedas imprimir en la pantalla o escribir directamente en un XML )

La versión sugerida es para .NET 2.0, para .NET 4.0 usa MemoryStream.CopyTo .

IMPORTANTE: El contenido comprimido no se puede escribir en la secuencia de salida hasta que GZipStream sepa que tiene toda la información (es decir, para comprimirla efectivamente necesita todos los datos). GZipStream de GZipStream Dispose() el GZipStream antes de inspeccionar el flujo de salida (p. Ej., mso.ToArray() ). Esto se hace con el bloque using() { } arriba. Tenga en cuenta que GZipStream es el bloque más interno y se accede a los contenidos fuera de él. Lo mismo GZipStream para descomprimir: Dispose() del GZipStream antes de intentar acceder a los datos.

de acuerdo con este fragmento, uso este código y está funcionando bien:

 using System; using System.IO; using System.IO.Compression; using System.Text; namespace CompressString { internal static class StringCompressor { ///  /// Compresses the string. ///  /// The text. ///  public static 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); Buffer.BlockCopy(BitConverter.GetBytes(buffer.Length), 0, gZipBuffer, 0, 4); return Convert.ToBase64String(gZipBuffer); } ///  /// Decompresses the string. ///  /// The compressed text. ///  public static string DecompressString(string compressedText) { byte[] gZipBuffer = Convert.FromBase64String(compressedText); using (var memoryStream = new MemoryStream()) { int dataLength = BitConverter.ToInt32(gZipBuffer, 0); memoryStream.Write(gZipBuffer, 4, gZipBuffer.Length - 4); var buffer = new byte[dataLength]; memoryStream.Position = 0; using (var gZipStream = new GZipStream(memoryStream, CompressionMode.Decompress)) { gZipStream.Read(buffer, 0, buffer.Length); } return Encoding.UTF8.GetString(buffer); } } } } 

Con el advenimiento de .NET 4.0 (y superior) con los métodos Stream.CopyTo (), pensé que publicaría un enfoque actualizado.

También creo que la siguiente versión es útil como un claro ejemplo de una clase autónoma para comprimir cadenas regulares en cadenas codificadas en Base64, y viceversa:

 public static class StringCompression { ///  /// Compresses a string and returns a deflate compressed, Base64 encoded string. ///  /// String to compress public static string Compress(string uncompressedString) { byte[] compressedBytes; using (var uncompressedStream = new MemoryStream(Encoding.UTF8.GetBytes(uncompressedString))) { var compressedStream = new MemoryStream(); // setting the leaveOpen parameter to true to ensure that compressedStream will not be closed when compressorStream is disposed // this allows compressorStream to close and flush its buffers to compressedStream and guarantees that compressedStream.ToArray() can be called afterward // although MSDN documentation states that ToArray() can be called on a closed MemoryStream, this approach avoids relying on that very odd behavior should it ever change using (var compressorStream = new DeflateStream(compressedStream, CompressionLevel.Fastest, true)) { uncompressedStream.CopyStreamTo(compressorStream); } // call compressedStream.ToArray() after the enclosing DeflateStream has closed and flushed its buffer to compressedStream compressedBytes = compressedStream.ToArray(); } return Convert.ToBase64String(compressedBytes); } ///  /// Decompresses a deflate compressed, Base64 encoded string and returns an uncompressed string. ///  /// String to decompress. public static string Decompress(string compressedString) { byte[] decompressedBytes; var compressedStream = new MemoryStream(Convert.FromBase64String(compressedString)); using (var decompressorStream = new DeflateStream(compressedStream, CompressionMode.Decompress)) { using (var decompressedStream = new MemoryStream()) { decompressorStream.CopyStreamTo(decompressedStream); decompressedBytes = decompressedStream.ToArray(); } } return Encoding.UTF8.GetString(decompressedBytes); } 

Aquí hay otro enfoque que usa la técnica de métodos de extensión para extender la clase String para agregar compresión y descompresión de cadenas. Puede colocar la clase a continuación en un proyecto existente y luego usarlo de la siguiente manera:

 var uncompressedString = "Hello World!"; var compressedString = uncompressedString.Compress(); 

y

 var decompressedString = compressedString.Decompress(); 

Esto es:

 public static class Extensions { ///  /// Compresses a string and returns a deflate compressed, Base64 encoded string. ///  /// String to compress public static string Compress(this string uncompressedString) { byte[] compressedBytes; using (var uncompressedStream = new MemoryStream(Encoding.UTF8.GetBytes(uncompressedString))) { var compressedStream = new MemoryStream(); // setting the leaveOpen parameter to true to ensure that compressedStream will not be closed when compressorStream is disposed // this allows compressorStream to close and flush its buffers to compressedStream and guarantees that compressedStream.ToArray() can be called afterward // although MSDN documentation states that ToArray() can be called on a closed MemoryStream, this approach avoids relying on that very odd behavior should it ever change using (var compressorStream = new DeflateStream(compressedStream, CompressionLevel.Fastest, true)) { uncompressedStream.CopyStreamTo(compressorStream); } // call compressedStream.ToArray() after the enclosing DeflateStream has closed and flushed its buffer to compressedStream compressedBytes = compressedStream.ToArray(); } return Convert.ToBase64String(compressedBytes); } ///  /// Decompresses a deflate compressed, Base64 encoded string and returns an uncompressed string. ///  /// String to decompress. public static string Decompress(this string compressedString) { byte[] decompressedBytes; var compressedStream = new MemoryStream(Convert.FromBase64String(compressedString)); using (var decompressorStream = new DeflateStream(compressedStream, CompressionMode.Decompress)) { using (var decompressedStream = new MemoryStream()) { decompressorStream.CopyStreamTo(decompressedStream); decompressedBytes = decompressedStream.ToArray(); } } return Encoding.UTF8.GetString(decompressedBytes); } 

Para aquellos que todavía obtienen el número mágico en el encabezado GZip no es correcto. Asegúrate de pasar en una transmisión GZip. ERROR y si su cadena fue comprimida usando php , necesitará hacer algo como:

  public static string decodeDecompress(string originalReceivedSrc) { byte[] bytes = Convert.FromBase64String(originalReceivedSrc); using (var mem = new MemoryStream()) { //the trick is here mem.Write(new byte[] { 0x1f, 0x8b, 0x08, 0x00, 0x00, 0x00, 0x00, 0x00 }, 0, 8); mem.Write(bytes, 0, bytes.Length); mem.Position = 0; using (var gzip = new GZipStream(mem, CompressionMode.Decompress)) using (var reader = new StreamReader(gzip)) { return reader.ReadToEnd(); } } } 

Esta es una versión actualizada para .NET 4.5 y versiones posteriores que usan async / await e IEnumerables:

 public static class CompressionExtensions { public static async Task> Zip(this object obj) { byte[] bytes = obj.Serialize(); using (MemoryStream msi = new MemoryStream(bytes)) using (MemoryStream mso = new MemoryStream()) { using (var gs = new GZipStream(mso, CompressionMode.Compress)) await msi.CopyToAsync(gs); return mso.ToArray().AsEnumerable(); } } public static async Task Unzip(this byte[] bytes) { using (MemoryStream msi = new MemoryStream(bytes)) using (MemoryStream mso = new MemoryStream()) { using (var gs = new GZipStream(msi, CompressionMode.Decompress)) { //gs.CopyTo(mso); await gs.CopyToAsync(mso); } return mso.ToArray().Deserialize(); } } } public static class SerializerExtensions { ///  /// Writes the given object instance to a binary file. /// Object type (and all child types) must be decorated with the [Serializable] attribute. /// To prevent a variable from being serialized, decorate it with the [NonSerialized] attribute; cannot be applied to properties. ///  /// The type of object being written to the XML file. /// The file path to write the object instance to. /// The object instance to write to the XML file. /// If false the file will be overwritten if it already exists. If true the contents will be appended to the file. public static byte[] Serialize(this T objectToWrite) { using (MemoryStream stream = new MemoryStream()) { BinaryFormatter binaryFormatter = new BinaryFormatter(); binaryFormatter.Serialize(stream, objectToWrite); return stream.GetBuffer(); } } ///  /// Reads an object instance from a binary file. ///  /// The type of object to read from the XML. /// The file path to read the object instance from. /// Returns a new instance of the object read from the binary file. public static async Task _Deserialize(this byte[] arr) { using (MemoryStream stream = new MemoryStream()) { BinaryFormatter binaryFormatter = new BinaryFormatter(); await stream.WriteAsync(arr, 0, arr.Length); stream.Position = 0; return (T)binaryFormatter.Deserialize(stream); } } public static async Task Deserialize(this byte[] arr) { object obj = await arr._Deserialize(); return obj; } } 

Con esto puedes serializar todo lo que admite BinaryFormatter, en lugar de solo cadenas.