From 77e6e6802b6a15eb9d2853539f3aef45446349b1 Mon Sep 17 00:00:00 2001 From: ljaentsch Date: Tue, 27 Feb 2024 23:09:12 +0100 Subject: [PATCH] beta-release-1.0.0 --- Program.cs | 132 ++++++++++++++++++++++++++++++++++++++++++++++++ removePW.csproj | 10 ++++ removePW.sln | 25 +++++++++ 3 files changed, 167 insertions(+) create mode 100644 Program.cs create mode 100644 removePW.csproj create mode 100644 removePW.sln diff --git a/Program.cs b/Program.cs new file mode 100644 index 0000000..a920543 --- /dev/null +++ b/Program.cs @@ -0,0 +1,132 @@ +// See https://aka.ms/new-console-template for more information +using System.Collections; +using System.Collections.Generic; +using System.IO.Compression; +using System.Text.RegularExpressions; +using System.Xml; + + + +internal class Program +{ + static string[] ExtensionList = { + // WORD + "docm", + "docx", + "dotm", + "dotx", + // EXCEL + "xlsm", + "xltx", + "xltm", + "xlsx", + // POWERPOINT + "potm", + "potx", + "pptm", + "pptx" + }; + private static void Main(string[] args) + { + //args = [".\\bin\\Debug\\net8.0\\Testx.docx"]; + //args.Append(".\\bin\\Debug\\net8.0\\Testx.docx"); + foreach (String a in args) + { + Console.WriteLine(a); + switch (a) + { + case "--help": + + break; + default: + bool extFound = false; + foreach (String ext in ExtensionList) + { + if (a.Contains("." + ext)) + { + extFound = true; + break; + } + } + if (extFound) + { + removePW(a); + } + break; + } + //Console.WriteLine(a); + } + Console.ReadLine(); + + } + private static void removePW(string FilePath) + { + string Path = AppContext.BaseDirectory; + Console.WriteLine(Path); + + Directory.CreateDirectory(Path + "\\temp"); + + string openPath = FilePath; + string extractPath = Path + "\\temp\\tmp_file.xml"; + + string filepath_woe = FilePath.Substring(0, FilePath.Length - 5); + string filepath_ext = FilePath.Substring(FilePath.Length - 5); + string TMP_FILE = filepath_woe + "_noProtection" + filepath_ext; + + string SettingsFileName = ""; + + using (FileStream zipToOpen = new FileStream(openPath, FileMode.Open)) + { + using (ZipArchive archive = new ZipArchive(zipToOpen, ZipArchiveMode.Read, false)) + { + foreach (ZipArchiveEntry entry in archive.Entries.Where(e => e.FullName.Contains("settings.xml"))) + { + + if (File.Exists(extractPath)) + { + File.Delete(extractPath); + } + SettingsFileName = entry.FullName; + entry.ExtractToFile(extractPath); + + } + } + } + + string text = File.ReadAllText(extractPath); + + string pattern = @""; + string substitution = @""; + RegexOptions options = RegexOptions.Multiline; + + Regex regex = new Regex(pattern, options); + string result = regex.Replace(text, substitution); + + File.WriteAllText(Path + "\\temp\\settings.xml", result); + + File.Copy(openPath, TMP_FILE); + using (FileStream zipToOpen = new FileStream(TMP_FILE, FileMode.Open)) + { + using (ZipArchive archive = new ZipArchive(zipToOpen, ZipArchiveMode.Update, false)) + { + ZipArchiveEntry tmp = null; + foreach (ZipArchiveEntry entry in archive.Entries.Where(e => e.FullName.Contains("settings.xml"))) + { + tmp = entry; + } + tmp.Delete(); + //ZipArchiveEntry readmeEntry = archive.CreateEntry("word/settings.xml", CompressionLevel.Fastest); + ZipArchiveEntry readmeEntry = archive.CreateEntry(SettingsFileName, CompressionLevel.Fastest); + using (StreamWriter writer = new StreamWriter(readmeEntry.Open())) + { + writer.Write(result); + } + } + } + File.Delete(extractPath); + File.Delete(Path + "\\temp\\settings.xml"); + Console.WriteLine("Doc unprotected | Dateibearbeitungspasswortschutz entfernt"); + Console.WriteLine("New File | Neue Datei: "+TMP_FILE); + Console.WriteLine("press Enter to close | Eingabetaste drücken zum Schließen"); + } +} \ No newline at end of file diff --git a/removePW.csproj b/removePW.csproj new file mode 100644 index 0000000..206b89a --- /dev/null +++ b/removePW.csproj @@ -0,0 +1,10 @@ + + + + Exe + net8.0 + enable + enable + + + diff --git a/removePW.sln b/removePW.sln new file mode 100644 index 0000000..cc8c8d6 --- /dev/null +++ b/removePW.sln @@ -0,0 +1,25 @@ + +Microsoft Visual Studio Solution File, Format Version 12.00 +# Visual Studio Version 17 +VisualStudioVersion = 17.5.002.0 +MinimumVisualStudioVersion = 10.0.40219.1 +Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "removePW", "removePW.csproj", "{8211AE5B-96EA-4494-9C6A-2040FDA13F43}" +EndProject +Global + GlobalSection(SolutionConfigurationPlatforms) = preSolution + Debug|Any CPU = Debug|Any CPU + Release|Any CPU = Release|Any CPU + EndGlobalSection + GlobalSection(ProjectConfigurationPlatforms) = postSolution + {8211AE5B-96EA-4494-9C6A-2040FDA13F43}.Debug|Any CPU.ActiveCfg = Debug|Any CPU + {8211AE5B-96EA-4494-9C6A-2040FDA13F43}.Debug|Any CPU.Build.0 = Debug|Any CPU + {8211AE5B-96EA-4494-9C6A-2040FDA13F43}.Release|Any CPU.ActiveCfg = Release|Any CPU + {8211AE5B-96EA-4494-9C6A-2040FDA13F43}.Release|Any CPU.Build.0 = Release|Any CPU + EndGlobalSection + GlobalSection(SolutionProperties) = preSolution + HideSolutionNode = FALSE + EndGlobalSection + GlobalSection(ExtensibilityGlobals) = postSolution + SolutionGuid = {B8EA5858-ED93-46BC-AE1A-AB9A1D9C5C2D} + EndGlobalSection +EndGlobal