remove-office-protection/Program.cs
2024-02-27 23:09:12 +01:00

132 lines
4.3 KiB
C#

// 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 = @"<w:documentProtection ([A-z0-9 ="":+\/]*)>";
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");
}
}