如何使用FileStream读取超过2GB的大文件

发布于 2024-12-28 01:07:02 字数 367 浏览 1 评论 0原文

如果我有一部电影 (MKV) 且其大小为 7 GB,我如何在 FileStream 中读取它.. 我们知道 int 的最大大小约为 2,147 MB​​ .. 如何从索引 3G.B 开始读取 .. 因为 FileStream 中的 .Read() 方法将偏移量作为整数,而 3 GB 超出了 int 范围.. ???

private void readingLargeFile(string path)
{
   int start = 3*(1024*1024*1024); 

   FileStream fs = new FileStream(path,FileMode.Open);
   fs.Read(data, start, (1024*8) );
}

If i have a movie (MKV) and its size like 7 G.B how can i read it in FileStream ..
we know that the maximum size of int is about 2,147 MB .. how to start read from index 3G.B .. since the .Read() method in FileStream takes the offset as an integer which 3 GB is out of int range .. ???

private void readingLargeFile(string path)
{
   int start = 3*(1024*1024*1024); 

   FileStream fs = new FileStream(path,FileMode.Open);
   fs.Read(data, start, (1024*8) );
}

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

恋竹姑娘 2025-01-04 01:07:02

那次阅读并没有达到你想象的那样。

Read 中的偏移量是距开始写入数据的缓冲区开头的偏移量,而不是文件中的偏移量从哪里开始阅读。

如果您已经部分填充了缓冲区并且想要添加更多内容,则它通常只是非零:

fs.Read (data,  0, 8 );   // Put first 8 bytes of file at buffer start
fs.Read (data, 16, 8 );   // Put next  8 bytes of file at buffer end
fs.Read (data,  8, 8 );   // Put first 8 bytes of file at buffer middle

在该示例中,包含 aaaaaaaabbbbbbbbcccccccc 的文件最终将在缓冲区中如下:

aaaaaaaaccccccccbbbbbbbb

您需要首先寻找,并且使用long作为偏移值,因此它应该能够很容易地处理8G文件。像这样的事情将是一个很好的起点:

private void readingLargeFile (string path) {
   long start = 3L * 1024L * 1024L * 1024L; 

   FileStream fs = new FileStream (path, FileMode.Open);
   fs.Seek (start, SeekOrigin.Begin)
   fs.Read (data, 0, 8 * 1024 );
}

Seek 更改为文件的当前位置(它将读取和/或写入的位置,具体取决于您调用的打开模式和函数)。

因此,fs.Seek (start, SeekOrigin.Begin) 会将文件指针设置为从文件开头开始的 start 字符。您还可以指定除 SeekOrigin.Begin 之外的其他移动方法,从当前位置(例如向前 27 个字节)查找,或从文件末尾查找。

MSDN 上提供了关于 Seek阅读

That read doesn't do what you think it does.

The offset in Read is the offset from the start of the buffer at which to start writing the data, it is not the offset in the file at which to start reading.

It's only usually non-zero if you've already partially populated the buffer and you want to tack on a bit more:

fs.Read (data,  0, 8 );   // Put first 8 bytes of file at buffer start
fs.Read (data, 16, 8 );   // Put next  8 bytes of file at buffer end
fs.Read (data,  8, 8 );   // Put first 8 bytes of file at buffer middle

With that example, the file containing aaaaaaaabbbbbbbbcccccccc would end up in the buffer as:

aaaaaaaaccccccccbbbbbbbb

You need to seek first, and that uses long as an offset value so it should be able to handle 8G files quite easily. Something like this would be a good starting point:

private void readingLargeFile (string path) {
   long start = 3L * 1024L * 1024L * 1024L; 

   FileStream fs = new FileStream (path, FileMode.Open);
   fs.Seek (start, SeekOrigin.Begin)
   fs.Read (data, 0, 8 * 1024 );
}

The Seek changes to current position of the file (where it will read from and/or write to, depending on the open mode and functions you call).

So fs.Seek (start, SeekOrigin.Begin) will set the file pointer to start characters from the beginning of the file. You can also specify other methods of movement than SeekOrigin.Begin, seeking from the current position like 27 bytes forward, or seeking from the end of the file.

Full detail are available on MSDN for both Seek and Read.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文