示例#1
0
 internal FileSliceInfo_Md5 GetFilesSlice_Md5Info(string fileName, SeatManageSubsystem system)
 {
     try
     {
         string filepath = "";
         if (fileName.IndexOf(@"\") == 0)//如果\第一次出现的位置为1,则不用加斜杠
         {
             filepath = string.Format("{0}{1}{2}", uploadFolder, system.ToString(), fileName);
         }
         else
         {
             filepath = string.Format(@"{0}{1}\{2}", uploadFolder, system.ToString(), fileName);
         }
         FileSliceInfo_Md5 file = new FileSliceInfo_Md5();
         file.Name   = fileName;
         file.Length = 0;
         file.Offset = 0;
         file.Data   = null;
         if (System.IO.File.Exists(filepath))
         {
             //文件已经存在
             System.IO.FileInfo fi = new System.IO.FileInfo(filepath);
             file.Md5    = SeatManage.SeatManageComm.SeatComm.GetMD5HashFromFile(filepath);
             file.Length = fi.Length;
             file.Offset = fi.Length;//返回追加数据后的文件位置
             fi          = null;
             return(file);
         }
         else
         {
             return(null);
         }
     }
     catch (Exception ex)
     {
         throw ex;
     }
 }
        /// <summary>
        /// 下载文件
        /// </summary>
        /// <param name="filePath">文件的完整</param>
        /// <param name="relativePath">文件的相对路径</param>
        /// <param name="system">系统名称</param>
        /// <returns></returns>
        public bool FileDownLoad(string filePath, string relativePath, SeatManageSubsystem system)
        {
            for (int i = 0; i < 50; i++)
            {
                //获取文件的路径,已经保存的文件名
                FileStream   fs     = null;
                BinaryWriter writer = null;//初始化文件写入器
                try
                {
                    string            fileName = relativePath;// filePath.Substring(filePath.LastIndexOf(@"\") + 1);
                    string            dirPath  = filePath.Substring(0, filePath.LastIndexOf(@"\") + 1);
                    FileSliceInfo     file2    = FileTransportBll.GetFilesSlice_Md5Info(fileName, system);
                    FileSliceInfo_Md5 f        = file2 as FileSliceInfo_Md5;
                    if (file2 == null)
                    {
                        if (DownloadError != null)
                        {
                            DownloadError(string.Format("文件{0}下载失败。", fileName));
                        }
                        return(false);
                    }
                    else if (File.Exists(filePath))
                    {
                        //先设置文件只读属性,然后再删除,不然无法删除文件
                        File.SetAttributes(filePath, System.IO.FileAttributes.Normal);
                        if (f.Md5 == SeatManageComm.SeatComm.GetMD5HashFromFile(filePath))
                        {
                            return(true);
                        }
                    }
                    file2.Name   = fileName;
                    file2.Offset = 0;

                    //如果文件目录不存在,创建文件所存放的目录.
                    if (!Directory.Exists(dirPath))
                    {
                        Directory.CreateDirectory(dirPath);
                    }

                    //文件先下载,下载完成后再删除
                    fs = new FileStream(filePath + ".tz", FileMode.OpenOrCreate);

                    file2.Offset = fs.Length;
                    if (file2.Offset != file2.Length)
                    {
                        //文件偏移位置,表示从这个位置开始进行后面的数据添加
                        writer = new BinaryWriter(fs);//初始化文件写入器
                        file2  = FileTransportBll.FileDownLoad(file2, system);
                        i      = 0;
                        while (file2.Data != null)
                        {
                            //打开文件
                            long Offset = file2.Offset; //file.Offset

                            if (Offset >= int.MaxValue)
                            {
                                writer.Seek(int.MaxValue, SeekOrigin.Begin);
                                Offset = Offset - int.MaxValue;
                                while (Offset > 0)
                                {
                                    if (Offset >= int.MaxValue)
                                    {
                                        writer.Seek(int.MaxValue, SeekOrigin.Current);
                                        Offset = Offset - int.MaxValue;
                                    }
                                    else
                                    {
                                        writer.Seek(int.Parse(Offset.ToString()), SeekOrigin.Current);
                                        Offset = 0;
                                    }
                                }
                            }
                            else
                            {
                                writer.Seek(Int32.Parse(Offset.ToString()), SeekOrigin.Begin);//设置文件的写入位置
                            }

                            writer.Write(file2.Data); //写入数据
                            file2.Offset = fs.Length; //返回追加数据后的文件位置
                            file2.Data   = null;
                            file2        = FileTransportBll.FileDownLoad(file2, system);
                            i            = 0;
                            if (HandleProgress != null)
                            {
                                int progress = (int)(((double)file2.Offset / (double)((long)file2.Length)) * 100);
                                HandleProgress(progress);
                            }
                        }
                        writer.Close();
                        fs.Close();
                    }
                    else//否则文件已经下载完成,只是还没有更改后缀,则关闭文件流
                    {
                        fs.Close();
                    }
                    if (File.Exists(filePath + ".tz"))
                    {
                        if (File.Exists(filePath))
                        {
                            File.Delete(filePath);
                        }
                        File.Move(filePath + ".tz", filePath);
                    }
                    return(true);
                }
                catch (Exception ex)
                {
                    if (writer != null)
                    {
                        writer.Close();
                    }
                    if (fs != null)
                    {
                        fs.Close();
                    }
                }
            }
            return(false);
        }