Skip to content

[API Proposal]: Introduce PathNormalizationOption in ZipFile.Open for Cross-Platform Zip Entry Path Compatibility #113058

Open
@XmmShp

Description

@XmmShp

Background and motivation

For zip files, ZipFile allows the creation of archives with '\' as the filename separator. However, in practice, entry paths are usually provided using a method similar to the following:

var zipEntryPath = Path.GetRelativePath(folderPath, filePath);

On Windows platforms, this results in paths separated by '\'. When the resulting .zip archive is moved to a Unix platform and extracted using ZipFile, issues arise, as mentioned in issue #98247 .

I believe it would be beneficial to provide an API to normalize the path separators in the entries of the archive to ensure compatibility across different operating systems.


Currently, when developers use ZipFile to create compressed files, inconsistencies in path separators across different operating systems can lead to issues. Specifically, .zip files created on Windows may use backslashes (\) as path separators, while Unix-based systems expect forward slashes (/). This discrepancy can cause errors when moving and extracting .zip files across platforms.

Although this issue can be resolved by manually replacing string separators, it is far from ideal. Ideally, there should be a direct and straightforward way to ensure consistent path separators, improving both user experience and code maintainability.

API Proposal

namespace System.IO.Compression;

public enum PathNormalizationOption
{
    None = 0, // No normalization of path separators.
    NormalizeToForwardSlash = 1, // Converts all directory separators to '/'.
}

public class ZipFile
{
    public static ZipArchive Open(string path, ZipArchiveMode mode, PathNormalizationOption option = PathNormalizationOption.None);
}

API Usage

using (var archive = ZipFile.Open(zipPath, ZipArchiveMode.Create, PathNormalizationOption.NormalizeToForwardSlash))
{
    // Do something with archive
}

With this approach, developers can easily choose whether to normalize path separators without manually performing string replacement operations.

Alternative Designs

Another possible design would be to add a boolean parameter to methods like CreateEntryFromFile to specify whether path separators should be normalized. However, this approach is less intuitive and centralized compared to setting the option directly when opening the archive.

Risks

The risk associated with this change is minimal, as it only involves adding a new enumeration type and an additional parameter to an existing method. Moreover, it does not affect existing behavior by default unless the path normalization option is explicitly specified.

Metadata

Metadata

Assignees

Labels

Type

No type

Projects

No projects

Relationships

None yet

Development

No branches or pull requests

Issue actions