我把这个Torrent.cs 文件放在App_Code目录下using System;
using System.Collections.Generic;
using System.Text;
using System.Collections;
using System.IO;
using System.Xml;//实例化对象
//Torrent tor = new Torrent(FileName); public class Torrent
{
/// <summary>
/// tracker服务器列表
/// </summary>
public List<string> Announcelist= new List<string>();
/// <summary>
/// 种子创建的时间
/// </summary>
public DateTime CreationDate = DateTime.Now;
/// <summary>
/// 备注
/// </summary>
public string Comment = "";
/// <summary>
/// 创建人或创建程序的信息
/// </summary>
public string CreatedBy = "";
/// <summary>
/// Torrent编码
/// </summary>
public string TorrEncod = "utf-8";
/// <summary>
/// Torrent文件名编码
/// </summary>
public string TorrFileEncode = "utf-8";
/// <summary>
/// Torrent编码代码
/// </summary>
public string CodePage = "936";
/// <summary>
/// 最上层的目录名字(多文件才有)
/// </summary>
public string Name = "";
/// <summary>
/// 最上层的目录名字(多文件才有)
/// </summary>
public string NameUTF8 = "";
/// <summary>
/// 每个块的大小,单位字节
/// </summary>
public long PieceLength = 0;
/// <summary>
/// 每个块的20个字节的SHA1 Hash的值(二进制格式)
/// </summary>
public byte[] Pieces;
/// <summary>
/// 字典结构,包含文件的主要信息
/// </summary>
public List<FileInfo> FileList = new List<FileInfo>();
/// <summary>
/// 错误信息
/// </summary>
public string Error { set { _Error = value; } get { return _Error; } }
private string _Error;
private byte[] _TorrentContent;
public Torrent(string FileName)
{
if (File.Exists(FileName))
{
System.IO.FileStream TorrentFile = new System.IO.FileStream(FileName, System.IO.FileMode.Open);
_TorrentContent = new byte[TorrentFile.Length];
TorrentFile.Read(_TorrentContent, 0, _TorrentContent.Length);
TorrentFile.Close();
if ((char)_TorrentContent[0] != 'd')
{
if (Error.Length == 0) Error = "错误的Torrent文件";
return;
}
Bencoding fTorrBencode = new Bencoding(_TorrentContent);
ParseData(fTorrBencode.TorrentNode.SelectSingleNode("Torrent"));
//string fTstr = fTorrBencode.TorrentNode.InnerXml;
//File.WriteAllText(FileName + ".xml", fTstr);
}
}
private void ParseData(XmlNode DataNode)
{
ParseAnnouncelist(GetSonNode(DataNode,@"Dictionary/announce"));
ParseAnnouncelist(GetSonNode(DataNode,@"Dictionary/announce-list"));
Comment = GetNodeText(DataNode, @"Dictionary/comment");
CreatedBy = GetNodeText(DataNode, @"Dictionary/createdby");
string fTstr = GetNodeText(DataNode, @"Dictionary/creationdate");
if (fTstr !="")
{
CreationDate = DateTime.FromFileTimeUtc(Convert.ToInt32(fTstr));
}
TorrEncod = GetNodeText(DataNode, @"Dictionary/encoding");
CodePage = GetNodeText(DataNode, @"Dictionary/codepage");
Name = FoldeName(DataNode, @"Dictionary/info/Dictionary/name");
fTstr = GetNodeText(DataNode, @"Dictionary/info/Dictionary/piecelength");
if (fTstr != "")
{
PieceLength = Convert.ToInt32(fTstr);
}
XmlNode fTNode = GetSonNode(DataNode,@"Dictionary/info/Dictionary/pieces");
if (fTNode != null)
{
long fTint1 = Convert.ToInt32(fTNode.Attributes["StartInt"].Value);
long fTint2 = Convert.ToInt32(fTNode.Attributes["Leng"].Value);
Pieces = new byte[fTint2];
for (long ii = 0; ii < Pieces.Length; ii++)
{
Pieces[ii] = _TorrentContent[fTint1 + ii];
}
}
ParseFileinfo(GetSonNode(DataNode,@"Dictionary/info/Dictionary"));
}
/// <summary>
/// Tracker服务列表
/// </summary>
/// <param name="DataNode"></param>
private void ParseAnnouncelist(XmlNode DataNode)
{
if (DataNode == null)
return;
if (DataNode.HasChildNodes)
{
for (int ii = 0; ii < DataNode.ChildNodes.Count; ii++)
{
ParseAnnouncelist(DataNode.ChildNodes[ii]);
}
}
else
{
if (DataNode.InnerText != "")
{
Announcelist.Add(DataNode.InnerText);
}
}
}
/// <summary>
/// 文件信息列表
/// </summary>
/// <param name="DataNode"></param>
private void ParseFileinfo(XmlNode DataNode)
{
XmlNode fFilesNode = GetSonNode(DataNode, @"files/List");
if (fFilesNode == null)
{
FileInfo fFinfo = new FileInfo();
XmlNode fname = GetSonNode(DataNode, "name");
XmlNode code = fname.NextSibling;
XmlNode nameutf = code.NextSibling; fFinfo.Name = fname.InnerText;
string fTstr = GetNodeText(DataNode, @"length");
if (fTstr !="")
{
fFinfo.Length = Convert.ToInt32(fTstr);
}
fFinfo.NameUTF8 = nameutf.InnerText;
if (fFinfo.Name != "")
{
FileList.Add(fFinfo);
}
}
else
{
for (int ii = 0; ii < fFilesNode.ChildNodes.Count; ii++)
{
FileInfo fFinfo = new FileInfo();
fFinfo.Name = GetNodeText(fFilesNode.ChildNodes[ii], @"path/List");
string fTstr = GetNodeText(fFilesNode.ChildNodes[ii], @"length");
if (fTstr !="")
{
fFinfo.Length = Convert.ToInt32(fTstr);
}
fFinfo.NameUTF8 = GetNodeText(fFilesNode.ChildNodes[ii], @"List/Value");
if (fFinfo.Name != "")
{
FileList.Add(fFinfo);
}
}
}
} /// <summary>
/// 获得种子中文件大小的和
/// </summary>
/// <returns></returns>
public long GetFileSizeSum()
{
long fRedata = 0;
for (int ii = 0; ii < FileList.Count; ii++)
{
fRedata += FileList[ii].Length;
}
return fRedata;
} private string GetNodeText(XmlNode DataNode,string NodeUri)
{
try
{
return DataNode.SelectSingleNode(NodeUri).InnerText;
}
catch (Exception)
{
return "";
}
}
private string FoldeName(XmlNode DataNode, string NodeUri)
{
try
{
XmlNode fname = DataNode.SelectSingleNode(NodeUri);
XmlNode fcode = fname.NextSibling;
XmlNode fyname = fcode.NextSibling;
TorrFileEncode = fcode.InnerText.Substring(fcode.InnerText.IndexOf(".") + 1);
NameUTF8 = fyname.InnerText;
return fname.InnerText;
}
catch (Exception)
{
return "";
}
}
private XmlNode GetSonNode(XmlNode DataNode, string NodeUri)
{
try
{
return DataNode.SelectSingleNode(NodeUri);
}
catch (Exception)
{
return null;
}
} public string ConvEncode(string inputString, string encodeName)
{
if (inputString == null)
{
return null;
}
string tempValue = null;
byte[] buf = new byte[inputString.Length];
System.Text.Encoding.GetEncoding(encodeName).GetBytes(inputString, 0, inputString.Length, buf, 0);
tempValue = System.Text.Encoding.Default.GetString(buf);
return tempValue;
}
}
using System.Collections.Generic;
using System.Text;
using System.Collections;
using System.IO;
using System.Xml;//实例化对象
//Torrent tor = new Torrent(FileName); public class Torrent
{
/// <summary>
/// tracker服务器列表
/// </summary>
public List<string> Announcelist= new List<string>();
/// <summary>
/// 种子创建的时间
/// </summary>
public DateTime CreationDate = DateTime.Now;
/// <summary>
/// 备注
/// </summary>
public string Comment = "";
/// <summary>
/// 创建人或创建程序的信息
/// </summary>
public string CreatedBy = "";
/// <summary>
/// Torrent编码
/// </summary>
public string TorrEncod = "utf-8";
/// <summary>
/// Torrent文件名编码
/// </summary>
public string TorrFileEncode = "utf-8";
/// <summary>
/// Torrent编码代码
/// </summary>
public string CodePage = "936";
/// <summary>
/// 最上层的目录名字(多文件才有)
/// </summary>
public string Name = "";
/// <summary>
/// 最上层的目录名字(多文件才有)
/// </summary>
public string NameUTF8 = "";
/// <summary>
/// 每个块的大小,单位字节
/// </summary>
public long PieceLength = 0;
/// <summary>
/// 每个块的20个字节的SHA1 Hash的值(二进制格式)
/// </summary>
public byte[] Pieces;
/// <summary>
/// 字典结构,包含文件的主要信息
/// </summary>
public List<FileInfo> FileList = new List<FileInfo>();
/// <summary>
/// 错误信息
/// </summary>
public string Error { set { _Error = value; } get { return _Error; } }
private string _Error;
private byte[] _TorrentContent;
public Torrent(string FileName)
{
if (File.Exists(FileName))
{
System.IO.FileStream TorrentFile = new System.IO.FileStream(FileName, System.IO.FileMode.Open);
_TorrentContent = new byte[TorrentFile.Length];
TorrentFile.Read(_TorrentContent, 0, _TorrentContent.Length);
TorrentFile.Close();
if ((char)_TorrentContent[0] != 'd')
{
if (Error.Length == 0) Error = "错误的Torrent文件";
return;
}
Bencoding fTorrBencode = new Bencoding(_TorrentContent);
ParseData(fTorrBencode.TorrentNode.SelectSingleNode("Torrent"));
//string fTstr = fTorrBencode.TorrentNode.InnerXml;
//File.WriteAllText(FileName + ".xml", fTstr);
}
}
private void ParseData(XmlNode DataNode)
{
ParseAnnouncelist(GetSonNode(DataNode,@"Dictionary/announce"));
ParseAnnouncelist(GetSonNode(DataNode,@"Dictionary/announce-list"));
Comment = GetNodeText(DataNode, @"Dictionary/comment");
CreatedBy = GetNodeText(DataNode, @"Dictionary/createdby");
string fTstr = GetNodeText(DataNode, @"Dictionary/creationdate");
if (fTstr !="")
{
CreationDate = DateTime.FromFileTimeUtc(Convert.ToInt32(fTstr));
}
TorrEncod = GetNodeText(DataNode, @"Dictionary/encoding");
CodePage = GetNodeText(DataNode, @"Dictionary/codepage");
Name = FoldeName(DataNode, @"Dictionary/info/Dictionary/name");
fTstr = GetNodeText(DataNode, @"Dictionary/info/Dictionary/piecelength");
if (fTstr != "")
{
PieceLength = Convert.ToInt32(fTstr);
}
XmlNode fTNode = GetSonNode(DataNode,@"Dictionary/info/Dictionary/pieces");
if (fTNode != null)
{
long fTint1 = Convert.ToInt32(fTNode.Attributes["StartInt"].Value);
long fTint2 = Convert.ToInt32(fTNode.Attributes["Leng"].Value);
Pieces = new byte[fTint2];
for (long ii = 0; ii < Pieces.Length; ii++)
{
Pieces[ii] = _TorrentContent[fTint1 + ii];
}
}
ParseFileinfo(GetSonNode(DataNode,@"Dictionary/info/Dictionary"));
}
/// <summary>
/// Tracker服务列表
/// </summary>
/// <param name="DataNode"></param>
private void ParseAnnouncelist(XmlNode DataNode)
{
if (DataNode == null)
return;
if (DataNode.HasChildNodes)
{
for (int ii = 0; ii < DataNode.ChildNodes.Count; ii++)
{
ParseAnnouncelist(DataNode.ChildNodes[ii]);
}
}
else
{
if (DataNode.InnerText != "")
{
Announcelist.Add(DataNode.InnerText);
}
}
}
/// <summary>
/// 文件信息列表
/// </summary>
/// <param name="DataNode"></param>
private void ParseFileinfo(XmlNode DataNode)
{
XmlNode fFilesNode = GetSonNode(DataNode, @"files/List");
if (fFilesNode == null)
{
FileInfo fFinfo = new FileInfo();
XmlNode fname = GetSonNode(DataNode, "name");
XmlNode code = fname.NextSibling;
XmlNode nameutf = code.NextSibling; fFinfo.Name = fname.InnerText;
string fTstr = GetNodeText(DataNode, @"length");
if (fTstr !="")
{
fFinfo.Length = Convert.ToInt32(fTstr);
}
fFinfo.NameUTF8 = nameutf.InnerText;
if (fFinfo.Name != "")
{
FileList.Add(fFinfo);
}
}
else
{
for (int ii = 0; ii < fFilesNode.ChildNodes.Count; ii++)
{
FileInfo fFinfo = new FileInfo();
fFinfo.Name = GetNodeText(fFilesNode.ChildNodes[ii], @"path/List");
string fTstr = GetNodeText(fFilesNode.ChildNodes[ii], @"length");
if (fTstr !="")
{
fFinfo.Length = Convert.ToInt32(fTstr);
}
fFinfo.NameUTF8 = GetNodeText(fFilesNode.ChildNodes[ii], @"List/Value");
if (fFinfo.Name != "")
{
FileList.Add(fFinfo);
}
}
}
} /// <summary>
/// 获得种子中文件大小的和
/// </summary>
/// <returns></returns>
public long GetFileSizeSum()
{
long fRedata = 0;
for (int ii = 0; ii < FileList.Count; ii++)
{
fRedata += FileList[ii].Length;
}
return fRedata;
} private string GetNodeText(XmlNode DataNode,string NodeUri)
{
try
{
return DataNode.SelectSingleNode(NodeUri).InnerText;
}
catch (Exception)
{
return "";
}
}
private string FoldeName(XmlNode DataNode, string NodeUri)
{
try
{
XmlNode fname = DataNode.SelectSingleNode(NodeUri);
XmlNode fcode = fname.NextSibling;
XmlNode fyname = fcode.NextSibling;
TorrFileEncode = fcode.InnerText.Substring(fcode.InnerText.IndexOf(".") + 1);
NameUTF8 = fyname.InnerText;
return fname.InnerText;
}
catch (Exception)
{
return "";
}
}
private XmlNode GetSonNode(XmlNode DataNode, string NodeUri)
{
try
{
return DataNode.SelectSingleNode(NodeUri);
}
catch (Exception)
{
return null;
}
} public string ConvEncode(string inputString, string encodeName)
{
if (inputString == null)
{
return null;
}
string tempValue = null;
byte[] buf = new byte[inputString.Length];
System.Text.Encoding.GetEncoding(encodeName).GetBytes(inputString, 0, inputString.Length, buf, 0);
tempValue = System.Text.Encoding.Default.GetString(buf);
return tempValue;
}
}
{
/// <summary>
/// 文件名
/// </summary>
public string Name;
/// <summary>
/// 文件名UTF-8
/// </summary>
public string NameUTF8;
/// <summary>
/// 文件长度,单位字节
/// </summary>
public long Length = 0;
/// <summary>
/// 长32个字符的文件的MD5校验和,BT不使用这个值,只是为了兼容一些程序所保留
/// </summary>
public string Md5sum;
} public class Bencoding
{
private int _ErrorCode = 0;///错误代码
private long _TorrentSize = 0;///Torrent 文件长度
private long _StartIndex = 0;///Torrent 文件的当前位置
private byte[] _SourContent;///Torrent 文件内容
private XmlDocument _TorrentNode = new XmlDocument();///Torrent 节点
private XmlNode _CurrNode;
public Bencoding(byte[] Content)
{
_SourContent = Content;
_TorrentSize = Content.Length;
_CurrNode = _TorrentNode.SelectSingleNode("/");
AddSonNode("Torrent");
ParseTorrent();
}
/// <summary>
/// 错误代码
/// </summary>
public int ErrorCode
{get{return _ErrorCode;}}
/// <summary>
/// 数据根节点
/// </summary>
public XmlDocument TorrentNode
{ get { return _TorrentNode; } }
private void ParseTorrent()
{
_ErrorCode = 0;
ParseBencode();
} private void ParseBencode()
{
char fCurrStr = (char)_SourContent[_StartIndex];
switch (fCurrStr)
{
case 'i':
ParseInt();
break;
case 'd':
ParseDict();
break;
case 'l':
ParseList();
break;
case 'e':
GotoFatherNode();
break;
default:
if (char.IsDigit(fCurrStr))
{
ParseKey(ParseStr());
}
else
{
_ErrorCode=9;
_StartIndex++;
return;
}
break;
}
}
private void ParseKey(string Key)
{
switch (Key.ToLower())
{
case "announce":
AddNode("announce", ParseStr());
break;
case "announce-list":
AddSonNode("announce-list");
ParseBencode();
_CurrNode = _CurrNode.ParentNode;
break;
case "codepage":
AddNode("codepage", ParseInt());
break;
case "comment":
AddNode("comment", ParseStr());
break;
case "created by":
AddNode("createdby", ParseStr());
break;
case "encoding":
AddNode("encoding", ParseStr());
break;
case "creation date":
AddNode("creationdate", ParseInt());
break;
case "info":
AddSonNode("info");
ParseBencode();
_CurrNode = _CurrNode.ParentNode;
break;
case "files":
AddSonNode("files");
ParseBencode();
_CurrNode = _CurrNode.ParentNode;
break;
case "length":
AddNode("length", ParseInt());
break;
case "path":
AddSonNode("path");
ParseBencode();
_CurrNode = _CurrNode.ParentNode;
break;
case "pieces":
long fLeng = KeyLeng();
AddByteNode("pieces", _StartIndex, fLeng);
_StartIndex += fLeng;
break;
case "nodes":
AddSonNode("nodes");
ParseBencode();
_CurrNode = _CurrNode.ParentNode;
break;
case "name":
AddNode("name", ParseStr());
break;
case "publisher":
AddNode("publisher", ParseStr());
break;
case "publisher-url":
AddNode("publisher-url", ParseStr());
break;
case "piece length":
AddNode("piecelength", ParseInt());
break;
default:
SetCurrNodeValue(Key);
break;
}
}
/// <summary>
/// 解析数据类型数据
/// </summary>
/// <returns></returns>
private string ParseInt()
{
if ((char)_SourContent[_StartIndex] != 'i')
{
_ErrorCode = 1;
return "0";
}
long fStartInt = ++_StartIndex;
while ((char)_SourContent[_StartIndex] != 'e')
{
char fCurrStr = (char)_SourContent[_StartIndex];
if (!char.IsDigit(fCurrStr))
{
if (!(fCurrStr == '+' || fCurrStr == '-'))
{
_ErrorCode = 10;
return "0";
}
}
_StartIndex++;
}
string fReint =PickUp(fStartInt,_StartIndex);
_StartIndex++;
return fReint;
} /// <summary>
/// 解析字符类型数据
/// </summary>
/// <returns></returns>
private string ParseStr()
{
long fStrLen = KeyLeng();
string fRestr = PickUp(_StartIndex, _StartIndex + fStrLen);
_StartIndex += fStrLen;
return fRestr;
}
private long KeyLeng()
{
long fStartInt = _StartIndex;
while ((char)_SourContent[_StartIndex] != ':')
{
char fCurrStr = (char)_SourContent[_StartIndex];
if (!char.IsDigit(fCurrStr))
{
_ErrorCode = 2;
return 0;
}
_StartIndex++;
}
long fStrLen = Convert.ToInt32(PickUp(fStartInt, _StartIndex));
_StartIndex++;
return fStrLen;
}
/// <summary>
/// 解析列表类型数据
/// </summary>
/// <returns></returns>
private void ParseList()
{
if ((char)_SourContent[_StartIndex] != 'l')
{
_ErrorCode = 3;
return;
}
AddSonNode("List");
_StartIndex++;
while ((char)_SourContent[_StartIndex] != 'e')
{
ParseBencode();
}
if ((char)_SourContent[_StartIndex] == 'e')
{
GotoFatherNode();
}
}
/// <summary>
/// 解析字典类型数据
/// </summary>
/// <returns></returns>
private void ParseDict()
{
if ((char)_SourContent[_StartIndex] != 'd')
{
_ErrorCode = 4;
return;
}
AddSonNode("Dictionary");
_StartIndex++;
while ((char)_SourContent[_StartIndex] != 'e')
{
ParseBencode();
}
if ((char)_SourContent[_StartIndex] == 'e')
{
GotoFatherNode();
}
}
/// <summary>
/// 读取从开始到结束的字符
/// </summary>
/// <param name="StartInt"></param>
/// <param name="EndInt"></param>
/// <returns></returns>
private string PickUp(long StartInt, long EndInt)
{
string fRestr = Encoding.UTF8.GetString(_SourContent, (int)StartInt,(int) (EndInt - StartInt));
return fRestr;
}
private void AddNode(string NodeName,string NodeValue)
{
if (NodeName != "")
{
XmlElement fNode = _TorrentNode.CreateElement(NodeName);
fNode.InnerText = NodeValue;
//XmlNode fFatNode = _CurrNode;
_CurrNode.AppendChild(fNode);
}
}
private void AddSonNode(string NodeName)
{
if (NodeName != "")
{
XmlElement fNode = _TorrentNode.CreateElement(NodeName);
XmlNode fFatNode =_CurrNode;
fFatNode.AppendChild(fNode);
_CurrNode = fNode;
}
}
private void SetCurrNodeValue(string Value)
{
XmlElement fNode = _TorrentNode.CreateElement("Value");
fNode.InnerText = Value;
_CurrNode.AppendChild(fNode);
}
private void AddByteNode(string NodeName,long StartInt,long Leng)
{
if (NodeName != "")
{
XmlElement fNode = _TorrentNode.CreateElement(NodeName);
fNode.SetAttribute("StartInt", StartInt.ToString());
fNode.SetAttribute("Leng", Leng.ToString());
fNode.InnerText = "Byte[]";
XmlNode fFatNode = _CurrNode;
fFatNode.AppendChild(fNode);
}
}
private void GotoFatherNode()
{
_CurrNode = _CurrNode.ParentNode;
_StartIndex++;
}
}
然后用页面调用public partial class GetFile : System.Web.UI.Page
{
//使用TransmifFile下载文件
protected void Page_Load(object sender, EventArgs e)
{
string filename = Server.MapPath("Upload/1.torrent");
Torrent tor = new Torrent(filename);Response.Write(tor); } }
没有BT种子内的信息,请问是不是这样引用,我是菜鸟,望个位高手给我看看,哪不对
这个怎么调用,谢谢