admin管理员组

文章数量:1532440

2023年12月28日发(作者:)

对于这个类在解析Torrent 文件时候可能有错误,请大家指正。先把文件解析的类贴出来,连接有空再发把。使用

TorrentFile File = new TorrentFile(me);tFileInfoClass[] 文件信息

tAnnounceList 服务器列表

全部代码

using System;

using c;

using ;

using ;

using tions;

namespace ile

{

///

/// Torrent文件

/// *************

/// 2008-08-28

///

public class TorrentFile

{

#region 私有字段

private string _OpenError = "";

private bool _OpenFile = false;

private string _TorrentAnnounce = "";

private IList _TorrentAnnounceList = new List();

private DateTime _TorrentCreateTime = new DateTime(1970,1,1,0,0,0);

private long _TorrentCodePage = 0;

private string _TorrentComment = "";

private string _TorrentCreatedBy = "";

private string _TorrentEncoding = "";

private string _TorrentCommentUTF8 = "";

private IList _TorrentFileInfo

List();

= new

private string _TorrentName = "";

private string _TorrentNameUTF8 = "";

private long _TorrentPieceLength = 0;

private byte[] _TorrentPieces;

private string _TorrentPublisher = "";

private string _TorrentPublisherUTF8 = "";

private string _TorrentPublisherUrl = "";

private string _TorrentPublisherUrlUTF8 = "";

private IList _TorrentNotes = new List();

#endregion

#region 属性

///

/// 错误信息

///

public string OpenError { set { _OpenError = value; } get { return _OpenError; } }

///

/// 是否正常打开文件

///

public bool OpenFile { set { _OpenFile = value; } get { return _OpenFile; } }

///

/// 服务器的URL(字符串)

///

public string TorrentAnnounce { set { _TorrentAnnounce = value; } get { return

_TorrentAnnounce; } }

///

/// 备用tracker服务器列表(列表)

///

public IList TorrentAnnounceList { set { _TorrentAnnounceList = value; } get

{ return _TorrentAnnounceList; } }

///

/// 种子创建的时间,Unix标准时间格式,从1970 1月1日 00:00:00到创建时间的秒数(整数)

///

public DateTime TorrentCreateTime { set { _TorrentCreateTime = value; } get { return

_TorrentCreateTime; } }

///

/// 未知数字CodePage

///

public long TorrentCodePage { set { _TorrentCodePage = value; } get { return

_TorrentCodePage; } }

///

/// 种子描述

///

public string TorrentComment { set { _TorrentComment = value; } get { return

_TorrentComment; } }

///

/// 编码方式

///

public string TorrentCommentUTF8 { set { _TorrentCommentUTF8 = value; } get

{ return _TorrentCommentUTF8; } }

///

/// 创建人

///

public string TorrentCreatedBy { set { _TorrentCreatedBy = value; } get { return

_TorrentCreatedBy; } }

///

/// 编码方式

///

public string TorrentEncoding { set { _TorrentEncoding = value; } get { return

_TorrentEncoding; } }

///

/// 文件信息

///

public IList TorrentFileInfo { set { _TorrentFileInfo = value; }

get { return _TorrentFileInfo; } }

///

/// 种子名

///

public string TorrentName { set { _TorrentName = value; } get { return

_TorrentName; } }

///

/// 种子名UTF8

///

public string TorrentNameUTF8 { set { _TorrentNameUTF8 = value; } get { return

_TorrentNameUTF8; } }

///

/// 每个块的大小,单位字节(整数)

///

public long TorrentPieceLength { set { _TorrentPieceLength = value; } get { return

_TorrentPieceLength; } }

///

/// 每个块的20个字节的SHA1 Hash的值(二进制格式)

///

private byte[] TorrentPieces { set { _TorrentPieces = value; } get { return

_TorrentPieces; } }

///

/// 出版

///

public string TorrentPublisher { set { _TorrentPublisher = value; } get { return

_TorrentPublisher; } }

///

/// 出版UTF8

///

public string TorrentPublisherUTF8 { set { _TorrentPublisherUTF8 = value; } get

{ return _TorrentPublisherUTF8; } }

///

/// 出版地址

///

public string TorrentPublisherUrl { set { _TorrentPublisherUrl = value; } get { return

_TorrentPublisherUrl; } }

///

/// 出版地址

///

public string TorrentPublisherUrlUTF8 { set { _TorrentPublisherUrlUTF8 = value; } get

{ return _TorrentPublisherUrlUTF8; } }

///

/// NODES

///

public IList TorrentNotes { set { _TorrentNotes = value; } get { return

_TorrentNotes; } }

#endregion

public TorrentFile(string FileName)

{

ream TorrentFile = new ream(FileName,

);

byte[] TorrentBytes = new byte[];

(TorrentBytes, 0, );

();

if ((char)TorrentBytes[0] != 'd')

{

if ( == 0) OpenError = "错误的Torrent文件,开头第1字节不是100";

return;

}

GetTorrentData(TorrentBytes);

}

#region 开始读数据

///

/// 开始读取

///

///

private void GetTorrentData(byte[] TorrentBytes)

{

int StarIndex = 1;

while (true)

{

object Keys = GetKeyText(TorrentBytes, ref StarIndex);

if (Keys == null)

{

if (StarIndex >= ) OpenFile = true;

break;

}

if (GetValueText(TorrentBytes, ref StarIndex, ng().ToUpper()) ==

false) break;

}

}

#endregion

///

/// 读取结构

///

///

///

///

///

private bool GetValueText(byte[] TorrentBytes,ref int StarIndex, string Keys)

{

switch (Keys)

{

case "ANNOUNCE":

TorrentAnnounce = GetKeyText(TorrentBytes, ref StarIndex).ToString();

break;

case "ANNOUNCE-LIST":

int ListCount = 0;

ArrayList _TempList=GetKeyData(TorrentBytes, ref StarIndex, ref

ListCount);

for (int i = 0; i != _; i++)

{

(_TempList[i].ToString());

}

break;

case "CREATION DATE":

object Date = GetKeyNumb(TorrentBytes, ref StarIndex).ToString();

if (Date == null)

{

if ( == 0) OpenError = "CREATION DATE 返回不是数字类型";

return false;

}

TorrentCreateTime

ks((ng()));

break;

case "CODEPAGE":

object CodePageNumb = GetKeyNumb(TorrentBytes, ref StarIndex);

if (CodePageNumb == null)

{

if ( == 0) OpenError = "CODEPAGE 返回不是数字类型";

return false;

}

TorrentCodePage = (ng());

break;

case "ENCODING":

TorrentEncoding = GetKeyText(TorrentBytes, ref StarIndex).ToString();

break;

case "CREATED BY":

TorrentCreatedBy = GetKeyText(TorrentBytes, ref StarIndex).ToString();

break;

case "COMMENT":

TorrentComment = GetKeyText(TorrentBytes, ref StarIndex).ToString();

break;

case "-8":

TorrentCommentUTF8

StarIndex).ToString();

break;

case "INFO":

int FileListCount = 0;

GetFileInfo(TorrentBytes, ref StarIndex, ref FileListCount);

break;

case "NAME":

TorrentName = GetKeyText(TorrentBytes, ref StarIndex).ToString();

= GetKeyText(TorrentBytes, ref

=

break;

case "-8":

TorrentNameUTF8

StarIndex).ToString();

break;

case "PIECE LENGTH":

object PieceLengthNumb = GetKeyNumb(TorrentBytes, ref StarIndex);

if (PieceLengthNumb == null)

{

if ( == 0) OpenError = "PIECE LENGTH 返回不是数字类型";

return false;

}

TorrentPieceLength = (ng());

break;

case "PIECES":

TorrentPieces = GetKeyByte(TorrentBytes, ref StarIndex);

break;

case "PUBLISHER":

TorrentPublisher = GetKeyText(TorrentBytes, ref StarIndex).ToString();

break;

case "-8":

TorrentPublisherUTF8

StarIndex).ToString();

break;

case "PUBLISHER-URL":

TorrentPublisherUrl

= GetKeyText(TorrentBytes, ref

= GetKeyText(TorrentBytes, ref

= GetKeyText(TorrentBytes, ref

StarIndex).ToString();

break;

case "-8":

TorrentPublisherUrlUTF8

StarIndex).ToString();

= GetKeyText(TorrentBytes, ref

break;

case "NODES":

int NodesCount = 0;

ArrayList _NodesList = GetKeyData(TorrentBytes, ref StarIndex, ref

NodesCount);

int IPCount= _/2;

for (int i = 0; i != IPCount; i++)

{

(_NodesList[i * 2] + ":" + _NodesList[(i * 2) +

1]);

}

break;

default:

return false;

}

return true;

}

#region 获取数据

///

/// 获取列表方式 "I1:Xe"="X" 会调用GetKeyText

///

///

///

///

private ArrayList GetKeyData(byte[] TorrentBytes, ref int StarIndex, ref int ListCount)

{

ArrayList _TempList = new ArrayList();

while (true)

{

string TextStar = ing(TorrentBytes, StarIndex, 1);

switch (TextStar)

{

case "l":

StarIndex++;

ListCount++;

break;

case "e":

ListCount--;

StarIndex++;

if (ListCount == 0) return _TempList;

break;

case "i":

_(GetKeyNumb(TorrentBytes,ref

StarIndex).ToString());

break;

default:

object ListText = GetKeyText(TorrentBytes, ref StarIndex);

if (ListText != null)

{

_(ng());

}

else

{

if ( == 0)

{

OpenError = "错误的Torrent文件,ANNOUNCE-LIST错误";

return _TempList;

}

}

break;

}

}

}

///

/// 获取字符串

///

///

///

///

private object GetKeyText(byte[] TorrentBytes, ref int StarIndex)

{

int Numb = 0;

int LeftNumb = 0;

for (int i = StarIndex; i != ; i++)

{

if ((char)TorrentBytes[i] == ':') break;

if ((char)TorrentBytes[i] == 'e')

{

LeftNumb++;

continue;

}

Numb++;

}

StarIndex += LeftNumb;

string TextNumb = ing(TorrentBytes, StarIndex, Numb);

try

{

int ReadNumb = (TextNumb);

StarIndex = StarIndex + Numb + 1;

object KeyText = ing(TorrentBytes, StarIndex,

ReadNumb);

StarIndex += ReadNumb;

return KeyText;

}

catch

{

return null;

}

}

///

/// 获取数字

///

///

///

private object GetKeyNumb(byte[] TorrentBytes, ref int StarIndex)

{

if (ing(TorrentBytes, StarIndex, 1) == "i")

{

int Numb = 0;

for (int i = StarIndex; i != ; i++)

{

if ((char)TorrentBytes[i] == 'e') break;

Numb++;

}

StarIndex++;

long RetNumb = 0;

try

{

RetNumb=(ing(TorrentBytes, StarIndex,

Numb-1));

StarIndex += Numb;

return RetNumb;

}

catch

{

return null;

}

}

else

{

return null;

}

}

///

/// 获取BYTE数据

///

///

///

///

private byte[] GetKeyByte(byte[] TorrentBytes, ref int StarIndex)

{

int Numb = 0;

for (int i = StarIndex; i != ; i++)

{

if ((char)TorrentBytes[i] == ':') break;

Numb++;

}

string TextNumb = ing(TorrentBytes, StarIndex, Numb);

try

{

int ReadNumb = (TextNumb);

StarIndex = StarIndex + Numb + 1;

Stream KeyMemory

Stream(TorrentBytes, StarIndex, ReadNumb);

byte[] KeyBytes = new byte[ReadNumb];

(KeyBytes, 0, ReadNumb);

();

StarIndex += ReadNumb;

return KeyBytes;

}

catch

{

return null;

}

}

///

/// 对付INFO的结构

///

///

///

///

private void GetFileInfo(byte[] TorrentBytes,ref int StarIndex,ref int ListCount)

{

if ((char)TorrentBytes[StarIndex] != 'd') return;

StarIndex++;

= new

if(GetKeyText(TorrentBytes, ref StarIndex).ToString().ToUpper()=="FILES")

{

TorrentFileInfoClass Info = new TorrentFileInfoClass();

while (true)

{

string TextStar = ing(TorrentBytes, StarIndex, 1);

switch (TextStar)

{

case "l":

StarIndex++;

ListCount++;

break;

case "e":

ListCount--;

StarIndex++;

if (ListCount == 1) (Info);

if (ListCount == 0) return;

break;

case "d":

Info = new TorrentFileInfoClass();

ListCount++;

StarIndex++;

break;

default:

object ListText = GetKeyText(TorrentBytes, ref StarIndex);

if (ListText == null) return;

switch (ng().ToUpper()) //转换为大写

{

= GetKeyText(TorrentBytes, ref

case "ED2K":

2K

StarIndex).ToString();

break;

case "FILEHASH":

sh

StarIndex).ToString();

break;

case "LENGTH":

break;

case "PATH":

= GetKeyText(TorrentBytes, ref

=

64(GetKeyNumb(TorrentBytes, ref StarIndex));

int PathCount=0;

ArrayList PathList = GetKeyData(TorrentBytes, ref

StarIndex, ref PathCount);

string Temp = "";

for (int i = 0; i != ; i++)

{

Temp += PathList[i].ToString();

}

=Temp;

break;

case "-8":

int PathUtf8Count = 0;

ArrayList Pathutf8List = GetKeyData(TorrentBytes,

ref StarIndex, ref PathUtf8Count);

string UtfTemp = "";

for (int i = 0; i != ; i++)

{

UtfTemp += Pathutf8List[i].ToString();

}

F8=UtfTemp;

break; }

break;

} }

}

}

#endregion

///

/// 对应结构 INFO 多个文件时

///

public class TorrentFileInfoClass

{

private string path = "";

private string pathutf8 = "";

private long length = 0;

private string md5sum = "";

private string de2k = "";

private string filehash = "";

///

/// 文件路径

///

public string Path { get { return path; } set { path = value; } }

///

/// UTF8的名称

///

public string PathUTF8 { get { return pathutf8; } set { pathutf8 = value; } }

}

}

}///

/// 文件大小

///

public long Length { get { return length; } set { length = value; } }

///

/// MD5验效 (可选)

///

public string MD5Sum { get { return md5sum; } set { md5sum = value; } }

///

/// ED2K 未知

///

public string De2K { get { return de2k; } set { de2k = value; } }

///

/// FileHash 未知

///

public string FileHash { get { return filehash; } set { filehash = value; } }

本文标签: 文件时间解析列表种子