Home > OS >  WCF Compress And Decompress Mehtod Problem Gzip Image Data(Binary) C#
WCF Compress And Decompress Mehtod Problem Gzip Image Data(Binary) C#

Time:08-24

i have two method for compress and decompress base64 image data in sql server

my decompress method for read data in sql its work but compressing return wrong data please help me ...

public static Byte[] BinaryCompress(Byte[] InputStream)
    {
        using (MemoryStream ms = new MemoryStream())
        {
        using (GZipStream x = new GZipStream(ms, CompressionMode.Compress, true))

            {
                byte[] inputBytes = (byte[])InputStream;
                x.Write(inputBytes, 0, inputBytes.Length);
            }

            return ms.ToArray();
        }

    }

CodePudding user response:

 public static Byte[] BinaryDecompress(Byte[] InputBinary)

        {

            byte[] inputBytes = (byte[])InputBinary;

            using (MemoryStream msin = new MemoryStream(inputBytes))

            {

                using (GZipStream x = new GZipStream(msin, CompressionMode.Decompress))

                {

                    using (MemoryStream msout = new MemoryStream())

                    {

                        int num = x.ReadByte();

                        while (num != -1)

                        {

                            msout.WriteByte((byte)num);

                            num = x.ReadByte();

                        }
                        return  msout.ToArray(); ;

                    }

                }

            }

        }

CodePudding user response:

Could you please provide more specific exception by using below code?

  public static Byte[] BinaryCompress(Byte[] InputStream)
        {
            try
            {
                MemoryStream ms = new MemoryStream();
                try
                {
                    GZipStream x = new GZipStream(ms, CompressionMode.Compress, true)
                    byte[] inputBytes = (byte[])InputStream;
                    x.Write(inputBytes, 0, inputBytes.Length);
                }
                catch (Exception ex)
                {
                    Console.Write(ex.StackTrace);
                }
                return ms.ToArray();
            }
            catch (Exception e)
            {
                Console.WriteLine(e.StackTrace);
                throw;
            }

        }
  • Related