/// <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); } } }
/// <summary> /// Method to copy the contents of the ZephyrFile into another ZephyrFile. /// It works by using the base "Stream" property and "Create"methods each implementation must create. /// Thus, the ZephyrFiles do not have to be of the same implementation type. /// </summary> /// <param name="file">The destination ZephyrFile.</param> /// <param name="overwrite">Should the ZephyrFile overwrite existing ZephyrFile if it exists.</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(ZephyrFile file, bool overwrite = true, bool createMissingDirectories = true, bool stopOnError = true, bool verbose = true, String callbackLabel = null, Action <string, string> callback = null) { try { if (!this.Exists) { throw new Exception($"File [{this.FullName}] Does Not Exist."); } if (file.Exists && !overwrite) { throw new Exception($"File [{file.FullName}] Already Exists."); } String targetDirectory = file.FullName.Substring(0, file.FullName.LastIndexOf(file.Name)); ZephyrDirectory targetDir = file.CreateDirectory(targetDirectory, verbose); if (!targetDir.Exists) { if (createMissingDirectories) { targetDir.Create(verbose: verbose); } else { throw new Exception($"Directory [{targetDir.FullName}] Does Not Exist."); } } Stream source = this.Open(AccessType.Read, verbose); Stream target = file.Open(AccessType.Write, verbose); source.CopyTo(target); this.Close(verbose); file.Close(verbose); if (verbose) { Logger.Log($"Copied File [{this.FullName}] to [{file.FullName}].", callbackLabel, callback); } } catch (Exception e) { Logger.Log($"ERROR - {e.Message}", callbackLabel, callback); if (stopOnError) { throw; } } }
/// <summary> /// Static method to create a ZephyrDirectory whose implementation is based on the Fullname / URL passed in. /// </summary> /// <param name="dirName">The Fullname or URL of the directory.</param> /// <param name="clients">A collection of connection clients.</param> /// <param name="failIfExists">Throws an error if the directory already exists.</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> /// <returns>A ZephyrDirectory instance.</returns> public static ZephyrDirectory CreateDirectory(string dirName, Clients clients = null, bool failIfExists = false, bool verbose = true, String callbackLabel = null, Action <string, string> callback = null) { ZephyrDirectory dir = Utilities.GetZephyrDirectory(dirName, clients); return(dir.Create(failIfExists, verbose, callbackLabel, callback)); }