17

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.

Lucas
  • 17,277
  • 5
  • 45
  • 40
Ivan Prodanov
  • 34,634
  • 78
  • 176
  • 248
  • i didn't understand as where you want to pass the byte[] to? to the CopyStream function? – Anirudh Goel Apr 09 '09 at 11:49
  • Also in while ((len = input.Read(buffer, 0, 2000)) > 0) { output.Write(buffer, 0, len); } don't you have to re adjust the location to which you want to write the buffer? it'd over write from location 0 always. – Anirudh Goel Apr 09 '09 at 11:50
  • 1
    Regarding the CopyStream function, it is correct. You always want to write to location 0. The extra parameters govern where in the buffer to copy from. But we always copy over starting at location 0 in the buffer each time through the loop. As a result, we always want to write to output starting from location 0 in buffer. – Yuliy May 09 '09 at 06:19

5 Answers5

78

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

byte[] buffer = new byte[200];
File.WriteAllBytes(@"c:\data.dmp", buffer);
RossFabricant
  • 12,364
  • 3
  • 41
  • 50
Christian Rodemeyer
  • 2,001
  • 1
  • 19
  • 22
5
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
  • 100,552
  • 23
  • 116
  • 167
3

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);
}
Andre Figueiredo
  • 12,930
  • 8
  • 48
  • 74
Erich Mirabal
  • 9,860
  • 3
  • 34
  • 39
1

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

Anton Gogolev
  • 113,561
  • 39
  • 200
  • 288
1

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
  • 4,571
  • 19
  • 79
  • 109