mirror of
https://github.com/revenz/FileFlowsPlugins.git
synced 2026-02-14 07:18:27 -06:00
FF-1001 - unpack node
This commit is contained in:
@@ -29,6 +29,10 @@
|
||||
<PackageReference Include="MSTest.TestFramework" Version="3.0.4" />
|
||||
</ItemGroup>
|
||||
|
||||
<ItemGroup>
|
||||
<PackageReference Include="SharpCompress" Version="0.33.0" />
|
||||
</ItemGroup>
|
||||
|
||||
<ItemGroup>
|
||||
<Content Include="BasicNodes.en.json">
|
||||
<CopyToOutputDirectory>Always</CopyToOutputDirectory>
|
||||
|
||||
@@ -323,6 +323,18 @@
|
||||
"Body-Help": "The body of the request being sent. Variables can be used in this field."
|
||||
}
|
||||
},
|
||||
"Unpack": {
|
||||
"Description": "Allows you to unpack an archive (zip, rar, tar, etc)",
|
||||
"Outputs": {
|
||||
"1": "File Unpacked"
|
||||
},
|
||||
"Fields": {
|
||||
"DestinationPath": "Destination Folder",
|
||||
"DestinationPath-Help": "The destination folder where to unpack the file.",
|
||||
"File": "File",
|
||||
"File-Help": "The name of the file to unpack. Can be left blank and if so the current working file will be used."
|
||||
}
|
||||
},
|
||||
"Unzip": {
|
||||
"Description": "Allows you to unzip a file",
|
||||
"Outputs": {
|
||||
|
||||
122
BasicNodes/Tools/Unpack.cs
Normal file
122
BasicNodes/Tools/Unpack.cs
Normal file
@@ -0,0 +1,122 @@
|
||||
using System.Diagnostics;
|
||||
using FileFlows.Plugin;
|
||||
using FileFlows.Plugin.Attributes;
|
||||
using System.IO.Compression;
|
||||
using FileFlows.BasicNodes;
|
||||
using SharpCompress.Archives;
|
||||
|
||||
namespace BasicNodes.Tools;
|
||||
|
||||
/// <summary>
|
||||
/// Node used to unpack files from archives
|
||||
/// </summary>
|
||||
public class Unpack: Node
|
||||
{
|
||||
public override int Inputs => 1;
|
||||
public override int Outputs => 1;
|
||||
public override FlowElementType Type => FlowElementType.Process;
|
||||
public override string Icon => "fas fa-file-archive";
|
||||
/// <summary>
|
||||
/// Gets the Help URL for this element
|
||||
/// </summary>
|
||||
public override string HelpUrl => "https://fileflows.com/docs/plugins/basic-nodes/unpack";
|
||||
|
||||
private string _DestinationPath = string.Empty;
|
||||
private string _file = string.Empty;
|
||||
|
||||
[Folder(1)]
|
||||
public string DestinationPath
|
||||
{
|
||||
get => _DestinationPath;
|
||||
set { _DestinationPath = value ?? ""; }
|
||||
}
|
||||
|
||||
[TextVariable(2)]
|
||||
public string File
|
||||
{
|
||||
get => _file;
|
||||
set { _file = value ?? ""; }
|
||||
}
|
||||
|
||||
public override int Execute(NodeParameters args)
|
||||
{
|
||||
try
|
||||
{
|
||||
var filename = args.ReplaceVariables(File ?? string.Empty, stripMissing: true)?.EmptyAsNull() ?? args.WorkingFile;
|
||||
|
||||
var fileInfo = new FileInfo(filename);
|
||||
|
||||
if (fileInfo.Exists == false)
|
||||
{
|
||||
args.Logger?.ELog("File does not exist: " + filename);
|
||||
return -1;
|
||||
}
|
||||
|
||||
string destDir = args.ReplaceVariables(DestinationPath, stripMissing: true, cleanSpecialCharacters: true);
|
||||
|
||||
if (fileInfo.Extension.ToLower() == ".zip")
|
||||
ZipFile.ExtractToDirectory(filename, destDir, true);
|
||||
else
|
||||
Extract(args, args.WorkingFile, destDir);
|
||||
|
||||
return 1;
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
args.Logger?.ELog("Failed unzip: " + ex.Message + Environment.NewLine + ex.StackTrace);
|
||||
return -1;
|
||||
}
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Unpacks a file
|
||||
/// </summary>
|
||||
/// <param name="args">the node parameters</param>
|
||||
/// <param name="workingFile">the file to extract</param>
|
||||
/// <param name="destinationPath">the location to extract to</param>
|
||||
private void Extract(NodeParameters args, string workingFile, string destinationPath)
|
||||
{
|
||||
bool isRar = workingFile.ToLowerInvariant().EndsWith(".cbr");
|
||||
try
|
||||
{
|
||||
ArchiveFactory.WriteToDirectory(workingFile, destinationPath);
|
||||
}
|
||||
catch (Exception ex) when (isRar && ex.Message.Contains("Unknown Rar Header"))
|
||||
{
|
||||
UnrarCommandLineExtract(args, workingFile, destinationPath);
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
/// <summary>
|
||||
/// Unpacks a folder
|
||||
/// </summary>
|
||||
/// <param name="args">the node parameters</param>
|
||||
/// <param name="workingFile">the file to extract</param>
|
||||
/// <param name="destinationPath">the location to extract to</param>
|
||||
void UnrarCommandLineExtract(NodeParameters args, string workingFile, string destinationPath)
|
||||
{
|
||||
var process = new Process();
|
||||
string unrar = args.GetToolPath("unrar")?.EmptyAsNull() ?? "unrar";
|
||||
process.StartInfo.FileName = unrar;
|
||||
process.StartInfo.ArgumentList.Add("x");
|
||||
process.StartInfo.ArgumentList.Add("-o+");
|
||||
process.StartInfo.ArgumentList.Add(workingFile);
|
||||
process.StartInfo.ArgumentList.Add(destinationPath);
|
||||
process.StartInfo.UseShellExecute = false;
|
||||
process.StartInfo.RedirectStandardOutput = true;
|
||||
process.StartInfo.RedirectStandardError = true;
|
||||
process.StartInfo.CreateNoWindow = true;
|
||||
process.Start();
|
||||
string output = process.StandardError.ReadToEnd();
|
||||
string error = process.StandardError.ReadToEnd();
|
||||
process.WaitForExit();
|
||||
|
||||
args.Logger?.ILog("Unrar output:\n" + output);
|
||||
if (string.IsNullOrWhiteSpace(error) == false)
|
||||
args.Logger?.ELog("Unrar error:\n" + error);
|
||||
|
||||
if (process.ExitCode != 0)
|
||||
throw new Exception(error?.EmptyAsNull() ?? "Failed to extract rar file");
|
||||
}
|
||||
}
|
||||
@@ -1,18 +1,29 @@
|
||||
using FileFlows.Plugin;
|
||||
using BasicNodes.Tools;
|
||||
using FileFlows.Plugin;
|
||||
using FileFlows.Plugin.Attributes;
|
||||
using System.IO.Compression;
|
||||
|
||||
namespace FileFlows.BasicNodes.File;
|
||||
|
||||
/// <summary>
|
||||
/// Node that unzips a file
|
||||
/// </summary>
|
||||
public class Unzip : Node
|
||||
public class Unzip : Node
|
||||
{
|
||||
/// <summary>
|
||||
/// Gets that this node is obsolete
|
||||
/// </summary>
|
||||
public override bool Obsolete => true;
|
||||
/// <summary>
|
||||
/// Gets the obsolete message
|
||||
/// </summary>
|
||||
public override string ObsoleteMessage => "This has been replaced with the Unpack node.\n\nUse that instead.";
|
||||
public override int Inputs => 1;
|
||||
public override int Outputs => 1;
|
||||
public override FlowElementType Type => FlowElementType.Process;
|
||||
public override string Icon => "fas fa-file-archive";
|
||||
/// <summary>
|
||||
/// Gets the Help URL for this element
|
||||
/// </summary>
|
||||
public override string HelpUrl => "https://fileflows.com/docs/plugins/basic-nodes/unzip";
|
||||
|
||||
private string _DestinationPath = string.Empty;
|
||||
@@ -32,27 +43,16 @@ public class Unzip : Node
|
||||
set { _zip = value ?? ""; }
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Executes the node
|
||||
/// </summary>
|
||||
/// <param name="args">the arguments</param>
|
||||
/// <returns>the output</returns>
|
||||
public override int Execute(NodeParameters args)
|
||||
{
|
||||
try
|
||||
{
|
||||
var zip = args.ReplaceVariables(Zip ?? string.Empty, stripMissing: true)?.EmptyAsNull() ?? args.WorkingFile;
|
||||
|
||||
if (System.IO.File.Exists(zip) == false)
|
||||
{
|
||||
args.Logger?.ELog("File does not exist: " + zip);
|
||||
return -1;
|
||||
}
|
||||
|
||||
string destDir = args.ReplaceVariables(DestinationPath, stripMissing: true, cleanSpecialCharacters: true);
|
||||
|
||||
ZipFile.ExtractToDirectory(zip, destDir, true);
|
||||
return 1;
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
args.Logger?.ELog("Failed unzip: " + ex.Message + Environment.NewLine + ex.StackTrace);
|
||||
return -1;
|
||||
}
|
||||
var unpack = new Unpack();
|
||||
unpack.File = Zip;
|
||||
unpack.DestinationPath = DestinationPath;
|
||||
return unpack.Execute(args);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -10,7 +10,7 @@ internal class GenericExtractor
|
||||
/// <summary>
|
||||
/// Uncompresses a folder
|
||||
/// </summary>
|
||||
/// <param name="args">the node paratemers</param>
|
||||
/// <param name="args">the node parameters</param>
|
||||
/// <param name="workingFile">the file to extract</param>
|
||||
/// <param name="destinationPath">the location to extract to</param>
|
||||
/// <param name="halfProgress">if the NodeParameter.PartPercentageUpdate should end at 50%</param>
|
||||
|
||||
Reference in New Issue
Block a user