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

最后都变了- 提交于 2019-12-04 08:21:01

问题


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:


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

byte[] buffer = new byte[200];
File.WriteAllBytes(@"c:\data.dmp", buffer);



回答2:


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();
}



回答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);
}



回答4:


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




回答5:


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();
    }


来源:https://stackoverflow.com/questions/733832/save-file-from-a-byte-in-c-sharp-net-3-5

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!