diff --git a/BasicNodes/Tools/SevenZip.cs b/BasicNodes/Tools/SevenZip.cs index cc55e66c..39d9d2a8 100644 --- a/BasicNodes/Tools/SevenZip.cs +++ b/BasicNodes/Tools/SevenZip.cs @@ -140,7 +140,7 @@ public class SevenZip : Node return -1; } isDir = true; - itemToCompress = Path.Combine(args.WorkingFile, "*"); + itemToCompress = FileHelper.Combine(args.WorkingFile, "*"); } else { @@ -225,7 +225,7 @@ public class SevenZip : Node string compressionMethod = CompressionMethod?.EmptyAsNull() ?? "lzma2"; - string targetFile = args.IsRemote ? Path.Combine(args.TempPath, Guid.NewGuid() + ".7zip") : destFile; + string targetFile = args.IsRemote ? FileHelper.Combine(args.TempPath, Guid.NewGuid() + ".7zip") : destFile; args.Execute(new() { diff --git a/BasicNodes/Tools/Zip.cs b/BasicNodes/Tools/Zip.cs index 8fd067f6..39a8f061 100644 --- a/BasicNodes/Tools/Zip.cs +++ b/BasicNodes/Tools/Zip.cs @@ -2,6 +2,7 @@ using FileFlows.Plugin.Attributes; using System.IO.Compression; using System.IO; +using FileFlows.Plugin.Helpers; namespace FileFlows.BasicNodes.File; @@ -65,11 +66,11 @@ public class Zip : Node try { - if (System.IO.Directory.Exists(args.WorkingFile)) + if (args.FileService.DirectoryExists(args.WorkingFile).Is(true)) { isDir = true; } - else if (System.IO.File.Exists(args.WorkingFile) == false) + else if (args.FileService.FileExists(args.WorkingFile).Is(true) == false) { args.Logger?.ELog("File or folder does not exist: " + args.WorkingFile); return -1; @@ -79,9 +80,9 @@ public class Zip : Node if (string.IsNullOrEmpty(destDir)) { if (isDir) - destDir = new DirectoryInfo(args.LibraryPath).FullName; + destDir = FileHelper.GetDirectory(args.LibraryPath); else - destDir = new FileInfo(args.FileName)?.DirectoryName ?? String.Empty; + destDir = FileHelper.GetDirectory(args.FileName); if (string.IsNullOrEmpty(destDir)) { args.Logger?.ELog("Failed to get destination directory"); @@ -90,81 +91,86 @@ public class Zip : Node } else { - // incase they set a linux path on windows or vice versa - destDir = destDir.Replace('\\', Path.DirectorySeparatorChar); - destDir = destDir.Replace('/', Path.DirectorySeparatorChar); + // in case they set a linux path on windows or vice versa + destDir = destDir.Replace('\\', args.FileService.PathSeparator); + destDir = destDir.Replace('/', args.FileService.PathSeparator); destDir = args.ReplaceVariables(destDir, stripMissing: true); // this converts it to the actual OS path - destDir = new FileInfo(destDir).DirectoryName!; - args.CreateDirectoryIfNotExists(destDir); + destDir = FileHelper.GetDirectory(destDir); + args.FileService.DirectoryCreate(destDir); } string destFile = args.ReplaceVariables(DestinationFile ?? string.Empty, true); if (string.IsNullOrEmpty(destFile)) { - if (isDir) - destFile = new DirectoryInfo(args.FileName).Name + ".zip"; - else - destFile = new FileInfo(args.FileName).Name + ".zip"; + destFile = FileHelper.GetShortFileName(args.FileName) + ".zip"; } if (destFile.ToLower().EndsWith(".zip") == false) destFile += ".zip"; - destFile = Path.Combine(destDir, destFile); + destFile = FileHelper.Combine(destDir, destFile); + + string tempZip = FileHelper.Combine(args.TempPath, Guid.NewGuid() + ".zip"); args.Logger?.ILog($"Compressing '{args.WorkingFile}' to '{destFile}'"); if (isDir) { + if (args.FileService.FileIsLocal(args.WorkingFile) == false) + { + args.Logger?.ELog("Cannot zip remote directories"); + return -1; + } var dir = new DirectoryInfo(args.WorkingFile); var files = dir.GetFiles("*.*", SearchOption.AllDirectories); - using (FileStream fs = new FileStream(destFile, FileMode.Create)) + using FileStream fs = new FileStream(tempZip, FileMode.Create); + using ZipArchive arch = new ZipArchive(fs, ZipArchiveMode.Create); + args?.PartPercentageUpdate(0); + float current = 0; + float count = files.Length; + foreach (var file in files) { - using (ZipArchive arch = new ZipArchive(fs, ZipArchiveMode.Create)) + ++count; + if (string.Equals(file.FullName, destFile, StringComparison.CurrentCultureIgnoreCase)) + continue; // cant zip the zip we are creating + string relative = file.FullName[(dir.FullName.Length + 1)..]; + try { - args?.PartPercentageUpdate(0); - float current= 0; - float count = files.Length; - foreach(var file in files) - { - ++count; - if (file.FullName.ToLower() == destFile.ToLower()) - continue; // cant zip the zip we are creating - string relative = file.FullName.Substring(dir.FullName.Length + 1); - try - { - arch.CreateEntryFromFile(file.FullName, relative, CompressionLevel.SmallestSize); - } - catch (Exception ex) - { - args.Logger?.WLog("Failed to add file to zip: " + file.FullName + " => " + ex.Message); - } - args?.PartPercentageUpdate((current / count) * 100); - } - args?.PartPercentageUpdate(100); + arch.CreateEntryFromFile(file.FullName, relative, CompressionLevel.SmallestSize); } + catch (Exception ex) + { + args.Logger?.WLog("Failed to add file to zip: " + file.FullName + " => " + ex.Message); + } + + args?.PartPercentageUpdate((current / count) * 100); } + + args?.PartPercentageUpdate(100); } else { - using (FileStream fs = new FileStream(destFile, FileMode.Create)) - { - using (ZipArchive arch = new ZipArchive(fs, ZipArchiveMode.Create)) - { - arch.CreateEntryFromFile(args.WorkingFile, new FileInfo(args.FileName).Name); - } - } + string localFile = args.FileService.GetLocalPath(args.WorkingFile); + using FileStream fs = new FileStream(tempZip, FileMode.Create); + using ZipArchive arch = new ZipArchive(fs, ZipArchiveMode.Create); + arch.CreateEntryFromFile(localFile, FileHelper.GetShortFileName(args.LibraryFileName)); } - if (System.IO.File.Exists(destFile)) + if (System.IO.File.Exists(tempZip) == false) { - args.SetWorkingFile(destFile); - args.Logger?.ILog("Zip created at: " + destFile); - return 1; + args.Logger?.ELog("Failed to create zip: " + destFile); + return -1; } - args.Logger?.ELog("Failed to create zip: " + destFile); - return -1; + if (args.FileService.FileMove(tempZip, destFile, true).Failed(out string error)) + { + args.Logger?.ELog("Failed to move zip: " + error); + return -1; + } + + args.SetWorkingFile(destFile); + args.Logger?.ILog("Zip created at: " + destFile); + return 1; } catch (Exception ex) { diff --git a/FileFlows.Plugin.dll b/FileFlows.Plugin.dll index eb851762..c58a5523 100644 Binary files a/FileFlows.Plugin.dll and b/FileFlows.Plugin.dll differ diff --git a/FileFlows.Plugin.pdb b/FileFlows.Plugin.pdb index b848ba7f..72fbf6af 100644 Binary files a/FileFlows.Plugin.pdb and b/FileFlows.Plugin.pdb differ