如何在C#中块文件? [英] how to chunk files in C# ?

查看:60
本文介绍了如何在C#中块文件?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我怎样才能将文件分块并加载到内存中然后以64mb的小步骤进入数据库?



是否有任何代码?

how can i chunk files and load them into memory and then into db in small steps like 64mb ?

is there any code for that ?

推荐答案

再次嗨:D

您可以一次将一个文件读入一个字节缓冲区,或者将现有的byte []拆分为几个块。这是很常见的做法,所以谷歌有很多帮助



这个链接应该有助于读取byte []块中的数据



特别是在第二个链接中给出的这个例子将chucks写入内存流。您可以在任何地方写下这些块:

Hi again :D
You can read a file into a byte buffer one chunk at a time, or you can split an existing byte[] into several chunks. It's pretty common practice so there's lots on google to help

This link should be helpful for reading data in byte[] chunks

in particular this example given in the second link writes the "chucks" to a memory stream. You can write these chunks anywhere:
/// <summary>
/// Reads data from a stream until the end is reached. The
/// data is returned as a byte array. An IOException is
/// thrown if any of the underlying IO calls fail.
/// </summary>
/// <param name="stream">The stream to read data from</param>
public static byte[] ReadFully (Stream stream)
{
    byte[] buffer = new byte[32768]; //set the size of your buffer (chunk)
    using (MemoryStream ms = new MemoryStream()) //You need a db connection instead
    {
        while (true) //loop to the end of the file
        {
            int read = stream.Read (buffer, 0, buffer.Length); //read each chunk
            if (read <= 0) //check for end of file
                return ms.ToArray();
            ms.Write (buffer, 0, read); //write chunk to [wherever]
        }
    }
}







我希望有所帮助。

PS:好奇,你在写什么?试图隐藏文件?有更好的方法。




I hope that helps.
PS: Just curious, what are you writing? trying to hide files? there are better ways.


打开一个流,并使用teh 读取 [ ^ ]获取大块的方法数据。这是简单的一点。

将它添加到数据库是困难的部分:除非您从文件中读取文本,否则您无法将数据附加到数据库列中(甚至包含文本) ,这将是非常低效的,所以你必须将所有的块组装成一个块,无论如何都要将它发送到数据库。如果你这样做,你也可以只使用File.ReadAllBytes获取批次并将其传递给数据库。

唯一一次真的很傻你的文件大小开始变得庞大 - 如果你在数据库中存储大量文件,那么这可能是一个重大错误!
Open a stream, and use teh Read[^] method to fetch a chunk of data. That's the simple bit.
Adding it to the DB is the difficult part: unless you are reading text from the file, you can't "append" the data into a DB column (and even with text, that would be monumentally inefficient), so you would have to assemble all the chunks into a single "block" to send it to the DB anyway. And if you are doing that, you might as well just use File.ReadAllBytes to get the lot and pass that to the DB in the first place.
The only time when this is really silly is when your file sizes start to get huge - and if you are storing huge files in your DB then that's probably a major mistake anyway!


这篇关于如何在C#中块文件?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