public Option <FlowOneToMany> ExpandArchive(FilePath Src, FolderPath Dst, bool Overwrite = true) { try { Dst.CreateIfMissing().Require(); var paths = MutableList.Create <FilePath>(); using (var stream = new FileStream(Src, FileMode.Open)) { using (var archive = new ZipArchive(stream, ZipArchiveMode.Read)) { foreach (var entry in archive.Entries) { var dstPath = Dst.GetCombinedFilePath(RelativePath.Parse(entry.FullName)); if (dstPath.Exists() && not(Overwrite)) { dstPath = dstPath.UniqueName(); } else { dstPath.DeleteIfExists().Require(); } paths.Add(Expand(entry, dstPath).Target); } } } return(new FlowOneToMany(Src, paths.ToReadOnlyList())); } catch (Exception e) { return(none <FlowOneToMany>(e)); } }
/// <summary> /// Copies the represented file to a specified folder /// </summary> /// <param name="dstfolder">The target folder</param> /// <param name="overwrite"></param> /// <param name="createFolder">Specifies whether to create the folder if it doesn't exist</param> /// <returns></returns> public static FileCopyResult CopyTo(this IFilePath path, FolderPath dstfolder, bool overwrite = true, bool createFolder = true) { if (createFolder) { dstfolder.CreateIfMissing(); } var dstpath = dstfolder + path.FileName; try { var exists = path.Exists(); File.Copy(path.FileSystemPath, dstpath, overwrite); return(new FileCopyResult(path.FileSystemPath, dstpath, exists && overwrite)); } catch (Exception e) { return(new FileCopyResult(path.FileSystemPath, dstpath, e.Message)); } }
/// <summary> /// Copies a source folder into a target folder accoring to specified options /// </summary> /// <param name="SrcFolder">The folder that will be copied</param> /// <param name="DstFolder">The folder that will receive the copies</param> /// <param name="recursive">Whether to deep-copy</param> /// <param name="cloneFiles">Whether to copy files</param> /// <param name="fileMatch">Criteria filenames must satisfy to be copied</param> /// <returns></returns> public static IEnumerable <FileCopyResult> Clone(this FolderPath SrcFolder, FolderPath DstFolder, bool recursive = true, bool cloneFiles = false, string fileMatch = null) { DstFolder.CreateIfMissing().Require(); if (cloneFiles) { foreach (var x in SrcFolder.CopyFilesTo(DstFolder, fileMatch)) { yield return(x); } } if (recursive) { foreach (var srcSubfolder in SrcFolder.Subfolders()) { foreach (var x in srcSubfolder.Clone(DstFolder + srcSubfolder.FolderName, recursive, cloneFiles, fileMatch)) { yield return(x); } } } }
/// <summary> /// Creates a folder, overwriting anything that currently exists /// </summary> /// <returns></returns> public static Option <FolderPath> Recreate(this FolderPath folder) => Try(() => from x in folder.DeleteIfExists() from y in folder.CreateIfMissing() select y );