我目前正在为Ruby开发一个用于管理库的洪流方法。
我很难从文件中读到这些片段。我只是不明白我该怎么做。我知道我应该对一个文件的SHA1片段长度字节进行一次消化(或者多次读取片段长度字节,或者什么?)
我指望你的帮助。伪/ Python / Ruby / PHP代码首选。
提前谢谢。
发布于 2010-09-21 07:10:36
C
// Open the file
using (var file = File.Open(...))
{
// Move to the relevant place in the file where the piece begins
file.Seek(piece * pieceLength, SeekOrigin.Begin);
// Attempt to read up to pieceLength bytes from the file into a buffer
byte[] buffer = new byte[pieceLength];
int totalRead = 0;
while (totalRead < pieceLength)
{
var read = stream.Read(buffer, totalRead, pieceLength-totalRead);
if (read == 0)
{
// the piece is smaller than the pieceLength,
// because it’s the last in the file
Array.Resize(ref buffer, totalRead);
break;
}
totalRead += read;
}
// If you want the raw data for the piece:
return buffer;
// If you want the SHA1 hashsum:
return SHA1.Create().ComputeHash(buffer);
}发布于 2010-09-21 07:28:19
请看一下这个发行版:
http://prdownload.berlios.de/torrentparse/TorrentParse.GTK.0.21.zip
用PHP编写,它包含一个编码器和解码器,以及我相信的输入和输出!
https://stackoverflow.com/questions/3757965
复制相似问题