Recursive AI

Skip ZIP symbolic links when extracting in C#

OneArchive is a free, open-source archive and compression library for .NET from Recursive AI: zip, tar, 7z, RAR and more in RecursiveAI.OneArchive, with compression codecs in RecursiveAI.OneCompress. MIT or Apache-2.0, no native dependencies.

Use LinkPolicy.Skip to extract files without creating symbolic links. upload.zip is in the download.

Terminal
dotnet add package RecursiveAI.OneArchive

Requires .NET 10 (net10.0); .NET 9 and earlier aren’t supported.

C#
using RecursiveAI.OneArchive;
 
var result = Archive.Extract("upload.zip", "out",
    new ArchiveExtractOptions { Symlinks = LinkPolicy.Skip });
Console.WriteLine($"Skipped: {result.EntriesSkipped}");

The example is the full program below: unzip it and dotnet run. To run only the code above, paste it into Program.cs of a new dotnet new console project and dotnet run.

Text
Skipped: 1

The full program

The sample’s notes.txt contains Weekend notes; shortcut is a symbolic link pointing to notes.txt. The program extracts into a fresh temporary folder, checks that the link wasn’t created, and deletes the folder before exiting. If you paste the snippet above, start with an unused out path instead.

C#
using RecursiveAI.OneArchive;
 
var options = new ArchiveExtractOptions
{
    Symlinks = LinkPolicy.Skip
};
var output = Directory.CreateTempSubdirectory();
try
{
    var result = Archive.Extract("upload.zip", output.FullName, options);
    Console.WriteLine($"Extracted: {result.EntriesExtracted}");
    Console.WriteLine($"Skipped: {result.EntriesSkipped}");
    Console.WriteLine(File.ReadAllText(Path.Combine(output.FullName, "notes.txt")).TrimEnd());
    var shortcut = new FileInfo(Path.Combine(output.FullName, "shortcut"));
    Console.WriteLine($"Shortcut link exists: {shortcut.LinkTarget is not null}");
}
finally
{
    output.Delete(recursive: true);
}
Text
Extracted: 1
Skipped: 1
Weekend notes
Shortcut link exists: False

The same Symlinks = LinkPolicy.Skip setting applies to any archive format OneArchive reads symbolic-link entries from, including ZIP and TAR.

Skipping symbolic links doesn’t turn off the other extraction rules: a disallowed path can still raise ArchiveSafetyException, and files extracted earlier in the same run remain on disk unless you clean them up.

Variations

Inspect links without writing files. This lists the link’s name and target from the same sample ZIP:

C#
using RecursiveAI.OneArchive;
 
using var zip = Archive.Open("upload.zip");
foreach (var entry in zip.EnumerateEntries())
    if (entry.Kind == ArchiveEntryKind.Symlink)
        Console.WriteLine($"{entry.Name} -> {entry.LinkTarget}");
Text
shortcut -> notes.txt

The short answer

Set ArchiveExtractOptions.Symlinks to LinkPolicy.Skip and pass the options to Archive.Extract. OneArchive skips recognized symbolic-link entries instead of creating them, even when their targets are relative paths inside the destination. The regular file in the sample still gets extracted. Keep the separate write-through check enabled and use a fresh destination so you aren’t relying on what happens to be there already.

Yes. The sample includes one regular file and one ZIP entry marked as a Unix symbolic link. The link entry stores notes.txt as its target, not a second copy of Weekend notes. OneArchive exposes that entry as ArchiveEntryKind.Symlink, with the target in LinkTarget. Listing the ZIP lets you see the difference without creating a link on disk. The skip option acts on the entry kind; a regular file with the same name would still be a regular file.

Skipping the link does not skip or delete its target. In the example the target file exists because its own entry was extracted. If an archive contains only a link and no corresponding file entry, skipping the link does not fetch the target or create a replacement file. See the ZIP entry notes for how link entries are represented.

Separate two decisions: should extraction create link entries, and may it write through a link already in the destination path? Symlinks = LinkPolicy.Skip controls the first. The default write-through rule controls the second: an entry written under a symbolic-link path component is refused, including when that link was already on disk. Keep AllowWriteThroughSymlinks off. Skipping entries does not remove links that were already there.

If your application needs to preserve links, the default policy isn’t “skip all.” It checks link targets and throws with rule link-escape on an escaping link. Pick the skip policy when regular files are all your application needs, and keep the rest of the archive safety defaults in place. For an extraction that fails, discard the fresh destination rather than processing a partial result as a complete upload.

All onearchive recipes