我目前正在为Ruby开发一个torrent metainfo管理库。
我无法从文件中读取这些内容。我只是不明白我应该怎么做。我知道我应该SHA1消化一次文件的篇幅字节(或多次读一段长度字节,或者什么?)
我指望你的帮助。 伪/ Python / Ruby / PHP代码首选。
提前致谢。
答案 0 :(得分:1)
// 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);
}
答案 1 :(得分:-1)
请在此处查看此分发:
http://prdownload.berlios.de/torrentparse/TorrentParse.GTK.0.21.zip
用PHP编写,它包含一个编码器和解码器以及我相信的输入和输出!