Esempio n. 1
0
        /// <summary>
        /// This just copies a stream to a file path with logging.  
        /// </summary>
        private void CopyStreamToFile(Stream fromStream, string fromUri, string fullDestPath, ISymbolNotification notification)
        {
            try
            {
                int total = 0;
                var dirName = Path.GetDirectoryName(fullDestPath);
                Directory.CreateDirectory(dirName);
                _log.WriteLine("Success Copying {0} to {1}", fromUri, fullDestPath);
                using (Stream toStream = File.Create(fullDestPath))
                {
                    byte[] buffer = new byte[8192];
                    int cnt = 0;
                    for (;;)
                    {
                        int count = fromStream.Read(buffer, 0, buffer.Length);
                        if (count == 0)
                            break;
                        toStream.Write(buffer, 0, count);

                        total += count;
                        notification.DownloadProgress(total);

                        _log.Write(".");
                        cnt++;
                        if (cnt > 40)
                        {
                            _log.WriteLine();
                            _log.Flush();
                            cnt = 0;
                        }
                        System.Threading.Thread.Sleep(0);       // allow interruption. 
                    }
                }

                notification.DownloadComplete(fullDestPath, fullDestPath[fullDestPath.Length - 1] == '_');
            }
            finally
            {
                fromStream.Close();
                _log.WriteLine();
            }
        }