【问题标题】:Decode (BEncode) torrent files解码 (BEncode) 种子文件
【发布时间】:2015-11-11 02:16:34
【问题描述】:

您好,我正在使用 C# 在 VS15 中制作控制台应用程序。

如何解码 torrent 文件?要获取 torrent 文件的名称、大小和日期?我想从服务器下载一个 torrent 文件,然后对其进行解码以查看名称、大小和日期。到目前为止,我可以使用 WebCLient 下载文件,但我已经搜索并搜索如何解码 torrent 文件,但没有运气。

我试过this library 并做到了:

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

但我不太明白字典给了我什么?我想在控制台中输出种子信息。

【问题讨论】:

  • 在您链接的同一页面上,您有大量有关如何使用它的示例。只需查看您获得的数据,您应该很清楚如何做您想做的事情。
  • 问题是我不知道如何查看我得到的数据。字典对象包含一些列表,但不包含正确的信息? @Luaan
  • 如果它不包含正确的信息,则信息不存在。按照协议规范,Metainfo files (also known as .torrent files) are bencoded dictionaries with the following keys: announce (The URL of the tracker) info (This maps to a dictionary, with keys described below)。所以你需要进一步解码顶级字典的info部分。但是,我仍然不明白读取 torrent 文件与 torrent 文件的“名称、大小和日期”有什么关系——这就是 FileInfo 的用途。如果您指的是 在 torrent 中描述的文件,则没有日期。
  • @Luaan。我想获取种子信息。就像当前种子中所有文件的大小一样。喜欢本站可以给我:i-tools.org/torrent/exec
  • 规范“如何”。你还需要什么?你有一个可以解码数据的库,并且你有一个数据结构是什么的规范。你知道,你不会为你作为程序员遇到的每一个问题找到一个分步指南(这非常接近于一个分步指南)。

标签: c# decode torrent bencoding


【解决方案1】:

我最近添加了专门用于处理 torrent 文件的功能。到目前为止,它是非常基础的,只有一些属性可以方便地访问一些信息。

您应该能够像这样提取文件的名称和大小:

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

有关.torrent 中存储的数据的更多信息,请查看BitTorrentSpecification

【讨论】:

  • 由于DecodeTorrentFile 的路径已更改,这似乎不再有效?
  • 图书馆从那时起发生了很大的变化。你可以像这样解析一个 torrent 文件:new BencodeParser().Parse<Torrent>("path-to-file").
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2013-06-09
  • 2020-08-20
  • 2014-05-15
  • 2015-09-10
  • 2020-12-23
  • 2011-02-11
  • 1970-01-01
相关资源
最近更新 更多