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

  • SEARCH
  • Home
  • 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 779755
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 14, 20262026-05-14T19:58:32+00:00 2026-05-14T19:58:32+00:00

I currently got this script, which compresses byte arrays. But I need it rewritten,

  • 0

I currently got this script, which compresses byte arrays.
But I need it rewritten, so it can compress triple byte arrays [,,]

Thanks!

public static byte[] Compress(byte[] buffer)
{
MemoryStream ms = new MemoryStream();
GZipStream zip = new GZipStream(ms, CompressionMode.Compress, true);
zip.Write(buffer, 0, buffer.Length);
zip.Close();
ms.Position = 0;

MemoryStream outStream = new MemoryStream();

byte[] compressed = new byte[ms.Length];
ms.Read(compressed, 0, compressed.Length);

byte[] gzBuffer = new byte[compressed.Length + 4];
Buffer.BlockCopy(compressed, 0, gzBuffer, 4, compressed.Length);
Buffer.BlockCopy(BitConverter.GetBytes(buffer.Length), 0, gzBuffer, 0, 4);
return gzBuffer;
}

public static byte[] Decompress(byte[] gzBuffer)
{
MemoryStream ms = new MemoryStream();
int msgLength = BitConverter.ToInt32(gzBuffer, 0);
ms.Write(gzBuffer, 4, gzBuffer.Length - 4);

byte[] buffer = new byte[msgLength];

ms.Position = 0;
GZipStream zip = new GZipStream(ms, CompressionMode.Decompress);
zip.Read(buffer, 0, buffer.Length);

return buffer;
}
  • 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-14T19:58:32+00:00Added an answer on May 14, 2026 at 7:58 pm

    Update: I rewrote the code, it is running much faster now and the code is cleaner. Just tested it with some random data (see end of this post).

    The Compression method:

    public static byte[] Compress(byte[, ,] uncompressed)
    {
        if (uncompressed == null)
            throw new ArgumentNullException("uncompressed", 
                                            "The given array is null!");
        if (uncompressed.LongLength > (long)int.MaxValue)
            throw new ArgumentException("The given array is to large!");
    
        using (MemoryStream ms = new MemoryStream())
        using (GZipStream gzs = new GZipStream(ms, CompressionMode.Compress))
        {
            // Save sizes of the dimensions
            for (int dim = 0; dim < 3; dim++)
                gzs.Write(BitConverter.GetBytes(
                          uncompressed.GetLength(dim)), 0, sizeof(int));
    
            // Convert byte[,,] to byte[] by just blockcopying it
            // I know, some pointer-magic/unmanaged cast wouldnt 
            // have to copy it, but its cleaner this way...
            byte[] data = new byte[uncompressed.Length];
            Buffer.BlockCopy(uncompressed, 0, data, 0, uncompressed.Length);
    
            // Write the data to the stream to compress it
            gzs.Write(data, 0, data.Length);
            gzs.Close();
    
            // Get the compressed byte array back
            return ms.ToArray();
        }
    }
    

    The Decompression method:

    public static byte[, ,] Decompress(byte[] compressed)
    {
        if (compressed == null)
            throw new ArgumentNullException("compressed", 
                                            "Data to decompress cant be null!");
    
        using (MemoryStream ms = new MemoryStream(compressed))
        using (GZipStream gzs = new GZipStream(ms, CompressionMode.Decompress))
        {
            // Read the header and restore sizes of dimensions
            byte[] dimheader = new byte[sizeof(int) * 3];
            gzs.Read(dimheader, 0, dimheader.Length);
            int[] dims = new int[3];
            for (int j = 0; j < 3; j++)
                dims[j] = BitConverter.ToInt32(dimheader, sizeof(int) * j);
    
            // Read the data into a buffer
            byte[] data = new byte[dims[0] * dims[1] * dims[2]];
            gzs.Read(data, 0, data.Length);
    
            // Copy the buffer to the three-dimensional array
            byte[, ,] uncompressed = new byte[dims[0], dims[1], dims[2]];
            Buffer.BlockCopy(data, 0, uncompressed, 0, data.Length);
    
            return uncompressed;
        }
    }
    

    The test code:

    Random rnd = new Random();
    
    // Create a new randomly big array, fill it with random data
    byte[, ,] uncomp = new byte[rnd.Next(70, 100), 
                           rnd.Next(70, 100), rnd.Next(70, 100)];
    for (int x = 0; x < uncomp.GetLength(0); x++)
        for (int y = 0; y < uncomp.GetLength(1); y++)
            for (int z = 0; z < uncomp.GetLength(2); z++)
                uncomp[x, y, z] = (byte)rnd.Next(30, 35);
    
    // Compress and Uncompress again
    Stopwatch compTime = new Stopwatch(), uncompTime = new Stopwatch();
    compTime.Start();
    byte[] comp = Compress(uncomp);
    compTime.Stop();
    uncompTime.Start();
    byte[, ,] uncompagain = Decompress(comp);
    uncompTime.Stop();
    
    // Assert all dimension lengths and contents are equal
    for (int j = 0; j < 3; j++)
        Debug.Assert(uncomp.GetLength(j) == uncompagain.GetLength(j));
    
    for (int x = 0; x < uncomp.GetLength(0); x++)
        for (int y = 0; y < uncomp.GetLength(1); y++)
            for (int z = 0; z < uncomp.GetLength(2); z++)
                Debug.Assert(uncomp[x, y, z] == uncompagain[x, y, z]);
    
    Console.WriteLine(string.Format("Compression: {0}ms, " +
        "Decompression: {1}ms, Ratio: {2}% ({3}/{4} bytes)",
        compTime.ElapsedMilliseconds, uncompTime.ElapsedMilliseconds,
        (int)((double)comp.LongLength / (double)uncomp.LongLength * 100),
        comp.LongLength, uncomp.LongLength));
    

    Output, for example:

    Compression: 77ms, Decompression: 23ms, Ratio: 41% (191882/461538 bytes)
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Ask A Question

Stats

  • Questions 391k
  • Answers 391k
  • Best Answers 0
  • User 1
  • Popular
  • Answers
  • Editorial Team

    How to approach applying for a job at a company ...

    • 7 Answers
  • Editorial Team

    How to handle personal stress caused by utterly incompetent and ...

    • 5 Answers
  • Editorial Team

    What is a programmer’s life like?

    • 5 Answers
  • Editorial Team
    Editorial Team added an answer You can implement the following two methods in your UIViewController.… May 15, 2026 at 1:25 am
  • Editorial Team
    Editorial Team added an answer Ok, try this: for (k = 0; k < homeImages.length;… May 15, 2026 at 1:25 am
  • Editorial Team
    Editorial Team added an answer Yes. UINavigationItem (not bar), has a titleView property. In your… May 15, 2026 at 1:25 am

Trending Tags

analytics british company computer developers django employee employer english facebook french google interview javascript language life php programmer programs salary

Top Members

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.