C#解析Torrent 文件源码.docx
《C#解析Torrent 文件源码.docx》由会员分享,可在线阅读,更多相关《C#解析Torrent 文件源码.docx(18页珍藏版)》请在冰豆网上搜索。
![C#解析Torrent 文件源码.docx](https://file1.bdocx.com/fileroot1/2022-12/14/fae0bc35-2c13-4a8f-885c-a03dd52bd7bf/fae0bc35-2c13-4a8f-885c-a03dd52bd7bf1.gif)
C#解析Torrent文件源码
对于这个类在解析Torrent文件时候可能有错误,请大家指正。
先把文件解析的类贴出来,连接有空再发把。
使用
TorrentFileFile=newTorrentFile(openFileDialog1.FileName);
File.TorrentFileInfoClass[]文件信息
File.TorrentAnnounceList服务器列表
全部代码
usingSystem;
usingSystem.Collections.Generic;
usingSystem.Text;
usingSystem.Windows.Forms;
usingSystem.Collections;
namespaceZgke.OtherFile
{
///
///Torrent文件
///Zgke@S
///2008-08-28
///
publicclassTorrentFile
{
#region私有字段
privatestring_OpenError="";
privatebool_OpenFile=false;
privatestring_TorrentAnnounce="";
privateIList_TorrentAnnounceList=newList();
privateDateTime_TorrentCreateTime=newDateTime(1970,1,1,0,0,0);
privatelong_TorrentCodePage=0;
privatestring_TorrentComment="";
privatestring_TorrentCreatedBy="";
privatestring_TorrentEncoding="";
privatestring_TorrentCommentUTF8="";
privateIList_TorrentFileInfo=newList();
privatestring_TorrentName="";
privatestring_TorrentNameUTF8="";
privatelong_TorrentPieceLength=0;
privatebyte[]_TorrentPieces;
privatestring_TorrentPublisher="";
privatestring_TorrentPublisherUTF8="";
privatestring_TorrentPublisherUrl="";
privatestring_TorrentPublisherUrlUTF8="";
privateIList_TorrentNotes=newList();
#endregion
#region属性
///
///错误信息
///
publicstringOpenError{set{_OpenError=value;}get{return_OpenError;}}
///
///是否正常打开文件
///
publicboolOpenFile{set{_OpenFile=value;}get{return_OpenFile;}}
///
///服务器的URL(字符串)
///
publicstringTorrentAnnounce{set{_TorrentAnnounce=value;}get{return_TorrentAnnounce;}}
///
///备用tracker服务器列表(列表)
///
publicIListTorrentAnnounceList{set{_TorrentAnnounceList=value;}get{return_TorrentAnnounceList;}}
///
///种子创建的时间,Unix标准时间格式,从19701月1日00:
00:
00到创建时间的秒数(整数)
///
publicDateTimeTorrentCreateTime{set{_TorrentCreateTime=value;}get{return_TorrentCreateTime;}}
///
///未知数字CodePage
///
publiclongTorrentCodePage{set{_TorrentCodePage=value;}get{return_TorrentCodePage;}}
///
///种子描述
///
publicstringTorrentComment{set{_TorrentComment=value;}get{return_TorrentComment;}}
///
///编码方式
///
publicstringTorrentCommentUTF8{set{_TorrentCommentUTF8=value;}get{return_TorrentCommentUTF8;}}
///
///创建人
///
publicstringTorrentCreatedBy{set{_TorrentCreatedBy=value;}get{return_TorrentCreatedBy;}}
///
///编码方式
///
publicstringTorrentEncoding{set{_TorrentEncoding=value;}get{return_TorrentEncoding;}}
///
///文件信息
///
publicIListTorrentFileInfo{set{_TorrentFileInfo=value;}get{return_TorrentFileInfo;}}
///
///种子名
///
publicstringTorrentName{set{_TorrentName=value;}get{return_TorrentName;}}
///
///种子名UTF8
///
publicstringTorrentNameUTF8{set{_TorrentNameUTF8=value;}get{return_TorrentNameUTF8;}}
///
///每个块的大小,单位字节(整数)
///
publiclongTorrentPieceLength{set{_TorrentPieceLength=value;}get{return_TorrentPieceLength;}}
///
///每个块的20个字节的SHA1Hash的值(二进制格式)
///
privatebyte[]TorrentPieces{set{_TorrentPieces=value;}get{return_TorrentPieces;}}
///
///出版
///
publicstringTorrentPublisher{set{_TorrentPublisher=value;}get{return_TorrentPublisher;}}
///
///出版UTF8
///
publicstringTorrentPublisherUTF8{set{_TorrentPublisherUTF8=value;}get{return_TorrentPublisherUTF8;}}
///
///出版地址
///
publicstringTorrentPublisherUrl{set{_TorrentPublisherUrl=value;}get{return_TorrentPublisherUrl;}}
///
///出版地址
///
publicstringTorrentPublisherUrlUTF8{set{_TorrentPublisherUrlUTF8=value;}get{return_TorrentPublisherUrlUTF8;}}
///
///NODES
///
publicIListTorrentNotes{set{_TorrentNotes=value;}get{return_TorrentNotes;}}
#endregion
publicTorrentFile(stringFileName)
{
System.IO.FileStreamTorrentFile=newSystem.IO.FileStream(FileName,System.IO.FileMode.Open);
byte[]TorrentBytes=newbyte[TorrentFile.Length];
TorrentFile.Read(TorrentBytes,0,TorrentBytes.Length);
TorrentFile.Close();
if((char)TorrentBytes[0]!
='d')
{
if(OpenError.Length==0)OpenError="错误的Torrent文件,开头第1字节不是100";
return;
}
GetTorrentData(TorrentBytes);
}
#region开始读数据
///
///开始读取
///
///
privatevoidGetTorrentData(byte[]TorrentBytes)
{
intStarIndex=1;
while(true)
{
objectKeys=GetKeyText(TorrentBytes,refStarIndex);
if(Keys==null)
{
if(StarIndex>=TorrentBytes.Length)OpenFile=true;
break;
}
if(GetValueText(TorrentBytes,refStarIndex,Keys.ToString().ToUpper())==false)break;
}
}
#endregion
///
///读取结构
///
///
///
///
///
privateboolGetValueText(byte[]TorrentBytes,refintStarIndex,stringKeys)
{
switch(Keys)
{
case"ANNOUNCE":
TorrentAnnounce=GetKeyText(TorrentBytes,refStarIndex).ToString();
break;
case"ANNOUNCE-LIST":
intListCount=0;
ArrayList_TempList=GetKeyData(TorrentBytes,refStarIndex,refListCount);
for(inti=0;i!
=_TempList.Count;i++)
{
TorrentAnnounceList.Add(_TempList[i].ToString());
}
break;
case"CREATIONDATE":
objectDate=GetKeyNumb(TorrentBytes,refStarIndex).ToString();
if(Date==null)
{
if(OpenError.Length==0)OpenError="CREATIONDATE返回不是数字类型";
returnfalse;
}
TorrentCreateTime=TorrentCreateTime.AddTicks(long.Parse(Date.ToString()));
break;
case"CODEPAGE":
objectCodePageNumb=GetKeyNumb(TorrentBytes,refStarIndex);
if(CodePageNumb==null)
{
if(OpenError.Length==0)OpenError="CODEPAGE返回不是数字类型";
returnfalse;
}
TorrentCodePage=long.Parse(CodePageNumb.ToString());
break;
case"ENCODING":
TorrentEncoding=GetKeyText(TorrentBytes,refStarIndex).ToString();
break;
case"CREATEDBY":
TorrentCreatedBy=GetKeyText(TorrentBytes,refStarIndex).ToString();
break;
case"COMMENT":
TorrentComment=GetKeyText(TorrentBytes,refStarIndex).ToString();
break;
case"COMMENT.UTF-8":
TorrentCommentUTF8=GetKeyText(TorrentBytes,refStarIndex).ToString();
break;
case"INFO":
intFileListCount=0;
GetFileInfo(TorrentBytes,refStarIndex,refFileListCount);
break;
case"NAME":
TorrentName=GetKeyText(TorrentBytes,refStarIndex).ToString();
break;
case"NAME.UTF-8":
TorrentNameUTF8=GetKeyText(TorrentBytes,refStarIndex).ToString();
break;
case"PIECELENGTH":
objectPieceLengthNumb=GetKeyNumb(TorrentBytes,refStarIndex);
if(PieceLengthNumb==null)
{
if(OpenError.Length==0)OpenError="PIECELENGTH返回不是数字类型";
returnfalse;
}
TorrentPieceLength=long.Parse(PieceLengthNumb.ToString());
break;
case"PIECES":
TorrentPieces=GetKeyByte(TorrentBytes,refStarIndex);
break;
case"PUBLISHER":
TorrentPublisher=GetKeyText(TorrentBytes,refStarIndex).ToString();
break;
case"PUBLISHER.UTF-8":
TorrentPublisherUTF8=GetKeyText(TorrentBytes,refStarIndex).ToString();
break;
case"PUBLISHER-URL":
TorrentPublisherUrl=GetKeyText(TorrentBytes,refStarIndex).ToString();
break;
case"PUBLISHER-URL.UTF-8":
TorrentPublisherUrlUTF8=GetKeyText(TorrentBytes,refStarIndex).ToString();
break;
case"NODES":
intNodesCount=0;
ArrayList_NodesList=GetKeyData(TorrentBytes,refStarIndex,refNodesCount);
intIPCount=_NodesList.Count/2;
for(inti=0;i!
=IPCount;i++)
{
TorrentNotes.Add(_NodesList[i*2]+":
"+_NodesList[(i*2)+1]);
}
break;
default:
returnfalse;
}
returntrue;
}
#region获取数据
///
///获取列表方式"I1:
Xe"="X"会调用GetKeyText
///
///
///
///
privateArrayListGetKeyData(byte[]TorrentBytes,refintStarIndex,refintListCount)
{
ArrayList_TempList=newArrayList();
while(true)
{
stringTextStar=Encoding.UTF8.GetString(TorrentBytes,StarIndex,1);
switch(TextStar)
{
case"l":
StarIndex++;
ListCount++;
break;
case"e":
ListCount--;
StarIndex++;
if(ListCount==0)return_TempList;
break;
case"i":
_TempList.Add(GetKeyNumb(TorrentBytes,refStarIndex).ToString());
break;
default:
objectListText=GetKeyText(TorrentBytes,refStarIndex);
if(ListText!
=null)
{
_TempList.Add(ListText.ToString());
}
else
{
if(OpenError.Length==0)
{
OpenError="错误的Torrent文件,ANNOUNCE-LIST错误";
return_TempList;
}
}
break;
}
}
}
///
///获取字符串
///
///
///
///
privateobjectGetKeyText(byte[]TorrentBytes,refintStarIndex)
{
intNumb=0;
intLeftNumb=0;
for(inti=StarIndex;i!
=TorrentBytes.Length;i++)
{
if((char)TorrentBytes[i]==':
')break;
if((char)TorrentBytes[i]=='e')
{
LeftNumb++;
continue;
}
Numb++;
}
StarIndex+=LeftNumb;
stringTextNumb=Encoding