Ejemplo n.º 1
0
        /// <summary>
        /// 下载文件(可断点续传)
        /// </summary>
        /// <param name="ftpAddress">服务器地址,格式为 userName:Password@hostName:port(ftp://softer:[email protected]:21)</param>
        /// <param name="remoteFilename"></param>
        /// <param name="localFilename"></param>
        /// <param name="resumeOperation"></param>
        public bool DownloadFile(string ftpAddress, string remoteFilename, string localFilename, bool resumeOperation)
        {
            FtpSiteData siteData = ParseFtpAddress(ftpAddress);

            if (siteData == null)
            {
                throw new ArgumentException("Invalid ftp address format!");
            }

            using (FtpConnection connection = new FtpConnection(siteData.Host, siteData.Port, siteData.UserName, siteData.Password))
            {
                SetConnection(connection);

                AbstractFolder remoteFolder = new FtpFolder(connection);
                AbstractFile   remoteFile   = remoteFolder.GetFile(remoteFilename);
                // 不行,必须要从Folder来
                //AbstractFile remoteFile = new FtpFile(connection, remoteFilename);
                AbstractFile localFile = new DiskFile(localFilename);

                if (!resumeOperation || !localFile.Exists || remoteFile.Size < localFile.Size)
                {
                    remoteFile.CopyTo(localFile, true);
                }
                else if (remoteFile.Size == localFile.Size)
                {
                    return(true);
                }
                else if (remoteFile.Size > localFile.Size)
                {
                    byte[] buf = new byte[1024];
                    int    cnt = -1;

                    using (System.IO.Stream localStream = localFile.OpenWrite(false))
                    {
                        using (System.IO.Stream remoteStream = remoteFile.OpenRead())
                        {
                            remoteStream.Seek(localFile.Size, System.IO.SeekOrigin.Begin);
                            localStream.Seek(0, System.IO.SeekOrigin.End);

                            do
                            {
                                cnt = remoteStream.Read(buf, 0, buf.Length);
                                localStream.Write(buf, 0, cnt);
                            } while (cnt == buf.Length);
                        }
                    }
                }

                return(true);
            }



            //FtpClient client = LoginFtp(ftpAddress);
            //if (System.IO.File.Exists(localFilename))
            //{
            //    Xceed.FileSystem.DiskFile file = new Xceed.FileSystem.DiskFile(localFilename);

            //    using (System.IO.Stream localStream = file.OpenWrite(false))
            //    {
            //        client.ReceiveFile(remoteFilename, file.Size, localStream);
            //    }
            //}
            //else
            //{
            //    client.ReceiveFile(remoteFilename, localFilename);
            //}
        }