Ejemplo n.º 1
0
        /// <summary>
        /// Method to copy the contents of the ZephyrDirectory into another ZephyrDirectory.
        /// It works by using the base "Stream" property and "Create" methods each implementation must create.
        /// Thus, the ZephyrDirectories do not have to be of the same implementation type.
        /// </summary>
        /// <param name="target">The destination ZephyrDirectory.</param>
        /// <param name="recurse">Copy all sub-directories and all files under this directory.</param>
        /// <param name="overwrite">Should files copied overwrite existing files of the same name.  Directories will be merged.</param>
        /// <param name="stopOnError">Stop copying when an error is encountered.</param>
        /// <param name="verbose">Log each file and directory copied.</param>
        /// <param name="callbackLabel">Optional "label" to be passed into the callback method.</param>
        /// <param name="callback">Optional method that is called for logging purposes.</param>
        public void CopyTo(ZephyrDirectory target, bool recurse = true, bool overwrite = true, bool stopOnError = true, bool verbose = true, String callbackLabel = null, Action <string, string> callback = null)
        {
            if (this.Exists)
            {
                foreach (ZephyrDirectory childDir in GetDirectories())
                {
                    try
                    {
                        String          targetChildDirName = target.PathCombine(target.FullName, $"{childDir.Name}/");
                        ZephyrDirectory targetChild        = target.CreateDirectory(targetChildDirName);
                        targetChild.Create(verbose: verbose);
                        if (recurse)
                        {
                            childDir.CopyTo(targetChild, recurse, overwrite, verbose, stopOnError, callbackLabel, callback);
                        }
                    }
                    catch (Exception e)
                    {
                        Logger.Log(e.Message, callbackLabel, callback);
                        if (stopOnError)
                        {
                            throw;
                        }
                    }
                }

                foreach (ZephyrFile file in GetFiles())
                {
                    try
                    {
                        String     targetFileName = target.PathCombine(target.FullName, file.Name);
                        ZephyrFile targetFile     = target.CreateFile(targetFileName, verbose, callbackLabel, callback);
                        file.CopyTo(targetFile, overwrite, true, stopOnError, verbose, callbackLabel, callback);
                    }
                    catch (Exception e)
                    {
                        Logger.Log(e.Message, callbackLabel, callback);
                        if (stopOnError)
                        {
                            throw;
                        }
                    }
                }

                if (verbose)
                {
                    Logger.Log($"Copied Directory [{this.FullName}] to [{target.FullName}].", callbackLabel, callback);
                }
            }
            else
            {
                string message = $"[{this.FullName}] Does Not Exist.";
                Logger.Log(message, callbackLabel, callback);
                if (stopOnError)
                {
                    throw new Exception(message);
                }
            }
        }
Ejemplo n.º 2
0
        /// <summary>
        /// Copies a ZephyrFile into a ZephyrDirectory with the same name as the original ZephyrFile.
        /// </summary>
        /// <param name="dir">The destination directory.</param>
        /// <param name="overwrite">Should the ZephyrFile overwrite existing ZephyrFile if it exists.</param>
        /// <param name="createMissingDirectories">Create any missing directories in the path to the file.</param>
        /// <param name="stopOnError">Throw an exception if the copy fails.</param>
        /// <param name="verbose">Log details of the file being copied.</param>
        /// <param name="callbackLabel">Optional "label" to be passed into the callback method.</param>
        /// <param name="callback">Optional method that is called for logging purposes.</param>
        public void CopyTo(ZephyrDirectory dir, bool overwrite = true, bool createMissingDirectories = true, bool stopOnError = true, bool verbose = true, String callbackLabel = null, Action <string, string> callback = null)
        {
            String     targetFilePath = dir.PathCombine(dir.FullName, this.Name);
            ZephyrFile targetFile     = dir.CreateFile(targetFilePath);

            CopyTo(targetFile, overwrite, createMissingDirectories, stopOnError, false, callbackLabel, callback);
            if (verbose)
            {
                Logger.Log($"Copied File [{this.FullName}] to [{dir.FullName}].", callbackLabel, callback);
            }
        }