1

I'm using the library ICSharpCode.SharpZipLib to compress text. After the compress finish I convert to base64 string and store in database. When I need to know what's stored, I get the value from database and decompress.

But, only in one client computer, when I compress a text and try to decompress the following error is throw:

BZip2 input stream crc error

I don't know whats the cause of error, because in my computer all compress and decompress are working fine. I need your help to know what's the cause of the error and how to fix it. Sorry my bad english.

This is my code of compress and decompress:

        public static byte[] Compress(byte[] bytesToCompress)
        {
            MemoryStream ms = new MemoryStream();
            Stream s = new BZip2OutputStream(ms);
            s.Write(bytesToCompress, 0, bytesToCompress.Length);
            s.Close();
            return ms.ToArray();
        }

        public static string Compress(string stringToCompress, Encoding encoding)
        {
            if (String.IsNullOrEmpty(stringToCompress))
                return String.Empty;

            byte[] compressedData = Compress(encoding.GetBytes(stringToCompress));
            string strOut = Convert.ToBase64String(compressedData);

            return strOut;
        }

    public static string DeCompress(string stringToDecompress)
    {
        Encoding encoding = Encoding.Unicode;
        string outString;

        try
        {
            byte[] inArr = Convert.FromBase64String(stringToDecompress.Trim());
            outString = encoding.GetString(DeCompress(inArr));
        }
        catch (NullReferenceException nEx)
        {
            return nEx.Message;
        }

        return outString;
    }

    public static byte[] DeCompress(byte[] bytesToDecompress)
    {
        byte[] writeData = new byte[4096];
        Stream s2 = new BZip2InputStream(new MemoryStream(bytesToDecompress));
        MemoryStream outStream = new MemoryStream();
        while (true)
        {
            int size = s2.Read(writeData, 0, writeData.Length);
            if (size > 0)
            {
                outStream.Write(writeData, 0, size);
            }
            else
            {
                break;
            }
        }
        s2.Close();
        byte[] outArr = outStream.ToArray();
        outStream.Close();
        return outArr;
    }
McNeight
  • 21
  • 3
Only a Curious Mind
  • 2,807
  • 23
  • 39

0 Answers0