使用c#从文件中读取第一个10MB

本文关键字:读取 第一个 10MB 文件 使用 | 更新日期: 2023-09-27 18:13:32

我需要获得文件的前10mb,并根据它计算md5,我如何实现这一点?我找不到任何阅读文件的样本。我有这样的东西:

FileStream file = new FileStream(fileName, FileMode.Open);
MD5 md5 = new MD5CryptoServiceProvider();
byte[] retVal = md5.ComputeHash(file);
file.Close();
StringBuilder sb = new StringBuilder();
for (int i = 0; i < retVal.Length; i++)
{
     sb.Append(retVal[i].ToString("x2"));
}
var md5Final = sb.ToString();

但它能读取整个文件。

使用c#从文件中读取第一个10MB

您可以分块读取文件,并使用TransformBlock将其分块提供给MD5CryptoServiceProvider。这样,您就不必为缓冲区消耗10MB的内存。示例:

long read = 0;
int r = -1; 
const long bytesToRead = 10 * 1024 * 1024;
const int bufferSize = 10*1024;
byte[] buffer = new byte[bufferSize];
MD5 md5 = new MD5CryptoServiceProvider();
using(var stream = new FileStream(fileName, FileMode.Open, FileAccess.Read) )
{
    while(read <= bytesToRead && r != 0) 
    {
        read += (r = stream.Read(buffer, 0, bufferSize));
        md5.TransformBlock(buffer, 0, r, null, 0);
    }
}
md5.TransformFinalBlock(buffer, 0,0);
string md5Final = String.Join("", md5.Hash.Select(x => x.ToString("x2")));

要使用小块读取文件的一部分,可以尝试:

public byte[] ReadPart(Stream stream)
{
    byte[] buffer = new byte[1024];
    int read=0;
    int chunk;
    while ( (chunk = stream.Read(buffer, read, buffer.Length-read)) > 0)
    {
        read += chunk;
        if (read == buffer.Length && read < 10*1024*1024) // 10MB
        {
            int nextByte = stream.ReadByte();
            if (nextByte==-1)
            {
                return buffer;
            }
            byte[] newBuffer = new byte[buffer.Length*2];
            Array.Copy(buffer, newBuffer, buffer.Length);
            newBuffer[read] = (byte)nextByte;
            buffer = newBuffer;
            read++;
        }
    }
    // Buffer is now too big. Shrink it.
    byte[] ret = new byte[read];
    Array.Copy(buffer, ret, read);
    return ret;
}

然后在这个数组上计算MD5。。。

  var bytes = new byte[10000000]; // Or 10*1024*1024 according to your def of a MB
  int realLength;
  using (var file = new FileStream(filename, FileMode.Open))
  {
    realLength = file.Read(bytes, 0, bytes.Length);
    // The file may be shorter than expected.
  }
  var md5 = new MD5CryptoServiceProvider();
  byte[] hash = md5.ComputeHash(bytes, 0, realLength); 

不过,我不得不承认,分配10MB可能不是最优雅的解决方案。我写的时候考虑的是10KB,而不是10MB。YMMV。

您可以将第一个10Mb读取到字节[]中,然后为ComputeHash提供一个由字节[]支持的MemoryStream实例。

您可以实现一个Stream类,该类限制底层Stream对象的长度(在您的情况下为FileStream(:

public class LengthLimitedStream : Stream
{
    private Stream baseStream;
    private long maxLength;
    public LengthLimitedStream(Stream stream, long maxLength)
    {
        if (stream.Position > maxLength) { throw new IOException(); }
        this.baseStream = stream;
        this.maxLength = maxLength;
    }
    public override bool CanRead
    {
        get { return this.baseStream.CanRead; }
    }
    public override long Length
    {
        get { return Math.Min(this.maxLength, this.baseStream.Length); }
    }
    public override long Position
    {
        get
        {
            return this.baseStream.Position;
        }
        set
        {
            if (value > maxLength)
            {
                throw new IOException();
            }
            this.baseStream.Position = value;
        }
    }
    public override int Read(byte[] buffer, int offset, int count)
    {
        if (this.Position + offset + count > this.maxLength)
        {
            count = (int)(this.maxLength - (this.Position + offset));
        }
        return this.baseStream.Read(buffer, offset, count);
    }
    // Lots of stuff omitted you may want to implement but not important in this case ...
}

(添加参数验证等内容,但你会明白的(

这样,您仍然可以使用ComputeHash(Stream)方法,而且它看起来干净简单(在我看来(

如何将文件转换为字节数组,以便在前10MB上循环。获取字节数组=>

private byte [] StreamFile(string filename)
{ 
FileStream fs = new FileStream(filename, FileMode.Open,FileAccess.Read); 
// Create a byte array of file stream length 
 byte[] fileData = new byte[fs.Length]; 
//Read block of bytes from stream into the byte array 
fs.Read(fileData,0,System.Convert.ToInt32(fs.Length)); 
//Close the File Stream 
fs.Close(); 
return fileData; //return the byte data
}

在这之后,我只需要在fileData上循环,并获取前10个MB,然后执行MD5