Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6707293
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 26, 20262026-05-26T07:37:20+00:00 2026-05-26T07:37:20+00:00

I had a compress and a decompress method that used to add an extra

  • 0

I had a compress and a decompress method that used to add an extra empty character. I managed to fix it, but I’m not sure why the fixed worked and hope somebody could explain it to me.

The Fix (-1 from buffer.length in the following line):

System.Buffer.BlockCopy(BitConverter.GetBytes(bytes.Length - 1), 0, gzBuffer, 0, 4)

Original line:

System.Buffer.BlockCopy(BitConverter.GetBytes(bytes.Length), 0, gzBuffer, 0, 4)

The Functions:

Private Function Compress(ByVal bytes As Byte()) As Byte()
    Using ms As New MemoryStream()

        Using zip As New Ionic.Zlib.GZipStream(ms, Ionic.Zlib.CompressionMode.Compress, Ionic.Zlib.CompressionLevel.BestCompression, True)
            zip.Write(bytes, 0, bytes.Length)
        End Using

        //ms.Position = 0
        Dim compressed As Byte() = ms.ToArray()

        Dim gzBuffer(compressed.Length + 4) As Byte
        System.Buffer.BlockCopy(compressed, 0, gzBuffer, 4, compressed.Length)
        System.Buffer.BlockCopy(BitConverter.GetBytes(bytes.Length -1), 0, gzBuffer, 0, 4)

        Return gzBuffer
    End Using
End Function

Private Function DeCompress(ByVal bytes As Byte()) As Byte()

    Using ms As New MemoryStream()
        Dim msgLength As Integer = BitConverter.ToInt32(bytes, 0)
        ms.Write(bytes, 4, bytes.Length - 4)

        Dim buffer(msgLength) As Byte

        ms.Position = 0
        Dim offset As Integer = 0
        Using zip As New Ionic.Zlib.GZipStream(ms, Ionic.Zlib.CompressionMode.Decompress)
            While offset < buffer.Length - 1
                offset += zip.Read(buffer, offset, buffer.Length - offset)
            End While
        End Using

        Return buffer
    End Using
End Function
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-26T07:37:20+00:00Added an answer on May 26, 2026 at 7:37 am

    That’s better, now the length doesn’t overwrite part of the compressed data.

    Now your problem is that you are not using the Stream.Read method correctly. The method returns the number of bytes read, which can be less than the number of bytes requested, so you have to get that return value and repeat the read until you have all the data:

    Dim offset as Integer = 0
    Using zip As New Ionic.Zlib.GZipStream(ms, Ionic.Zlib.CompressionMode.Decompress)
      Do While offset < buffer.Length
        offset += zip.Read(buffer, offset, buffer.Length - offset)
      Loop
    End Using
    

    Also, instead of writing a byte array to the memory stream, just create the memory stream from the array:

    Using ms As New MemoryStream(bytes)
    

    Instead of reading the memory stream into an array, just use the ToArray method:

    Dim compressed As Byte() = ms.ToArray()
    

    Edit:

    The one-off problem in the code was due to how arrays are created in VB, using the highest index instead of the size, so the buffer should be created using:

    Dim buffer(msgLength - 1) As Byte
    

    Using the Position property when reading and writing the memory stream you can avoid creating extra buffers:

    Private Function Compress(ByVal bytes As Byte()) As Byte()
      Using ms As New MemoryStream()
        ms.Position = 4
        Using zip As New GZipStream(ms, CompressionMode.Compress, True)
          zip.Write(bytes, 0, bytes.Length)
        End Using
        ms.Position = 0
        ms.Write(BitConverter.GetBytes(bytes.Length), 0, 4)
        Return ms.ToArray()
      End Using
    End Function
    
    Private Function DeCompress(ByVal bytes As Byte()) As Byte()
      Dim msgLength As Integer = BitConverter.ToInt32(bytes, 0)
      Using ms As New MemoryStream(bytes)
        Dim buffer(msgLength - 1) As Byte
        ms.Position = 4
        Dim offset As Integer = 0
        Using zip As New GZipStream(ms, CompressionMode.Decompress)
          While offset < buffer.Length
            offset += zip.Read(buffer, offset, buffer.Length - offset)
          End While
        End Using
        Return buffer
      End Using
    End Function
    

    (Note: This code uses the standard .NET GZipStream.)

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Had a page that was working fine. Only change I made was to add
Had a good search here but can't see anything that gets my mind in
I had a discussion with some colleagues mentioning that there are not too many
I had used Server Explorer and related tools for graphical database development with Microsoft
I had an idea, if I add a python .py file to my C#
I had to delete all the rows from a log table that contained about
I had the idea of a search engine that would index web items like
My application has a collection of around 1940 icons that are used throughout. They're
So I have been using Minify to compress my JS and CSS which had
Had an interesting experience with Python's file buffering and wanted to know that I

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.