DZone
Thanks for visiting DZone today,
Edit Profile
  • Manage Email Subscriptions
  • How to Post to DZone
  • Article Submission Guidelines
Sign Out View Profile
  • Post an Article
  • Manage My Drafts
Over 2 million developers have joined DZone.
Log In / Join
Refcards Trend Reports
Events Video Library
Refcards
Trend Reports

Events

View Events Video Library

Related

  • Intent-Driven AI Frontends: AI Assistance to Enterprise Angular Architecture
  • Essential Techniques for Production Vector Search Systems Part 2 - Binary Quantization
  • Essential Techniques for Production Vector Search Systems Part 1 - Hybrid Search
  • Hybrid Search Using Postgres DB

Trending

  • How to Save Money Using Custom LLMs for Specific Tasks
  • Stop Debugging Glue Jobs Manually: Building an Agentic Observability Layer for Data Pipelines
  • From 24 Hours to 2 Hours: How We Fixed a Broken BI System With Apache Airflow
  • Pragmatica Aether: Let Java Be Java
  1. DZone
  2. Data Engineering
  3. Data
  4. Compress/decompress Byte Array

Compress/decompress Byte Array

By 
Snippets Manager user avatar
Snippets Manager
·
Jun. 10, 07 · Code Snippet
Likes (0)
Comment
Save
Tweet
Share
8.7K Views

Join the DZone community and get the full member experience.

Join For Free

 using System;
 using System.Collections.Generic;
 using System.IO.Compression;
 using System.IO;
 using System.Collections;

namespace Utilities
{
 class Compression
 {
        public static byte[] Compress(byte[] data)
        {
            MemoryStream ms = new MemoryStream();
            DeflateStream ds = new DeflateStream(ms, CompressionMode.Compress);
            ds.Write(data, 0, data.Length);
            ds.Flush();
            ds.Close();
            return ms.ToArray();
        }
        public static byte[] Decompress(byte[] data)
        {
            const int BUFFER_SIZE = 256;
            byte[] tempArray = new byte[BUFFER_SIZE];
            List tempList = new List();
            int count = 0, length = 0;

            MemoryStream ms = new MemoryStream(data);
            DeflateStream ds = new DeflateStream(ms, CompressionMode.Decompress);

            while ((count = ds.Read(tempArray, 0, BUFFER_SIZE)) > 0)
            {
                if (count == BUFFER_SIZE)
                {
                    tempList.Add(tempArray);
                    tempArray = new byte[BUFFER_SIZE];
                }
                else
                {
                    byte[] temp = new byte[count];
                    Array.Copy(tempArray, 0, temp, 0, count);
                    tempList.Add(temp);
                }
                length += count;
            }

            byte[] retVal = new byte[length];

            count = 0;
            foreach (byte[] temp in tempList)
            {
                Array.Copy(temp, 0, retVal, count, temp.Length);
                count += temp.Length;
            }

            return retVal;
        }
 }
}
Data structure Data Types

Opinions expressed by DZone contributors are their own.

Related

  • Intent-Driven AI Frontends: AI Assistance to Enterprise Angular Architecture
  • Essential Techniques for Production Vector Search Systems Part 2 - Binary Quantization
  • Essential Techniques for Production Vector Search Systems Part 1 - Hybrid Search
  • Hybrid Search Using Postgres DB

Partner Resources

×

Comments

The likes didn't load as expected. Please refresh the page and try again.

  • RSS
  • X
  • Facebook

ABOUT US

  • About DZone
  • Support and feedback
  • Community research

ADVERTISE

  • Advertise with DZone

CONTRIBUTE ON DZONE

  • Article Submission Guidelines
  • Become a Contributor
  • Core Program
  • Visit the Writers' Zone

LEGAL

  • Terms of Service
  • Privacy Policy

CONTACT US

  • 3343 Perimeter Hill Drive
  • Suite 215
  • Nashville, TN 37211
  • [email protected]

Let's be friends:

  • RSS
  • X
  • Facebook