Ejemplo n.º 1
0
        public virtual void BackupProgress(BackupProgressEventArgs e)
        {
            EventHandler <BackupProgressEventArgs> handler = OnBackupProgress;

            if (handler != null)
            {
                handler(this, e);
            }
        }
        private int SynchonizeLocalAndRemoteFile(int fileId, string localFile, string remoteFile, List <string> remoteHashes)
        {
            int byteCount = 0;
            int index     = 0;

            byte[] buffer    = new byte[_chunkSize];
            int    chunkSent = 0;
            string localHash = String.Empty;
            BackupProgressEventArgs progressEventArgs = new BackupProgressEventArgs(0);

            Logger.Write(30013, "Synchronizing " + localFile + " with " + remoteFile, Logger.MessageSeverity.Debug);

            if (!base.IsCancelRequired)
            {
                using (FileStream fileToRead = FileSystem.GetOutlookFile(localFile))
                {
                    using (Stream fileToWrite = File.OpenWrite(remoteFile))
                    {
                        do
                        {
                            if (!base.IsCancelRequired)
                            {
                                byteCount = fileToRead.Read(buffer, 0, _chunkSize);
                                localHash = GetHash(buffer);
                                if (index >= remoteHashes.Count || localHash != remoteHashes[index])
                                {
                                    fileToWrite.Position = (long)index * _chunkSize;
                                    fileToWrite.Write(buffer, 0, byteCount);
                                    chunkSent++;
                                    if (index >= remoteHashes.Count)
                                    {
                                        _clientDb.InsertHash(fileId, index, localHash);
                                    }
                                    else
                                    {
                                        _clientDb.UpdateHash(fileId, index, localHash);
                                    }
                                }
                                index++;
                                progressEventArgs.Percent = (int)(fileToRead.Position * 100 / fileToRead.Length);
                                BackupProgress(progressEventArgs);
                            }
                            else
                            {
                                break;
                            }
                        } while (fileToRead.Position < fileToRead.Length);
                    }
                }
            }
            Logger.Write(30014, "Synchronizing finish. " + chunkSent + " chunk(s) have been sent to the remote destination", Logger.MessageSeverity.Debug);
            return(chunkSent);
        }
Ejemplo n.º 3
0
        private void CopyFile(string sourceFilePath, string outputFilePath, bool isCompressed)
        {
            BackupProgressEventArgs progressEventArgs = new BackupProgressEventArgs(0);

            Logger.Write(30018, "Copying " + sourceFilePath + " to " + outputFilePath, Logger.MessageSeverity.Debug);

            using (Stream sourceFile = FileSystem.GetOutlookFile(sourceFilePath))
                using (Stream outputFile = new FileStream(outputFilePath, FileMode.Create, FileAccess.Write))
                {
                    int    bufferLength   = 1024 * 1024;
                    byte[] buffer         = new byte[bufferLength];
                    long   sourceLength   = sourceFile.Length;
                    double totalReadBytes = 0;
                    int    readBytes      = 0;
                    double percentage     = 0;
                    while ((readBytes = sourceFile.Read(buffer, 0, bufferLength)) > 0)
                    {
                        totalReadBytes += readBytes;
                        if (isCompressed)
                        {
                            percentage = 50.0 + totalReadBytes * 50.0 / sourceLength;
                        }
                        else
                        {
                            percentage = totalReadBytes * 100.0 / sourceLength;
                        }

                        outputFile.Write(buffer, 0, readBytes);

                        if (base.IsCancelRequired)
                        {
                            outputFile.Close();
                            (new System.IO.FileInfo(outputFilePath)).Directory.Delete(true);
                            throw new BackupCanceledException(sourceFilePath);
                        }
                        else
                        {
                            progressEventArgs.Percent = (int)percentage;
                            BackupProgress(progressEventArgs);
                        }
                    }
                }
            Logger.Write(30019, "Copy completed", Logger.MessageSeverity.Debug);
        }