我该如何阅读bittorrent文章?

2024-09-27 00:23:21 发布

您现在位置:Python中文网/ 问答频道 /正文

我目前正在为Ruby开发一个torrent metainfo管理库。在

我读档案时有点困难。我只是不明白我该怎么做。我知道我应该对一个文件的字节进行一次摘要(或者多次读取段长度字节,或者什么?)在

我指望你的帮助。 Pseudo/Python/Ruby/PHP代码优先。在

提前谢谢。在


Tags: 文件代码字节档案torrentphprubypseudo
2条回答

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);
}

请看一下这里的分布:

http://prdownload.berlios.de/torrentparse/TorrentParse.GTK.0.21.zip

它是用PHP编写的,它包含了一个编码器和解码器以及输入输出!在

相关问题 更多 >

    热门问题