private long ProcessFileSize(HttpWebResponse webResp, out string filePath)
        {
            long length = 0;

            filePath = null;
            Stream       receiveStream = webResp.GetResponseStream();
            Encoding     encode        = System.Text.Encoding.GetEncoding("utf-8");
            StreamReader readStream    = new StreamReader(receiveStream, encode);

            Char[] read = new Char[webResp.ContentLength];
            readStream.Read(read, 0, (int)webResp.ContentLength);

            string file = new string(read, 0, (int)webResp.ContentLength);

            if (file.Contains("PATH"))
            {
                file = file.Substring(5, file.Length - 5); //Removing PATH: from the output

                try
                {
                    System.IO.FileInfo fInfo = new System.IO.FileInfo(file);
                    if (fInfo.Exists)
                    {
                        length   = fInfo.Length;
                        filePath = file;
                    }
                }
                catch (Exception ex)
                {
                    WriteToLog(file, ex);
                }
            }
            else
            {
                int    position = webResp.ResponseUri.PathAndQuery.IndexOf(".pdb");
                string fileName = webResp.ResponseUri.PathAndQuery.Substring(1, position + 3);
                if (!FailedFiles.ContainsKey(fileName))
                {
                    FailedFiles.Add(fileName, " - No matching PDBs found - " + file);
                }
            }

            return(length);
        }
        private void downloadFile(Int32 fileNr)
        {
            bool headVerb = false;

            m_currentFileSize = 0;
            bool fileptr = false;

            fireEventFromBgw(Event.FileDownloadAttempting);

            FileInfo file = this.Files[fileNr];

            Int64 size = 0;

            Byte[] readBytes = new Byte[this.PackageSize];
            Int32  currentPackageSize;

            System.Diagnostics.Stopwatch speedTimer = new System.Diagnostics.Stopwatch();
            Int32     readings = 0;
            Exception exc      = null;

            FileStream writer;
            string     dirPath     = DownloadLocation + "\\" + file.Name + "\\" + file.PdbGuid;
            string     downloadUrl = this.Files[fileNr].Path;

            HttpWebRequest  webReq;
            HttpWebResponse webResp = null;

            try
            {
                webReq           = (HttpWebRequest)System.Net.WebRequest.Create(downloadUrl);
                webReq.UserAgent = Constants.SymbolServer;
                webResp          = (HttpWebResponse)webReq.GetResponseNoException();
                if (webResp.StatusCode == HttpStatusCode.NotFound)
                {
                    webResp = Retry(fileNr, headVerb);

                    if (webResp.StatusCode == HttpStatusCode.OK)
                    {
                        file.IsCompressed = true;
                        size = webResp.ContentLength;
                    }

                    if (webResp.StatusCode == HttpStatusCode.NotFound)
                    {
                        webResp = RetryFilePointer(fileNr);
                        fileptr = true;
                    }

                    if (webResp.StatusCode != HttpStatusCode.OK)
                    {
                        if (!FailedFiles.ContainsKey(file.Name))
                        {
                            FailedFiles.Add(file.Name, " - " + webResp.StatusCode + "  " + webResp.StatusDescription);
                        }
                    }
                }
                else if (webResp.StatusCode == HttpStatusCode.OK)
                {
                    size = webResp.ContentLength;
                }
            }
            catch (Exception ex)
            {
                exc = ex;
                WriteToLog(file.Name, exc);
            }
            if (webResp.StatusCode == HttpStatusCode.OK)
            {
                Directory.CreateDirectory(dirPath);

                if (fileptr)
                {
                    string filePath = dirPath + "\\" +
                                      file.Name;
                    string     srcFile = null;
                    FileStream reader;
                    size = ProcessFileSize(webResp, out srcFile);
                    m_currentFileSize = size;

                    if (srcFile != null)
                    {
                        reader = new FileStream(srcFile, FileMode.Open, FileAccess.Read);
                        writer = new FileStream(filePath,
                                                System.IO.FileMode.Create);

                        //   DownloadFile(srcFile, filePath);
                        fireEventFromBgw(Event.FileDownloadStarted);
                        m_currentFileProgress = 0;
                        while (m_currentFileProgress < size && !bgwDownloader.CancellationPending)
                        {
                            while (this.IsPaused)
                            {
                                System.Threading.Thread.Sleep(100);
                            }

                            speedTimer.Start();

                            currentPackageSize = reader.Read(readBytes, 0, this.PackageSize);

                            m_currentFileProgress += currentPackageSize;
                            m_totalProgress       += currentPackageSize;
                            fireEventFromBgw(Event.ProgressChanged);

                            writer.Write(readBytes, 0, currentPackageSize);
                            readings += 1;

                            if (readings >= this.StopWatchCyclesAmount)
                            {
                                m_currentSpeed = (Int32)(this.PackageSize * StopWatchCyclesAmount * 1000 / (speedTimer.ElapsedMilliseconds + 1));
                                speedTimer.Reset();
                                readings = 0;
                            }
                        }
                        reader.Close();
                        writer.Close();
                        speedTimer.Stop();
                        //end
                    }
                }
                else
                {
                    m_currentFileSize = size;
                    //string name;
                    if (file.IsCompressed)
                    {
                        file.Name = ProbeWithUnderscore(file.Name);
                    }
                    string filePath = dirPath + "\\" +
                                      file.Name;
                    writer = new FileStream(filePath,
                                            System.IO.FileMode.Create);

                    if (exc != null)
                    {
                        bgwDownloader.ReportProgress((Int32)InvokeType.FileDownloadFailedRaiser, exc);
                    }
                    else
                    {
                        m_currentFileProgress = 0;
                        while (m_currentFileProgress < size && !bgwDownloader.CancellationPending)
                        {
                            while (this.IsPaused)
                            {
                                System.Threading.Thread.Sleep(100);
                            }

                            speedTimer.Start();

                            currentPackageSize = webResp.GetResponseStream().Read(readBytes, 0, this.PackageSize);

                            m_currentFileProgress += currentPackageSize;
                            m_totalProgress       += currentPackageSize;
                            fireEventFromBgw(Event.ProgressChanged);

                            writer.Write(readBytes, 0, currentPackageSize);
                            readings += 1;

                            if (readings >= this.StopWatchCyclesAmount)
                            {
                                m_currentSpeed = (Int32)(this.PackageSize * StopWatchCyclesAmount * 1000 / (speedTimer.ElapsedMilliseconds + 1));
                                speedTimer.Reset();
                                readings = 0;
                            }
                        }

                        speedTimer.Stop();
                        writer.Close();

                        webResp.Close();
                        if (file.IsCompressed)
                        {
                            HandleCompression(filePath);
                        }
                    }
                    if (!bgwDownloader.CancellationPending)
                    {
                        fireEventFromBgw(Event.FileDownloadSucceeded);
                    }
                }
            }
            fireEventFromBgw(Event.FileDownloadStopped);
        }