Web Services WCF压缩和解压缩方法问题Gzip图像数据(二进制)C#

d7v8vwbk  于 2022-11-15  发布在  C#
关注(0)|答案(2)|浏览(187)

我有两种方法在sql server中压缩和解压缩base64图像数据
我的解压缩方法读取数据在sql它的工作,但压缩返回错误的数据,请帮助我...

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();
        }

    }
oknrviil

oknrviil1#

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(); ;

                    }

                }

            }

        }
wnvonmuf

wnvonmuf2#

您能否使用以下代码提供更具体的异常?

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;
            }

        }

相关问题