Forum C# Decompressing byte arrays in C#

Decompressing byte arrays in C#

Postby GameTwoThree » September 25th, 2011, 3:30 pm

I was wondering, is there a way to read and decompress a bytearray in C# which was compressed in flash(as3) using the deflate(); function?

So, I'm sending a compressed arrray from client to server, and want to decompress it there and read it's contents.

If anyone knows how to do it, can you please provide me with an code example if it's not a problem.

Thanks a lot,
Karlo
GameTwoThree
 
Posts: 11
Joined: February 28th, 2011, 2:11 am

Re: Decompressing byte arrays in C#

Postby fox1980 » September 28th, 2011, 2:57 pm

I think it's not possible at the moment. Oliver said in another post (http://playerio.com/forum/post7083) they we're going to whitelist System.Compression namespace, but i don't think it's done yet.
fox1980
 
Posts: 206
Joined: April 1st, 2010, 10:39 pm

Re: Decompressing byte arrays in C#

Postby Oliver » October 12th, 2011, 12:41 pm

System.IO.Compression is whitelisted now, has been for some time :)

Best,
Oliver
User avatar
Oliver
.IO
 
Posts: 1159
Joined: January 12th, 2010, 8:29 am

Re: Decompressing byte arrays in C#

Postby Vania » July 6th, 2012, 7:02 pm

System.IO.Compression is terrible, it can actually increase the size of the data.

Anyone knows another library that has Deflate and is compatible with player.io ?
Vania
 
Posts: 198
Joined: March 24th, 2010, 9:01 pm

Re: Decompressing byte arrays in C#

Postby Henrik » July 6th, 2012, 9:56 pm

Vania wrote:System.IO.Compression is terrible, it can actually increase the size of the data.

Only if you use it wrong. As long as you compress all your data in one go you should be fine. If you make a streamwriter or in any other way just write small bits of data to the compressed stream, you can indeed make it larger. So don't do that. :D
Henrik
.IO
 
Posts: 1880
Joined: January 4th, 2010, 1:53 pm

Re: Decompressing byte arrays in C#

Postby Vania » July 6th, 2012, 11:34 pm

Hmm, what is a streamwriter?

Henrik, could you share your code for compressing bytearrays? I have no idea how to use this class right...
What is the minimum size that one should try to compress?
Vania
 
Posts: 198
Joined: March 24th, 2010, 9:01 pm

Re: Decompressing byte arrays in C#

Postby Henrik » July 7th, 2012, 11:41 am

Code: Select all
public byte[] Compress(byte[] bytes) {
   using (var ms = new MemoryStream()) {
      using (var ds = new DeflateStream(ms, CompressionMode.Compress)) {
         ds.Write(bytes, 0, bytes.Length);
      }
      return ms.ToArray();
   }
}

public byte[] Uncompress(byte[] bytes) {
   using (var ds = new DeflateStream(new MemoryStream(bytes), CompressionMode.Decompress)) {
      using (var ms = new MemoryStream()) {
         ds.CopyTo(ms);
         return ms.ToArray();
      }
   }
}

As for recommended sizes, I have no idea. Try it. It all depends on the data you are compressing.
Henrik
.IO
 
Posts: 1880
Joined: January 4th, 2010, 1:53 pm

Re: Decompressing byte arrays in C#

Postby Vania » July 16th, 2012, 5:08 pm

Thanks Henrik, it worked, turned 1MB into 8k :P
Sometimes the Deflate stream will make things bigger though... what one can do is compress the bytearray, and then check if it's actually smaller than the original(otherwise send the original bytearray), add a boolean flag at the start so the client knows if it's compressed or not.
Vania
 
Posts: 198
Joined: March 24th, 2010, 9:01 pm


Return to C#