OFFICIUM INSERVIO IT
Your reliable partner for your business software...
ZIP-Packen und Entpacken / ZIP compressing and extraction
.net Framework 4.8.x
Mittwoch, 3. Juni 2026
Deutsch
Hintergrund
Sehr häufig möchte man im klassischen .net-Framework 4.8-Umfeld , z.B. innerhalb von Anpassungen für Sage ERP-Systeme wie die "Sage 100" , vollständige(!) Verzeichnisse komprimiert speichern und dann wieder entpacken.
Code-Beispiel
Die Umsetzung ist auch im klassischen .net-Framework 4.8 ohne(!) zusätzliche NuGet-Pakete möglich.
Man muss dem Projekt lediglich eine Referenz auf die .net-Bibliothek "System.IO.Compression.FileSystem.dll" hinzufügen , die von Microsoft im Rahmen des .net-Frameworks standardmäßig ausgerollt wird.
Für die Verwendung von Code wie "System.IO.Compression.ZipFile.OpenRead" ist auch ein Verweis auf die DLL "System.IO.Compression.dll" nötig.
var sourceDir = @"C:\Test\MeinOrdner\";
var zipFile = @"C:\Test\Backup.zip";
// Entscheidend ist der Parameter "includeBaseDirectory" !
// Dieser stellt sicher , dass nur die Dateien im Ordner und alle Unterordner inkl. deren Dateien gesichert werden.
System.IO.Compression.ZipFile.CreateFromDirectory( sourceDir, zipFile , System.IO.Compression.CompressionLevel.Optimal , includeBaseDirectory: false );
// ... Div. Aktionen.
// ... Z.B. lokaler Ordner geleert oder gelöscht und die Daten sollen wieder hergestellt werden!
// Bitte in die MSDN-Dokumentation der Entpacken-Funktion schauen , um sich mit den Details vertraut zu machen.
// Nachfolgender Code sucht z.B. nach einer bestimmten Datei in der ZIP. Für eine direkte Suche kann man auch "archive.GetEntry(exactRelativePath)" verwenden.
// Das folgende Iterieren fragt alle(!) Elemente in der ZIP ab!
using ( System.IO.Compression.ZipArchive archive = System.IO.Compression.ZipFile.OpenRead( zipFile ) )
{
foreach ( System.IO.Compression.ZipArchiveEntry entry in archive.Entries )
{
if ( string.Equals( entry.Name, "test.pdf", System.StringComparison.OrdinalIgnoreCase ) )
{
// Found the file!
}
}
}
// Folgender Code entpackt alles wieder zurück! System.IO.Compression.ZipFile.ExtractToDirectory( zipFile , sourceDir );Besonderheiten
Angemerkt werden sollte , dass es einige mögliche "Stolperfallen" geben kann.
Dateizeitstempel:
Der Zeitpunkt der letzten Änderung (LastWriteTime) bleibt im ZIP-Format in der Regel erhalten, jedoch werden Erstellungszeit (CreationTime) und Zeitpunkt des letzten Zugriffs (LastAccessTime) der extrahierten Dateien auf den Zeitpunkt der Extraktion zurückgesetzt.
NTFS-Berechtigungen:
ZIP-Dateien speichern keine Windows-Zugriffskontrolllisten (ACLs). Die extrahierten Dateien übernehmen die Standard-Sicherheitsberechtigungen des Zielverzeichnisses.
Erweiterte Dateiattribute:
Spezielle Kennzeichnungen wie "Schreibgeschützt" , "Versteckt" oder "System" gehen beim Komprimieren und Extrahieren in der Regel verloren.
English
Background
Quite frequently in the classic .NET Framework 4.8 environment, for example within customisations for Sage ERP systems such as "Sage 100", one wants to save entire directories in a compressed format and extract the data again later.
Code Example
The implementation is also possible in the classic .NET Framework 4.8 without(!) any additional NuGet packages.
One must only add a reference to the "System.IO.Compression.FileSystem.dll" assembly to use the necessary methods.
For using code like "System.IO.Compression.ZipFile.OpenRead" one also needs to add a reference to the DLL "System.IO.Compression.dll".
var sourceDir = @"C:\Test\MyFolder";
var zipFile = @"C:\Test\Backup.zip";
// The decisive parameter is "includeBaseDirectory"!
// This ensures that only the files in the folder and all subfolders including their files are backed up.
System.IO.Compression.ZipFile.CreateFromDirectory( sourceDir, zipFile, System.IO.Compression.CompressionLevel.Optimal, includeBaseDirectory: false );
// ... Various actions.
// ... E.g., local folder emptied or deleted and the data needs to be restored!
// Note the behaviour of the extraction method for details - see MSDN documentation.
// E.g., the following code looks for a specific file within the ZIP. For a direct search one can use "archive.GetEntry(exactRelativePath)", also.
// The following iteration queries all(!) files in the ZIP!
using ( System.IO.Compression.ZipArchive archive = System.IO.Compression.ZipFile.OpenRead( zipFile ) )
{
foreach ( System.IO.Compression.ZipArchiveEntry entry in archive.Entries )
{
if ( string.Equals( entry.Name, "test.pdf", System.StringComparison.OrdinalIgnoreCase ) )
{
// Found the file!
}
}
}
// The following code extracts everything back!
System.IO.Compression.ZipFile.ExtractToDirectory( zipFile, sourceDir );Specifics
It should be noted that there can be a few potential pitfalls.
File Timestamps:
The time of the last modification (LastWriteTime) is usually preserved in the ZIP format, however, the creation time (CreationTime) and the time of the last access (LastAccessTime) of the extracted files are reset to the time of extraction.
NTFS Permissions:
ZIP files do not store Windows Access Control Lists (ACLs). The extracted files inherit the default security permissions of the target directory.
Extended File Attributes
Special flags such as "Read-only", "Hidden", or "System" are generally lost during compression and extraction.
