如何解决根据偏移量和长度从文件中读取数据块
int n = 0;
string encodeString = string.Empty;
using (FileStream fsSource = new FileStream("test.pdf",FileMode.Open,FileAccess.Read))
{
byte[] bytes = new byte[count];
n = fsSource.Read(bytes,offset,count);
encodeString = System.Convert.ToBase64String(bytes);
}
如果我提供 offset-0 和 length-1024,上面的代码工作正常,但是如果我第二次提供 Offset-1024 和 length-1024,它会返回错误。
我的要求是我想从偏移量到长度获取字节数组数据。
第一个块 = 0-1024
第二块 = 1024-2048
..
最后一个块 = SomeValue -Filesize。
Node.js 中使用 readChunk.sync(file_path,Number(offset),Number(size));
的示例 - 此代码能够获取从偏移量到长度的字节数组数据。
解决方法
src.pipe(dest)
,
实际上,我认为您的问题是理解代码中这些参数的概念,Count 是您的块大小,而偏移量是从哪里开始阅读,因此如果您想阅读 (1): a part of File to end Just Add To你的偏移量(偏移量+你想要寻找的字节数)但是(2):如果你想从中间读取文件的一部分,你不应该修改你的块大小你应该修改你通常写字节数组的位置这是一个 Do-While 循环,例如:
public static string ReadFileStreamInChunks()
{
const int readChunkBufferLength = 1024;
string filePath = "test.pdf";
string encodeString = string.Empty;
var readChunk = new char[readChunkBufferLength];
int readChunkLength;
using (StringWriter sw = new StringWriter())
using (FileStream fs = new FileStream(filePath,FileMode.Open,FileAccess.Read))
using (StreamReader sr = new StreamReader(fs))
{
do
{
readChunkLength = sr.ReadBlock(readChunk,readChunkBufferLength);
sw.Write(readChunk,readChunkLength);
} while (readChunkLength > 0);
return sw.ToString();
}
}
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。