Esempio n. 1
0
        /// <summary>
        /// 将分块续传的文件释放出来并删除分块续传文件
        /// </summary>
        /// <returns></returns>
        public async Task <bool> Release()
        {
            string filePath = this.downloadPartPath.Replace(DownloadFile.Ext, string.Empty);

            if (System.IO.File.Exists(filePath))
            {
                System.IO.File.Delete(filePath);
            }
            using (FileStream write = System.IO.File.Create(filePath))
            {
                this.fileStream.Position = this.Position;
                await this.fileStream.CopyToAsync(write);
            }
            //校验服务器上的md5 如果服务器上下载数据不包含md5则视为md5校验通过
            if (this.MD5 == null || this.MD5 == MD5Tools.GetFileMd5(filePath))
            {
                this.fileStream?.Dispose();
                System.IO.File.Delete(this.downloadPartPath);
                return(true);
            }
            else
            {
                System.IO.File.Delete(filePath);
                return(false);
            }
        }
Esempio n. 2
0
        /// <summary>
        ///
        /// </summary>
        /// <param name="uri"></param>
        /// <returns></returns>
        public async void Download(string filePath)
        {
            bool   success          = false;
            string downloadPartPath = Path.Combine(filePath + ".downloadPart");
            string url = this.DownloadUrl + "?fileName=" + Path.GetFileName(filePath);

            using (FileStream fileStream = File.Create(downloadPartPath))
                using (HttpClient httpClient = new HttpClient()
                {
                    Timeout = TimeSpan.FromSeconds(30)
                })
                {
                    try
                    {
                        httpClient.DefaultRequestHeaders.Range = new RangeHeaderValue(0, null);
                        HttpResponseMessage httpResponseMessage = await httpClient.GetAsync(url, HttpCompletionOption.ResponseHeadersRead);

                        long?contentLength = httpResponseMessage.Content.Headers.ContentLength;
                        if (httpResponseMessage.Content.Headers.ContentRange != null)                //如果为空,则说明服务器不支持断点续传
                        {
                            contentLength = httpResponseMessage.Content.Headers.ContentRange.Length; //服务器上的文件大小
                        }
                        string md5    = httpResponseMessage.Content.Headers.ContentMD5 == null ? null : Convert.ToBase64String(httpResponseMessage.Content.Headers.ContentMD5);
                        Stream stream = await httpResponseMessage.Content.ReadAsStreamAsync();

                        stream.ReadTimeout = 10 * 1000;
                        DownloadFile downloadFile = new DownloadFile()
                        {
                            Length     = contentLength,
                            MD5        = md5,
                            RangeBegin = 0,
                            URL        = url
                        };
                        await Download(stream, fileStream, downloadFile);

                        if (md5 == null || md5 == MD5Tools.GetFileMd5(downloadPartPath))
                        {
                            success = true;
                        }
                    }
                    catch (Exception ex)
                    {
                        this.OnDownloadError(ex.Message);
                    }
                }
            if (success)
            {
                try
                {
                    File.Move(downloadPartPath, filePath);
                }
                catch (Exception ex)
                {
                    this.OnDownloadError(ex.Message);
                }
            }
            this.OnDownloadCompleted(success);
        }
Esempio n. 3
0
 public FileScan(string dir, string name, bool calcMD5 = true)
 {
     this.FullPath = Path.Combine(dir, name);
     this.Length   = new FileInfo(this.FullPath).Length;
     this.Version  = System.Diagnostics.FileVersionInfo.GetVersionInfo(this.FullPath).ProductVersion;
     this.Name     = name;
     if (calcMD5)
     {
         this.MD5 = MD5Tools.GetFileMd5(this.FullPath);
     }
 }
