0

I am reading an unzipped binary file from disk like this:

string fn = @"c:\\MyBinaryFile.DAT";
byte[] ba = File.ReadAllBytes(fn);
MemoryStream msReader = new MemoryStream(ba);

I now want to increase speed of I/O by using a zipped binary file. But how do I fit it into the above schema?

string fn = @"c:\\MyZippedBinaryFile.GZ";
//Put something here
byte[] ba = File.ReadAllBytes(fn);
//Or here
MemoryStream msReader = new MemoryStream(ba);

What is the best way to achieve this pls.

I need to end up with a MemoryStream as my next step is to deserialize it.

ManInMoon
  • 6,795
  • 15
  • 70
  • 133
  • 1
    Check this: [Unzipping a .gz file using C#](http://stackoverflow.com/questions/1348198/unzipping-a-gz-file-using-c-sharp) – scheien Apr 10 '14 at 09:49
  • What have you tried? Are you just looking for a library for unzipping gzipped stuff? – flindeberg Apr 10 '14 at 09:51

1 Answers1

1

You'd have to use a GZipStream on the content of your file.

So basically it should be like this:

string fn = @"c:\\MyZippedBinaryFile.GZ";
byte[] ba = File.ReadAllBytes(fn);
using (MemoryStream msReader = new MemoryStream(ba))
using (GZipStream zipStream = new GZipStream(msReader, CompressionMode.Decompress))
{
    // Read from zipStream instead of msReader
}

To account for the valid comment by flindenberg, you can also open the file directly without having to read the entire file into memory first:

string fn = @"c:\\MyZippedBinaryFile.GZ";
using (FileStream stream = File.OpenRead(fn))
using (GZipStream zipStream = new GZipStream(stream, CompressionMode.Decompress))
{
    // Read from zipStream instead of stream
}

You need to end up with a memory stream? No problem:

string fn = @"c:\\MyZippedBinaryFile.GZ";
using (FileStream stream = File.OpenRead(fn))
using (GZipStream zipStream = new GZipStream(stream, CompressionMode.Decompress))
using (MemoryStream ms = new MemoryStream()
{
    zipStream.CopyTo(ms);
    ms.Seek(0, SeekOrigin.Begin); // don't forget to rewind the stream!

    // Read from ms
}
Thorsten Dittmar
  • 55,956
  • 8
  • 91
  • 139
  • Is there a particular reason why you are going via `ReadAllBytes` and then decompressing? Instead of opening the file as a stream and decompressing and therefore only keeping the decompressed version of the file in memory? – flindeberg Apr 10 '14 at 09:56
  • I took as much code as possible from the original question. The OP did it like that. Of course you can also open a `FileStream` directly on the file. – Thorsten Dittmar Apr 10 '14 at 10:01
  • @ThorstenDittmar Thank you - but I need to end up with the MemoryStream – ManInMoon Apr 10 '14 at 10:02
  • @ManInMoon I edited my answer so you get your memory stream :-) – Thorsten Dittmar Apr 10 '14 at 10:03
  • @flindeberg I am happy to consider that approach too (not reading all into memory) I just want to have I/O as fast as possible. – ManInMoon Apr 10 '14 at 10:07
  • @ManInMoon His third example does all that. May I ask why you need a memory stream? – flindeberg Apr 10 '14 at 10:21
  • Third example seems to works well, but had to remove the USINGS.. Many thanks – ManInMoon Apr 10 '14 at 11:02
  • Oh, you should really, REALLY make sure to close the streams properly! Otherwise the file will remain open too long. At least keep the `using`s around `FileStream` and `GZipStream`, as they close the file and dispose of the memory used to do decryption. You can keep the `MemoryStream` as long as you want. – Thorsten Dittmar Apr 10 '14 at 11:09