Description
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