【问题标题】:Save file from a byte[] in C# NET 3.5从 C# NET 3.5 中的字节 [] 保存文件
【发布时间】:2010-10-18 13:02:31
【问题描述】:

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

我打算将压缩后的图片保存在当前目录下,解压后加载到表单中。

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

下面的代码加载压缩文件,解压后保存。

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

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

我们将不胜感激。

谢谢。

【问题讨论】:

  • 我不明白您要将字节 [] 传递到哪里?到 CopyStream 函数?
  • 同样在 while ((len = input.Read(buffer, 0, 2000)) > 0) { output.Write(buffer, 0, len);您不必重新调整要写入缓冲区的位置吗?它总是从位置 0 重写。
  • 关于CopyStream函数,是正确的。您总是想写入位置 0。额外的参数控制缓冲区中的复制位置。但是每次循环时,我们总是从缓冲区中的位置 0 开始复制。因此,我们总是希望从缓冲区中的位置 0 开始写入输出。

标签: c# .net-3.5 compression zlib


【解决方案1】:

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

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

【讨论】:

  • 你忘了转义你的角色;)。参数应该是@"c:\data.dmp" 或 "c:\\data.dmp"
  • 编码怎么样??对于示例,将文件(ANSI,UTF-8)读取到字节数组,然后写入新文件名
  • 哇。为什么不能总是那么容易? +1
【解决方案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】:

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

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

    【讨论】:

      【解决方案4】:

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

      【讨论】:

        【解决方案5】:

        你可以试试这个代码

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

        【讨论】:

        • 您忘记处理 FileStream。
        • Anirudh,习惯于使用“使用”语句。请参阅上面 Erich 的回答。
        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2012-02-29
        • 2012-10-24
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多