Decode (BEncode) torrent files

荒凉一梦 提交于 2019-12-06 11:14:11

问题


Hello I'm making a Console app in VS15 using C#.

How can I decode torrent files? To get the Name, Size and Date of the torrent file? I want to donwload a torrent file from a server and then decode it to see the name, size and date. So far i can download a file using WebCLient, but i have search and search for how to decode a torrent file, but without luck.

I have tried this library and did this:

using (var fs = File.OpenRead("Ubuntu.torrent"))
{
    BDictionary bdictionary = Bencode.DecodeDictionary(fs);
}

But i don't quite understand what bdictionary gives me? I want to output the torrents information in the console.


回答1:


I have recently added functionality to work specifically with torrent files. So far it is very basic and just have properties for easy access to some of the info.

You should be able to extract the name and size of files like this:

TorrentFile torrent = Bencode.DecodeTorrentFile("Ubuntu.torrent");

// Calculate info hash (e.g. "B415C913643E5FF49FE37D304BBB5E6E11AD5101")
string infoHash = torrent.CalculateInfoHash();

// Get name and size of each file in 'files' list of 'info' dictionary ("multi-file mode")
BList files = (BList)torrent.Info["files"];
foreach (BDictionary file in files)
{
    // File size in bytes (BNumber has implicit conversion to int and long)
    int size = (BNumber) file["length"];

    // List of all parts of the file path. 'dir1/dir2/file.ext' => dir1, dir2 and file.ext
    BList path = (BList) file["path"];

    // Last element is the file name
    BString fileName = (BString) path.Last();

    // Converts fileName (BString = bytes) to a string
    string fileNameString = fileName.ToString(Encoding.UTF8);
}

For more information on the data stored in a .torrent have a look at the BitTorrentSpecification.



来源:https://stackoverflow.com/questions/32067409/decode-bencode-torrent-files

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!