示例#1
0
        void Write(ISync local, SyncFolder lfolder, ISync remote, SyncFolder rfolder, string filterFolder, string filterFile, bool useMd5)
        {
            if (rfolder == null || lfolder == null)
            {
                return;
            }

            // Folder
            foreach (SyncFolder f in rfolder.Folders)
            {
                SyncFolder esta = null;
                foreach (SyncFolder f2 in lfolder.Folders)
                {
                    if (f2.Name == f.Name)
                    {
                        f2.Checked = true;
                        esta       = f2;
                        break;
                    }
                }

                if (!StringHelper.Like(filterFile, f.Name))
                {
                    continue;
                }

                try
                {
                    if (esta == null)
                    {
                        esta = new SyncFolder(lfolder)
                        {
                            Name = f.Name
                        };
                        local.CreateFolder(esta);
                        WriteInfo("Creating folder: " + esta.GetFullPath('/'));
                    }

                    // Esta, hacerlo recursivo
                    Write(local, esta, remote, f, filterFolder, filterFile, useMd5);
                }
                catch (Exception e)
                {
                    WriteError(e.Message);
                }
            }

            // Files
            foreach (SyncFile f in rfolder.Files)
            {
                SyncFile esta = null;
                foreach (SyncFile f2 in lfolder.Files)
                {
                    if (f2.Name == f.Name)
                    {
                        f2.Checked = true;
                        esta       = f2;
                        break;
                    }
                }

                if (!StringHelper.Like(filterFile, f.Name))
                {
                    continue;
                }

                try
                {
                    if (esta != null)
                    {
                        // Compare
                        if (f.Length == esta.Length)
                        {
                            // Same file
                            if (useMd5)
                            {
                                if (local.GetMd5(f) == remote.GetMd5(esta))
                                {
                                    continue;
                                }
                            }
                            else
                            {
                                continue;
                            }

                            // Delete
                            local.DeleteFile(esta);
                            WriteInfo("Deleting file: " + esta.GetFullPath('/'));
                        }
                    }

                    // Write
                    using (Stream stream = remote.GetFile(f))
                    {
                        SyncFile put = new SyncFile(lfolder)
                        {
                            Name = f.Name
                        };
                        local.WriteFile(put, stream);

                        WriteInfo("Writing file: " + put.GetFullPath('/'));
                    }
                }
                catch (Exception e)
                {
                    WriteError(e.Message);
                }
            }
        }