Seeking and writing files bigger than 2GB in C#

我与影子孤独终老i 提交于 2019-12-07 15:57:27

问题


In C#, the FileStream's methods Read/Write/Seek take integer in parameter. In a previous post , I have seen a good solution to read/write files that are bigger than the virtual memory allocated to a process.

This solution works if you want to write the data from the beginning to the end. But in my case, the chunks of data I am receiving are in no particular order.

I have a code that works for files smaller than 2GB :

private void WriteChunk(byte[] data, int position, int chunkSize, int count, string path)
    {

        FileStream destination = new FileStream(path, FileMode.OpenOrCreate, FileAccess.Write);
        BinaryWriter writer = new BinaryWriter(destination);
        writer.Seek((int) (position*chunkSize), SeekOrigin.Begin);
        writer.Write(data, 0, count);
        writer.Close();
    }

Is there a way I can seek and write my chunks in files bigger than 2GB?


回答1:


Don't use int, use long. Seek takes a long.

You need to use long everywhere though and not just cast to int somewhere.




回答2:


writer.Seek((long)position*chunkSize, SeekOrigin.Begin);


来源:https://stackoverflow.com/questions/10936367/seeking-and-writing-files-bigger-than-2gb-in-c-sharp

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