views:

7882

answers:

5

Hello,

My TCP Client receives a image within a packet.The image is compressed with zlib.The task is to decompress the image and put it on the form.

I'm planning to save the compressed image in the current directory,decompress it and load the decompressed file on the form.

The first problem comes with saving the file(compressed).The zlib can save it decompressed.

The code below loads the compressed file and saves it after decompression.

    private void decompressFile(string inFile, string outFile)
 {
  System.IO.FileStream outFileStream = new System.IO.FileStream(outFile, System.IO.FileMode.Create);
  zlib.ZOutputStream outZStream = new zlib.ZOutputStream(outFileStream);
  System.IO.FileStream inFileStream = new System.IO.FileStream(inFile, System.IO.FileMode.Open);   
  try
  {
   CopyStream(inFileStream, outZStream);
  }
  finally
  {
   outZStream.Close();
   outFileStream.Close();
   inFileStream.Close();
  }
 }

    public static void CopyStream(System.IO.Stream input, System.IO.Stream output)
 {
  byte[] buffer = new byte[2000];
  int len;
  while ((len = input.Read(buffer, 0, 2000)) > 0)
  {
   output.Write(buffer, 0, len);
  }
  output.Flush();
 }

How to pass the byte[] array directly to that function? I'm planning to save it as compressed and then call the function with the location of the compressed file,but I don't know neither how to save a file from a byte[] array nor a way to pass the byte[] array as the input file.

Any help will be highly appreciated.

Thanks.

+1  A: 

Stick the byte array you received into a MemoryStream and compress/decompress it on the fly without using temporary files.

Anton Gogolev
+14  A: 

Use the static void System.IO.File.WriteAllBytes(string path, byte[] bytes) method.

byte[] buffer = new byte[200];
File.WriteAllBytes(@"c:\data.dmp", buffer);
Christian Rodemeyer
you forgot to escape your characters ;). parameter should be @"c:\data.dmp" or "c:\\data.dmp"
Erich Mirabal
A: 
public static void SaveFile(this Byte[] fileBytes, string fileName)
{
    FileStream fileStream = new FileStream(fileName, FileMode.Create, FileAccess.ReadWrite);
    fileStream.Write(fileBytes, 0, fileBytes.Length);
    fileStream.Close();
}
Robin Day
A: 

You can try this code

 private void t1()
    {
        FileStream f1 = new FileStream("C:\\myfile1.txt", FileMode.Open);
        int length = Convert.ToInt16(f1.Length);
        Byte[] b1 = new Byte[length];
        f1.Read(b1, 0, length);
        File.WriteAllBytes("C:\\myfile.txt",b1);
        f1.Dispose();
    }
Anirudh Goel
You're forgetting to dispose the FileStream.
Noldorin
thanks. i fixed that right.
Anirudh Goel
Anirudh, get used to using a 'using' statement instead. refer to Erich's answer, above.
Pure.Krome
+2  A: 

In addition to what everyone else has already stated, I would also suggest you use 'using' clauses since all those objects implement IDisposable.

using(FileStream outFileStream = new ...)
using(ZOutputStream outZStream = new ...)
using(FileStream inFileStream = new ...)
{
CopyStream(inFileStream, outZStream);
}

Erich Mirabal