从 C# NET 3.5 中的字节 [] 保存文件

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/733832/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-04 23:04:19  来源:igfitidea点击:

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

c#.net-3.5compressionzlib

提问by Ivan Prodanov

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.

我的 TCP 客户端在一个数据包中收到一个图像。图像是用 zlib 压缩的。任务是解压缩图像并将其放在表单上。

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.

第一个问题是保存文件(压缩)。zlib 可以解压保存。

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.

如何将 byte[] 数组直接传递给该函数?我打算将其保存为压缩文件,然后使用压缩文件的位置调用该函数,但我既不知道如何从 byte[] 数组中保存文件,也不知道如何传递 byte[] 数组作为输入文件。

Any help will be highly appreciated.

任何帮助将不胜感激。

Thanks.

谢谢。

采纳答案by Christian Rodemeyer

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

使用静态 void System.IO.File.WriteAllBytes(string path, byte[] bytes) 方法。

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

回答by Anton Gogolev

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

将您收到的字节数组粘贴到 a 中,MemoryStream并在不使用临时文件的情况下动态压缩/解压缩它。

回答by Robin Day

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

回答by Anirudh Goel

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

回答by Erich Mirabal

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

除了其他人已经说过的内容之外,我还建议您使用“using”子句,因为所有这些对象都实现了 IDisposable。

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