Esempio n. 4
0
        public void OnPreprocessBuild(BuildTarget InTarget, string InPath)
        {
            string streamingAssetsPath = Application.streamingAssetsPath;

            if (!Directory.Exists(streamingAssetsPath))
            {
                return;
            }
            List <FileInfo> fileInfos = FileTools.GetAllFileInfos(streamingAssetsPath);

            if (null == fileInfos)
            {
                return;
            }
            List <FileInfo> dbFileInfos = new List <FileInfo>();
            int             count       = fileInfos.Count;

            for (int i = 0; i < count; i++)
            {
                if (fileInfos[i].Extension == ".db")
                {
                    dbFileInfos.Add(fileInfos[i]);
                }
            }

            SQLite3Data data     = Resources.Load <SQLite3Data>("Sqlite3Data");
            string      dataPath = AssetDatabase.GetAssetPath(data);

            data = ScriptableObject.CreateInstance <SQLite3Data>();
            int dbCount = dbFileInfos.Count;

            data.AllData = new List <SQLite3SingleData>(dbCount);
            for (int i = 0; i < dbCount; i++)
            {
                SQLite3SingleData singleData = new SQLite3SingleData
                {
                    Name      = dbFileInfos[i].Name,
                    LocalName = MD5Tools.GetStringMd5(dbFileInfos[i].Name),
                    Md5       = MD5Tools.GetFileMd5(dbFileInfos[i].FullName)
                };
                string dirPath = dbFileInfos[i].DirectoryName;
                singleData.Directory = string.IsNullOrEmpty(dirPath) || dirPath == streamingAssetsPath
                    ? string.Empty
                    : dirPath.Replace('\\', '/').Replace(streamingAssetsPath, string.Empty);

                data.AllData.Add(singleData);
            }

            AssetDatabase.CreateAsset(data, dataPath);
            AssetDatabase.SaveAssets();
        }
        private static void WriteVersion(string InStreamPath)
        {
            FileInfo[] buildFileInfos = new DirectoryInfo(InStreamPath).GetFiles();

            int len = buildFileInfos.Length;

            List <BuildInfo> buildInfos = new List <BuildInfo>();

            for (int i = 0; i < len; ++i)
            {
                if (buildFileInfos[i].Extension == ".manifest" || buildFileInfos[i].Extension == ".meta")
                {
                    buildFileInfos[i].Delete();
                    continue;
                }

                if (buildFileInfos[i].Name == "Audio")
                {
                    buildFileInfos[i].Delete();
                    continue;
                }

                buildInfos.Add(new BuildInfo(buildFileInfos[i].Name,
                                             MD5Tools.GetFileMd5(Path.Combine(InStreamPath, buildFileInfos[i].Name))));
            }


            using (FileStream fs = new FileStream(Path.Combine(InStreamPath, "45264b0d287afd9795f479a7882b3765"),
                                                  FileMode.Create,
                                                  FileAccess.ReadWrite, FileShare.ReadWrite))
            {
                using (StreamWriter sw = new StreamWriter(fs))
                {
                    foreach (BuildInfo buildInfo in buildInfos)
                    {
                        sw.WriteLine(buildInfo.ToString());
                    }

                    sw.Flush();
                    sw.Close();
                }

                fs.Close();
            }
        }
Esempio n. 6
0
        /// <summary>
        /// 断点续传下载
        /// </summary>
        /// <param name="uri"></param>
        /// <returns></returns>
        public async void ResumeDownload(string partPath)
        {
            bool success = false;

            using (FileStream fileStream = System.IO.File.Open(partPath, FileMode.Open))
                using (HttpClient httpClient = new HttpClient()
                {
                    Timeout = TimeSpan.FromSeconds(30)
                })
                {
                    DownloadFile downloadFile = DownloadFile.FromFileStream(fileStream);
                    if (downloadFile == null) //为空说明已经完成
                    {
                        success = true;
                    }
                    else
                    {
                        try
                        {
                            httpClient.DefaultRequestHeaders.Range = new RangeHeaderValue(downloadFile.RangeBegin, null);
                            HttpResponseMessage httpResponseMessage = await httpClient.GetAsync(downloadFile.URL, HttpCompletionOption.ResponseHeadersRead);

                            long?contentLength = httpResponseMessage.Content.Headers.ContentLength;
                            if (httpResponseMessage.Content.Headers.ContentRange != null)                //如果为空,则说明服务器不支持断点续传
                            {
                                contentLength = httpResponseMessage.Content.Headers.ContentRange.Length; //服务器上的文件大小
                            }
                            string md5 = httpResponseMessage.Content.Headers.ContentMD5 == null ? null : Convert.ToBase64String(httpResponseMessage.Content.Headers.ContentMD5);
                            if (md5 != downloadFile.MD5)
                            {
                                throw new Exception("服务器的上的版本已经变化");
                            }
                            Stream stream = await httpResponseMessage.Content.ReadAsStreamAsync();

                            stream.ReadTimeout = 10 * 1000;
                            await Download(stream, fileStream, downloadFile);

                            if (fileStream.Length == downloadFile.Length)
                            {
                                if (md5 == null || md5 == MD5Tools.GetFileMd5(partPath))
                                {
                                    success = true;
                                }
                            }
                            else
                            {
                                success = false;
                            }
                        }
                        catch (Exception ex)
                        {
                            //请求错误
                            this.OnDownloadError(new ErrorArgs(ex.Message));
                        }
                    }
                }
            if (success)
            {
                try
                {
                    System.IO.File.Move(partPath, partPath.Replace(".downloadPart", ""));
                }
                catch (Exception ex)
                {
                    this.OnDownloadError(new ErrorArgs(ex.Message));
                }
            }
            this.OnDownloadCompleted(new CompletedArgs(success));
        }