Note that there are some explanatory texts on larger screens.

plurals
  1. POcompression string in c#
    primarykey
    data
    text
    <p>I am using the following code to compress 2 strings, it works well in compression. However, when I try to decompress the second strings and if the first one is short (about 4 characters) I got Block length does not match with its complement exception. Here is the class I used for compression:</p> <pre><code> using System; using System.IO; using System.IO.Compression; using System.Text; namespace CompressString { internal static class StringCompressor { /// &lt;summary&gt; /// Compresses the string. /// &lt;/summary&gt; /// &lt;param name="text"&gt;The text.&lt;/param&gt; /// &lt;returns&gt;&lt;/returns&gt; public static string CompressString(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(); } /// &lt;summary&gt; /// Decompresses the string. /// &lt;/summary&gt; /// &lt;param name="compressedText"&gt;The compressed text.&lt;/param&gt; /// &lt;returns&gt;&lt;/returns&gt; public static string DecompressString(string sData) { byte[] byteArray = new byte[sData.Length]; int indexBa = 0; foreach (char item in sData) byteArray[indexBa++] = (byte)item; MemoryStream memoryStream = new MemoryStream(byteArray); GZipStream gZipStream = new GZipStream(memoryStream, CompressionMode.Decompress); byteArray = new byte[1024]; StringBuilder stringBuilder = new StringBuilder(); int readBytes; while ((readBytes = gZipStream.Read(byteArray, 0, byteArray.Length)) != 0) { for (int i = 0; i &lt; readBytes; i++) stringBuilder.Append((char)byteArray[i]); } gZipStream.Close(); memoryStream.Close(); gZipStream.Dispose(); memoryStream.Dispose(); return stringBuilder.ToString(); } } </code></pre> <p>}</p> <p>I got the exception in DecompressString method in the line:</p> <pre><code>while ((readBytes = gZipStream.Read(byteArray, 0, byteArray.Length)) != 0) </code></pre>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload