Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[API Proposal]: Provide a NormalizeEntrySeparator(Char, Char) method for ZipFileExtension. #113058

Open
XmmShp opened this issue Mar 3, 2025 · 3 comments
Assignees
Labels
api-suggestion Early API idea and discussion, it is NOT ready for implementation area-System.IO.Compression
Milestone

Comments

@XmmShp
Copy link

XmmShp commented Mar 3, 2025

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.

API Proposal

namespace System.IO.Compression;

public partial class ZipFileExtensions
{
    public static ZipArchive NormalizeEntrySeparator(this ZipArchive archive);
    public static ZipArchive NormalizeEntrySeparator(this ZipArchive archive, char newSeparator);
    public static ZipArchive NormalizeEntrySeparator(this ZipArchive archive, char originalSeparator, char newSeparator);
}

API Usage

using (var archive = ZipFile.Open(zipPath, ZipArchiveMode.Create))
{
    // Do something with archive
    archive.NormalizeEntrySeparators(); // Replace Path.DirectorySeparatorChar in the entry path with '/'.
    char separatorChar = '/';
    archive.NormalizeEntrySeparators(separatorChar); // Replace Path.DirectorySeparatorChar in the entry path with the separatorChar.
    char originSeparatorChar = '\';
    archive.NormalizeEntrySeparators(originSeparatorChar, separatorChar); // Replace the originSeparatorChar in the entry path with the separatorChar.
}

Alternative Designs

Perhaps we could also provide an Option when calling ZipFile.Open that allows users to specify their own separator, or offer an overload for functions like CreateEntry or CreateEntryFromFile that includes a separatorChar parameter. However, this might make things more complicated.

Risks

No response

@XmmShp XmmShp added the api-suggestion Early API idea and discussion, it is NOT ready for implementation label Mar 3, 2025
@dotnet-policy-service dotnet-policy-service bot added the untriaged New issue has not been triaged by the area owner label Mar 3, 2025
Copy link
Contributor

Tagging subscribers to this area: @dotnet/area-system-io-compression
See info in area-owners.md if you want to be subscribed.

@carlossanlop carlossanlop self-assigned this Mar 3, 2025
@carlossanlop carlossanlop removed the untriaged New issue has not been triaged by the area owner label Mar 3, 2025
@ericstj ericstj added this to the Future milestone Mar 7, 2025
@ericstj
Copy link
Member

ericstj commented Mar 7, 2025

@XmmShp -- have you tried doing this with existing APIs? This seems like a pretty special purpose API, we don't even have it on Path but similar problems exist there when folks are dealing with path strings that might need to work cross-platform. cc @dotnet/area-system-io.

If you can't do it today with existing APIs, then maybe you might want to consider making a more generalized API proposal like ZipArchiveEntry.Rename or similar?

@XmmShp
Copy link
Author

XmmShp commented Mar 8, 2025

@XmmShp -- have you tried doing this with existing APIs?

@ericstj I solved it locally with a simple string.Replace(Path.DirectorySeparatorChar, '/') method, but I believe a more directive solution should be provided for this issue. It might even be considered to set this as an Option for Open, because I think this problem could trouble many people using dotnet to manipulate Zip files across platforms.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
api-suggestion Early API idea and discussion, it is NOT ready for implementation area-System.IO.Compression
Projects
None yet
Development

No branches or pull requests

3 participants