对于这个类在解析Torrent 文件时候可能有错误,请大家指正。先把文件解析的类贴出来,连接有空再发把。
使用
TorrentFile File = new TorrentFile(openFileDialog1.FileName);
File.TorrentFileInfoClass[] 文件信息
File.TorrentAnnounceList 服务器列表
全部代码
using System;
using System.Collections.Generic;
using System.Text;
using System.Windows.Forms;
using System.Collections;
namespace Zgke.OtherFile
{
    /// <summary>
    /// Torrent文件
    /// Zgke@Sina
    /// 2008-08-28
    /// </summary>
    public class TorrentFile
    {
        #region 私有字段
        private string _OpenError = "";
        private bool _OpenFile = false;
        private string _TorrentAnnounce = "";
        private IList<string> _TorrentAnnounceList = new List<string>();
        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<TorrentFileInfoClass> _TorrentFileInfo = new List<TorrentFileInfoClass>();
        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<string> _TorrentNotes = new List<string>();
        #endregion
        #region 属性
        /// <summary>
        /// 错误信息
        /// </summary>
        public string OpenError { set { _OpenError = value; } get { return _OpenError; } }
        /// <summary>
        /// 是否正常打开文件
        /// </summary>
        public bool OpenFile { set { _OpenFile = value; } get { return _OpenFile; } }
        /// <summary>
        /// 服务器的URL(字符串)
        /// </summary>
        public string TorrentAnnounce { set { _TorrentAnnounce = value; } get { return _TorrentAnnounce; } }
        /// <summary>
        /// 备用tracker服务器列表(列表)
        /// </summary>
        public IList<string> TorrentAnnounceList { set { _TorrentAnnounceList = value; } get { return _TorrentAnnounceList; } }
        /// <summary>
        /// 种子创建的时间,Unix标准时间格式,从1970 1月1日 00:00:00到创建时间的秒数(整数)
        /// </summary>
        public DateTime TorrentCreateTime { set { _TorrentCreateTime = value; } get { return _TorrentCreateTime; } }
        /// <summary>
        /// 未知数字CodePage
        /// </summary>
        public long TorrentCodePage { set { _TorrentCodePage = value; } get { return _TorrentCodePage; } }
        /// <summary>
        /// 种子描述
        /// </summary>
        public string TorrentComment { set { _TorrentComment = value; } get { return _TorrentComment; } }
        /// <summary>
        /// 编码方式
        /// </summary>
        public string TorrentCommentUTF8 { set { _TorrentCommentUTF8 = value; } get { return _TorrentCommentUTF8; } }
        /// <summary>
        /// 创建人
        /// </summary>
        public string TorrentCreatedBy { set { _TorrentCreatedBy = value; } get { return _TorrentCreatedBy; } }
        /// <summary>
        /// 编码方式
        /// </summary>
        public string TorrentEncoding { set { _TorrentEncoding = value; } get { return _TorrentEncoding; } }
        /// <summary>
        /// 文件信息
        /// </summary>
        public IList<TorrentFileInfoClass> TorrentFileInfo { set { _TorrentFileInfo = value; } get { return _TorrentFileInfo; } }
        /// <summary>
        /// 种子名
        /// </summary>
        public string TorrentName { set { _TorrentName = value; } get { return _TorrentName; } }
        /// <summary>
        /// 种子名UTF8
        /// </summary>
        public string TorrentNameUTF8 { set { _TorrentNameUTF8 = value; } get { return _TorrentNameUTF8; } }
        /// <summary>
        /// 每个块的大小,单位字节(整数)
        /// </summary>
        public long TorrentPieceLength { set { _TorrentPieceLength = value; } get { return _TorrentPieceLength; } }
        /// <summary>
        /// 每个块的20个字节的SHA1 Hash的值(二进制格式)
        /// </summary>
        private byte[] TorrentPieces { set { _TorrentPieces = value; } get { return _TorrentPieces; } }
        /// <summary>
        /// 出版
        /// </summary>
        public string TorrentPublisher { set { _TorrentPublisher = value; } get { return _TorrentPublisher; } }
        /// <summary>
        /// 出版UTF8
        /// </summary>
        public string TorrentPublisherUTF8 { set { _TorrentPublisherUTF8 = value; } get { return _TorrentPublisherUTF8; } }
        /// <summary>
        /// 出版地址
        /// </summary>
        public string TorrentPublisherUrl { set { _TorrentPublisherUrl = value; } get { return _TorrentPublisherUrl; } }
        /// <summary>
        /// 出版地址
        /// </summary>
        public string TorrentPublisherUrlUTF8 { set { _TorrentPublisherUrlUTF8 = value; } get { return _TorrentPublisherUrlUTF8; } }
        /// <summary>
        /// NODES
        /// </summary>
        public IList<string> TorrentNotes { set { _TorrentNotes = value; } get { return _TorrentNotes; } }
        #endregion
     
     
        public TorrentFile(string FileName)
        {
            System.IO.FileStream TorrentFile = new System.IO.FileStream(FileName, System.IO.FileMode.Open);
            byte[] TorrentBytes = new byte[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 开始读数据
        /// <summary>
        /// 开始读取
        /// </summary>
        /// <param name="TorrentBytes"></param>
        private void GetTorrentData(byte[] TorrentBytes)
        {
            int StarIndex = 1;       
            while (true)
            {
             
             
                object Keys = GetKeyText(TorrentBytes, ref StarIndex);
                if (Keys == null)
                {
                    if (StarIndex >= TorrentBytes.Length) OpenFile = true;error parse new
                    break;
                }

版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系QQ:729038198,我们将在24小时内删除。