none
GZipStream - What am I missing? RRS feed

  • Question

  •   

    Why this code compress small strings and not large strings?

    Private Function Decompress(ByRef message As String) As String

            Dim bytes() As Byte = Convert.FromBase64String(message.ToArray)

            Dim stream As System.IO.MemoryStream = New System.IO.MemoryStream(bytes)
            Dim memStream As GZipStream = New GZipStream(stream, System.IO.Compression.CompressionMode.Decompress)

            Dim str As String = ""

            Dim respBuffer As Byte() = New Byte(1024) {}

            While True

                Dim bytesRead As Integer = memStream.Read(respBuffer, 0, respBuffer.Length)
                str += Encoding.ASCII.GetString(respBuffer.ToArray())

                If bytesRead = 0 Then
                    Exit While
                End If

            End While

            memStream.Close()
            stream.Close()


            Return str

        End Function

        Private Function Compress(ByRef message As String) As String

            Dim stream As System.IO.MemoryStream = New System.IO.MemoryStream()
            Dim memStream As GZipStream = New GZipStream(stream, System.IO.Compression.CompressionMode.Compress, False)

            ' Compress
            Dim byteArray() As Byte = Encoding.ASCII.GetBytes(message)
            memStream.Write(byteArray, 0, byteArray.Length)

            memStream.Close()
            stream.Close()

            Return Convert.ToBase64String(stream.ToArray)

        End Function

    Friday, July 20, 2012 6:16 PM

Answers

  • I made it worked by myself here's the code that works.  Above code compress the text right but decompress code is a mess.

            private string Compress(string text)
            {

                System.IO.MemoryStream output = new System.IO.MemoryStream();
                GZipStream gzip = new GZipStream(output, System.IO.Compression.CompressionMode.Compress, false);

                // Compress
                byte[] byteArray = Encoding.ASCII.GetBytes(text);

                gzip.Write(byteArray, 0, byteArray.Length);

                gzip.Close();
                output.Close();

                return Convert.ToBase64String(output.ToArray());

            }

    /// <summary>
            ///
            /// </summary>
            /// <param name="zipped"></param>
            /// <returns></returns>
            /// <remarks></remarks>
            private string Decompress(string zipped)
            {


                byte[] bytes = Convert.FromBase64String(zipped);

                //create a MemoryStream for holding the incoming data
                System.IO.MemoryStream input = new System.IO.MemoryStream(bytes);
                GZipStream gzip = new GZipStream(input, System.IO.Compression.CompressionMode.Decompress);

                string text = "";

                byte[] buff = new byte[4097];


                while (true)
                {
                    int bytesRead = gzip.Read(buff, 0, buff.Length);

                    if (bytesRead == 0)
                    {
                        break;
                    }

                    text += Encoding.ASCII.GetString(buff, 0, bytesRead);
                }

                input.Close();
                gzip.Close();

                return text;

            }


    Thursday, July 26, 2012 6:05 PM

All replies

  • What does it do to large strings?  What errors do you get and on what line is the error?
    Friday, July 20, 2012 8:44 PM
  • Hi Dip,

    Welcome to the MSDN Forum.

    As JohnWein said, would you like to post the steps to reproduce this scenario?

    Best regards,


    Mike Feng
    MSDN Community Support | Feedback to us
    Please remember to mark the replies as answers if they help and unmark them if they provide no help.

    Monday, July 23, 2012 9:43 AM
    Moderator
  • I made it worked by myself here's the code that works.  Above code compress the text right but decompress code is a mess.

            private string Compress(string text)
            {

                System.IO.MemoryStream output = new System.IO.MemoryStream();
                GZipStream gzip = new GZipStream(output, System.IO.Compression.CompressionMode.Compress, false);

                // Compress
                byte[] byteArray = Encoding.ASCII.GetBytes(text);

                gzip.Write(byteArray, 0, byteArray.Length);

                gzip.Close();
                output.Close();

                return Convert.ToBase64String(output.ToArray());

            }

    /// <summary>
            ///
            /// </summary>
            /// <param name="zipped"></param>
            /// <returns></returns>
            /// <remarks></remarks>
            private string Decompress(string zipped)
            {


                byte[] bytes = Convert.FromBase64String(zipped);

                //create a MemoryStream for holding the incoming data
                System.IO.MemoryStream input = new System.IO.MemoryStream(bytes);
                GZipStream gzip = new GZipStream(input, System.IO.Compression.CompressionMode.Decompress);

                string text = "";

                byte[] buff = new byte[4097];


                while (true)
                {
                    int bytesRead = gzip.Read(buff, 0, buff.Length);

                    if (bytesRead == 0)
                    {
                        break;
                    }

                    text += Encoding.ASCII.GetString(buff, 0, bytesRead);
                }

                input.Close();
                gzip.Close();

                return text;

            }


    Thursday, July 26, 2012 6:05 PM
  • What does it do to large strings?  What errors do you get and on what line is the error?

    The function is compress and decompress. Cannot decompress compress string. I was able to find the fix and added to this tread.
    Thursday, July 26, 2012 6:07 PM