2019-05-29 01:33:42 +02:00
|
|
|
|
using System;
|
|
|
|
|
using System.Collections.Generic;
|
|
|
|
|
using System.Linq;
|
|
|
|
|
using System.Text;
|
2020-03-17 21:31:43 +01:00
|
|
|
|
using System.Threading;
|
2019-07-16 23:35:21 +02:00
|
|
|
|
using Toolbox.Library.Forms;
|
2019-05-29 01:33:42 +02:00
|
|
|
|
using System.Windows.Forms;
|
|
|
|
|
using System.IO;
|
2019-07-16 23:35:21 +02:00
|
|
|
|
using Toolbox.Library.IO;
|
2019-05-29 01:33:42 +02:00
|
|
|
|
|
2019-07-16 23:35:21 +02:00
|
|
|
|
namespace Toolbox.Library
|
2019-05-29 01:33:42 +02:00
|
|
|
|
{
|
|
|
|
|
public class TreeHelper
|
|
|
|
|
{
|
2019-07-21 02:56:22 +02:00
|
|
|
|
public static void CreateFileDirectory(TreeNode root)
|
|
|
|
|
{
|
|
|
|
|
List<TreeNode> treeNodes = new List<TreeNode>();
|
|
|
|
|
foreach (TreeNode n in root.Nodes)
|
|
|
|
|
treeNodes.Add(n);
|
|
|
|
|
|
|
|
|
|
var rootText = root.Text;
|
|
|
|
|
var rootTextLength = rootText.Length;
|
|
|
|
|
root.Nodes.Clear();
|
|
|
|
|
|
|
|
|
|
int I = 0;
|
|
|
|
|
foreach (TreeNode node in treeNodes)
|
|
|
|
|
{
|
|
|
|
|
string nodeString = node.Text;
|
|
|
|
|
|
|
|
|
|
var roots = nodeString.Split(new char[] { '/' },
|
|
|
|
|
StringSplitOptions.RemoveEmptyEntries);
|
|
|
|
|
|
|
|
|
|
// The initial parent is the root node
|
|
|
|
|
var parentNode = root;
|
|
|
|
|
var sb = new System.Text.StringBuilder(rootText, nodeString.Length + rootTextLength);
|
|
|
|
|
for (int rootIndex = 0; rootIndex < roots.Length; rootIndex++)
|
|
|
|
|
{
|
|
|
|
|
// Build the node name
|
|
|
|
|
var parentName = roots[rootIndex];
|
|
|
|
|
sb.Append("/");
|
|
|
|
|
sb.Append(parentName);
|
|
|
|
|
var nodeName = sb.ToString();
|
|
|
|
|
|
|
|
|
|
// Search for the node
|
|
|
|
|
var index = parentNode.Nodes.IndexOfKey(nodeName);
|
|
|
|
|
if (index == -1)
|
|
|
|
|
{
|
|
|
|
|
// Node was not found, add it
|
|
|
|
|
var folder = new TreeNode(parentName);
|
|
|
|
|
if (rootIndex == roots.Length - 1)
|
|
|
|
|
{
|
|
|
|
|
node.Text = parentName;
|
|
|
|
|
parentNode.Nodes.Add(node);
|
|
|
|
|
parentNode = node;
|
|
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
{
|
|
|
|
|
folder.Name = nodeName;
|
|
|
|
|
parentNode.Nodes.Add(folder);
|
|
|
|
|
parentNode = folder;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
{
|
|
|
|
|
// Node was found, set that as parent and continue
|
|
|
|
|
parentNode = parentNode.Nodes[index];
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
2019-08-14 22:36:19 +02:00
|
|
|
|
public static string[] ExtractAllFiles(string ParentPath, TreeNodeCollection Nodes, string overridePath = "")
|
2019-05-29 01:33:42 +02:00
|
|
|
|
{
|
2019-08-14 22:36:19 +02:00
|
|
|
|
List<string> filesExtracted = new List<string>();
|
|
|
|
|
|
|
|
|
|
if (overridePath == string.Empty)
|
2019-05-29 01:33:42 +02:00
|
|
|
|
{
|
2019-08-14 22:36:19 +02:00
|
|
|
|
FolderSelectDialog folderDialog = new FolderSelectDialog();
|
|
|
|
|
if (folderDialog.ShowDialog() != DialogResult.OK)
|
|
|
|
|
return new string[0];
|
|
|
|
|
|
|
|
|
|
overridePath = folderDialog.SelectedPath;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
STProgressBar progressBar = new STProgressBar();
|
|
|
|
|
progressBar.Task = "Extracing Files...";
|
|
|
|
|
progressBar.Refresh();
|
|
|
|
|
progressBar.Value = 0;
|
|
|
|
|
progressBar.StartPosition = FormStartPosition.CenterScreen;
|
|
|
|
|
progressBar.Show();
|
2019-05-29 01:33:42 +02:00
|
|
|
|
|
2020-03-17 21:31:43 +01:00
|
|
|
|
Thread Thread = new Thread((ThreadStart)(() =>
|
2019-08-14 22:36:19 +02:00
|
|
|
|
{
|
2020-03-17 21:31:43 +01:00
|
|
|
|
var Collection = TreeViewExtensions.Collect(Nodes).ToList();
|
|
|
|
|
Console.WriteLine($"Collection {Collection.Count}");
|
2019-12-12 01:08:16 +01:00
|
|
|
|
|
2020-03-17 21:31:43 +01:00
|
|
|
|
int Curfile = 0;
|
|
|
|
|
foreach (TreeNode node in Collection)
|
2019-05-29 01:33:42 +02:00
|
|
|
|
{
|
2020-03-17 21:31:43 +01:00
|
|
|
|
if (progressBar.IsDisposed || progressBar.Disposing) {
|
|
|
|
|
break;
|
|
|
|
|
}
|
2019-06-17 02:34:04 +02:00
|
|
|
|
|
2020-03-17 21:31:43 +01:00
|
|
|
|
ArchiveFileInfo file = null;
|
2019-05-29 01:33:42 +02:00
|
|
|
|
|
2020-03-17 21:31:43 +01:00
|
|
|
|
if (node.Tag != null && node.Tag is ArchiveFileInfo)
|
|
|
|
|
file = (ArchiveFileInfo)node.Tag;
|
|
|
|
|
else if (node is ArchiveFileWrapper)
|
|
|
|
|
file = ((ArchiveFileWrapper)node).ArchiveFileInfo;
|
2019-05-29 01:33:42 +02:00
|
|
|
|
|
2020-03-17 21:31:43 +01:00
|
|
|
|
if (file != null)
|
|
|
|
|
{
|
|
|
|
|
string FilePath = file.FileName;
|
|
|
|
|
string FolderPath = Path.GetDirectoryName(FilePath.RemoveIllegaleFolderNameCharacters());
|
|
|
|
|
string FolderPathDir = Path.Combine(overridePath, FolderPath);
|
2019-05-29 01:33:42 +02:00
|
|
|
|
|
2020-03-17 21:31:43 +01:00
|
|
|
|
if (!Directory.Exists(FolderPathDir))
|
|
|
|
|
Directory.CreateDirectory(FolderPathDir);
|
2019-05-29 01:33:42 +02:00
|
|
|
|
|
2020-03-17 21:31:43 +01:00
|
|
|
|
string FileName = Path.GetFileName(file.FileName).RemoveIllegaleFileNameCharacters();
|
2019-05-29 01:33:42 +02:00
|
|
|
|
|
2020-03-17 21:31:43 +01:00
|
|
|
|
FilePath = Path.Combine(FolderPath, FileName);
|
2019-05-29 01:33:42 +02:00
|
|
|
|
|
2020-03-17 21:31:43 +01:00
|
|
|
|
if (ParentPath != string.Empty)
|
|
|
|
|
FilePath = FilePath.Replace(ParentPath, string.Empty);
|
2019-08-14 22:36:19 +02:00
|
|
|
|
|
2020-03-17 21:31:43 +01:00
|
|
|
|
var path = $"{overridePath}/{FilePath}";
|
2019-12-12 01:08:16 +01:00
|
|
|
|
|
2020-03-17 21:31:43 +01:00
|
|
|
|
if (progressBar.InvokeRequired)
|
|
|
|
|
{
|
|
|
|
|
progressBar.Invoke((MethodInvoker)delegate {
|
|
|
|
|
// Running on the UI thread
|
|
|
|
|
progressBar.Task = $"Extracting File {FileName}";
|
|
|
|
|
progressBar.Value = (Curfile++ * 100) / Collection.Count;
|
|
|
|
|
progressBar.Refresh();
|
|
|
|
|
});
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
CreateDirectoryIfExists($"{path}");
|
|
|
|
|
|
|
|
|
|
filesExtracted.Add($"{path}");
|
|
|
|
|
|
|
|
|
|
if (file.FileFormat != null && file.FileFormat.CanSave)
|
|
|
|
|
file.SaveFileFormat();
|
|
|
|
|
|
|
|
|
|
if (file.FileDataStream != null)
|
|
|
|
|
file.FileDataStream.ExportToFile(path);
|
|
|
|
|
else
|
|
|
|
|
File.WriteAllBytes($"{path}", file.FileData);
|
|
|
|
|
}
|
2019-05-29 01:33:42 +02:00
|
|
|
|
}
|
2019-08-14 22:36:19 +02:00
|
|
|
|
|
2020-03-17 21:31:43 +01:00
|
|
|
|
if (progressBar.InvokeRequired)
|
|
|
|
|
{
|
|
|
|
|
progressBar.Invoke((MethodInvoker)delegate {
|
|
|
|
|
progressBar.Value = 100;
|
|
|
|
|
progressBar.Refresh();
|
|
|
|
|
progressBar.Close();
|
|
|
|
|
});
|
|
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
{
|
|
|
|
|
progressBar.Value = 100;
|
|
|
|
|
progressBar.Refresh();
|
|
|
|
|
progressBar.Close();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
}));
|
|
|
|
|
Thread.Start();
|
2019-08-14 22:36:19 +02:00
|
|
|
|
|
|
|
|
|
return filesExtracted.ToArray();
|
2019-05-29 01:33:42 +02:00
|
|
|
|
}
|
|
|
|
|
|
2019-12-10 22:59:40 +01:00
|
|
|
|
/// <summary>
|
|
|
|
|
/// S
|
|
|
|
|
/// </summary>
|
|
|
|
|
/// <param name="directory"></param>
|
|
|
|
|
/// <returns></returns>
|
2019-07-17 22:14:55 +02:00
|
|
|
|
public static List<Tuple<string, string>> ReadFiles(string directory)
|
|
|
|
|
{
|
|
|
|
|
var Files = new List<Tuple<string, string>>();
|
2019-12-10 22:59:40 +01:00
|
|
|
|
ProcessDirectory(directory, "", Files);
|
2019-07-17 22:14:55 +02:00
|
|
|
|
return Files;
|
|
|
|
|
}
|
|
|
|
|
|
2019-12-10 22:59:40 +01:00
|
|
|
|
private static void ProcessDirectory(string targetDirectory, string directory,
|
|
|
|
|
List<Tuple<string, string>> FileList, string seperator = "/")
|
2019-07-17 22:14:55 +02:00
|
|
|
|
{
|
2019-12-10 22:59:40 +01:00
|
|
|
|
//Combine the target and current directory to get the full path for searching
|
|
|
|
|
string folder = Path.Combine(targetDirectory, directory);
|
2019-07-17 22:14:55 +02:00
|
|
|
|
|
2019-12-10 22:59:40 +01:00
|
|
|
|
//Search sub directories. Remove target directory from path
|
|
|
|
|
foreach (string dir in Directory.GetDirectories(folder))
|
|
|
|
|
ProcessDirectory(targetDirectory, dir.Replace($"{targetDirectory}\\", string.Empty), FileList);
|
2019-07-17 22:14:55 +02:00
|
|
|
|
|
2019-12-10 22:59:40 +01:00
|
|
|
|
//Search files. Remove target directory from path
|
|
|
|
|
foreach (string file in Directory.GetFiles(folder))
|
|
|
|
|
FileList.Add(Tuple.Create($"{file.Replace($"{targetDirectory}\\", string.Empty)}".Replace("\\", seperator), file));
|
2019-07-17 22:14:55 +02:00
|
|
|
|
}
|
|
|
|
|
|
2019-05-29 01:33:42 +02:00
|
|
|
|
private static void CreateDirectoryIfExists(string Dir)
|
|
|
|
|
{
|
|
|
|
|
if (!String.IsNullOrWhiteSpace(Path.GetDirectoryName(Dir)))
|
|
|
|
|
{
|
|
|
|
|
//Make sure no file names use the same name to prevent errors
|
|
|
|
|
if (!File.Exists(Dir))
|
|
|
|
|
{
|
|
|
|
|
if (!Directory.Exists(Dir))
|
|
|
|
|
{
|
|
|
|
|
Directory.CreateDirectory(Path.GetDirectoryName(Dir));
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
2019-05-29 20:24:24 +02:00
|
|
|
|
|
2021-12-28 21:02:38 +01:00
|
|
|
|
public static string GetFolderAbsoultePath(TreeNode node, ArchiveRootNodeWrapper rootNode)
|
|
|
|
|
{
|
|
|
|
|
string nodePath = node.FullPath;
|
|
|
|
|
int startIndex = nodePath.IndexOf(rootNode.Text);
|
|
|
|
|
if (startIndex > 0)
|
|
|
|
|
nodePath = nodePath.Substring(startIndex);
|
|
|
|
|
|
|
|
|
|
string slash = Path.DirectorySeparatorChar.ToString();
|
|
|
|
|
string slashAlt = Path.AltDirectorySeparatorChar.ToString();
|
|
|
|
|
return nodePath.Replace(rootNode.Text + slash, string.Empty).Replace(slash ?? "", slashAlt);
|
|
|
|
|
}
|
2019-05-29 20:24:24 +02:00
|
|
|
|
|
2019-07-22 21:06:49 +02:00
|
|
|
|
public static void AddFiles(TreeNode parentNode, ArchiveRootNodeWrapper rootNode, string[] Files)
|
2019-05-29 20:24:24 +02:00
|
|
|
|
{
|
2019-07-22 21:06:49 +02:00
|
|
|
|
var archiveFile = rootNode.ArchiveFile;
|
|
|
|
|
|
2019-05-29 20:24:24 +02:00
|
|
|
|
if (Files == null || Files.Length <= 0 || !archiveFile.CanAddFiles) return;
|
|
|
|
|
|
|
|
|
|
for (int i = 0; i < Files.Length; i++)
|
|
|
|
|
{
|
2019-07-22 21:06:49 +02:00
|
|
|
|
var File = ArchiveFileWrapper.FromPath(Files[i], rootNode);
|
2019-05-29 20:24:24 +02:00
|
|
|
|
string FileName = Path.GetFileName(Files[i]);
|
|
|
|
|
|
|
|
|
|
//Don't add the root file name
|
2019-07-19 01:19:16 +02:00
|
|
|
|
if (parentNode.FullPath != string.Empty && !(parentNode is ArchiveRootNodeWrapper))
|
2019-06-10 02:16:11 +02:00
|
|
|
|
{
|
2019-07-27 03:30:51 +02:00
|
|
|
|
string nodePath = parentNode.FullPath;
|
|
|
|
|
int startIndex = nodePath.IndexOf(rootNode.Text);
|
|
|
|
|
if (startIndex > 0)
|
|
|
|
|
nodePath = nodePath.Substring(startIndex);
|
|
|
|
|
|
|
|
|
|
string slash = Path.DirectorySeparatorChar.ToString();
|
|
|
|
|
string slashAlt = Path.AltDirectorySeparatorChar.ToString();
|
|
|
|
|
string SetPath = nodePath.Replace(rootNode.Text + slash, string.Empty).Replace(slash ?? "", slashAlt);
|
|
|
|
|
|
|
|
|
|
string FullPath = Path.Combine(SetPath, FileName).Replace(slash ?? "", slashAlt);
|
2019-07-19 01:19:16 +02:00
|
|
|
|
File.ArchiveFileInfo.FileName = FullPath;
|
2019-06-10 02:16:11 +02:00
|
|
|
|
}
|
2019-05-29 20:24:24 +02:00
|
|
|
|
else
|
|
|
|
|
File.ArchiveFileInfo.FileName = FileName;
|
|
|
|
|
|
2019-07-19 01:19:16 +02:00
|
|
|
|
|
2019-05-29 20:24:24 +02:00
|
|
|
|
bool HasAddedFile = archiveFile.AddFile(File.ArchiveFileInfo);
|
|
|
|
|
if (HasAddedFile)
|
2019-07-19 02:02:37 +02:00
|
|
|
|
{
|
2019-08-14 21:27:45 +02:00
|
|
|
|
//Re apply the newly added archive info
|
|
|
|
|
File.ArchiveFileInfo = archiveFile.Files.LastOrDefault();
|
|
|
|
|
|
2019-07-27 03:30:51 +02:00
|
|
|
|
parentNode.Nodes.Add(File);
|
|
|
|
|
|
2019-07-19 02:02:37 +02:00
|
|
|
|
if (parentNode is ArchiveRootNodeWrapper)
|
2019-07-22 21:06:49 +02:00
|
|
|
|
((ArchiveRootNodeWrapper)parentNode).AddFileNode(File);
|
2019-07-19 02:02:37 +02:00
|
|
|
|
if (parentNode is ArchiveFolderNodeWrapper)
|
2019-07-26 22:44:22 +02:00
|
|
|
|
((ArchiveFolderNodeWrapper)parentNode).RootNode.AddFileNode(File);
|
2019-07-19 02:02:37 +02:00
|
|
|
|
}
|
2019-05-29 20:24:24 +02:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public static void RemoveFile(ArchiveFileWrapper fileNode, IArchiveFile archiveFile)
|
|
|
|
|
{
|
|
|
|
|
if (!archiveFile.CanDeleteFiles) return;
|
|
|
|
|
|
|
|
|
|
var parentNode = fileNode.Parent;
|
|
|
|
|
|
|
|
|
|
bool HasRemovedFile = archiveFile.DeleteFile(fileNode.ArchiveFileInfo);
|
|
|
|
|
|
|
|
|
|
if (HasRemovedFile)
|
|
|
|
|
parentNode.Nodes.Remove(fileNode);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public static void RemoveFolder(TreeNode folderNode, IArchiveFile archiveFile)
|
|
|
|
|
{
|
|
|
|
|
if (!archiveFile.CanDeleteFiles) return;
|
|
|
|
|
|
|
|
|
|
foreach (var node in TreeViewExtensions.Collect(folderNode.Nodes))
|
|
|
|
|
{
|
|
|
|
|
if (node is ArchiveFileWrapper)
|
|
|
|
|
archiveFile.DeleteFile(((ArchiveFileWrapper)node).ArchiveFileInfo);
|
|
|
|
|
}
|
2019-07-26 22:44:22 +02:00
|
|
|
|
|
|
|
|
|
var parentNode = folderNode.Parent;
|
|
|
|
|
parentNode.Nodes.Remove(folderNode);
|
2019-05-29 20:24:24 +02:00
|
|
|
|
}
|
2019-05-29 01:33:42 +02:00
|
|
|
|
}
|
|
|
|
|
}
|