gpt4 book ai didi

c# - 解码 (BEncode) 种子文件

转载 作者:太空宇宙 更新时间:2023-11-03 10:24:39 29 4
gpt4 key购买 nike

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

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

我试过了this library并这样做:

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

但我不太明白 bdictionary 给我的是什么?我想在控制台输出种子信息。

最佳答案

我最近添加了专门处理 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 .

关于c# - 解码 (BEncode) 种子文件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32067409/

29 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com