Save file from a byte[] in C# NET 3.5

后端 未结 5 1700
长发绾君心
长发绾君心 2021-02-04 04:39

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 co

相关标签:
5条回答
  • 2021-02-04 05:05

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

    byte[] buffer = new byte[200];
    File.WriteAllBytes(@"c:\data.dmp", buffer);
    
    0 讨论(0)
  • 2021-02-04 05:08

    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);
    }
    
    0 讨论(0)
  • 2021-02-04 05:14

    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();
        }
    
    0 讨论(0)
  • 2021-02-04 05:18

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

    0 讨论(0)
  • 2021-02-04 05:22
    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();
    }
    
    0 讨论(0)
提交回复
热议问题