Skip to content

Commit 0ef7941

Browse files
fix(zip): avoid throwing on empty file name (#828)
Co-authored-by: Dmitrii Makarov <[email protected]>
1 parent c19f0a4 commit 0ef7941

File tree

2 files changed

+26
-1
lines changed

2 files changed

+26
-1
lines changed

src/ICSharpCode.SharpZipLib/Core/PathUtils.cs

+4-1
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,9 @@ public static class PathUtils
1616
/// <returns>The path with the root removed if it was present; path otherwise.</returns>
1717
public static string DropPathRoot(string path)
1818
{
19+
// No need to drop anything
20+
if (path == string.Empty) return path;
21+
1922
var invalidChars = Path.GetInvalidPathChars();
2023
// If the first character after the root is a ':', .NET < 4.6.2 throws
2124
var cleanRootSep = path.Length >= 3 && path[1] == ':' && path[2] == ':';
@@ -26,7 +29,7 @@ public static string DropPathRoot(string path)
2629
var cleanPath = new string(path.Take(258)
2730
.Select( (c, i) => invalidChars.Contains(c) || (i == 2 && cleanRootSep) ? '_' : c).ToArray());
2831

29-
var stripLength = Path.GetPathRoot(cleanPath).Length;
32+
var stripLength = Path.GetPathRoot(cleanPath)?.Length ?? 0;
3033
while (path.Length > stripLength && (path[stripLength] == '/' || path[stripLength] == '\\')) stripLength++;
3134
return path.Substring(stripLength);
3235
}

test/ICSharpCode.SharpZipLib.Tests/Zip/ZipFileHandling.cs

+22
Original file line numberDiff line numberDiff line change
@@ -1815,5 +1815,27 @@ public void TestDescriptorUpdateOnAdd(UseZip64 useZip64)
18151815
}
18161816
}
18171817
}
1818+
1819+
/// <summary>
1820+
/// Check that Zip files can be created with an empty file name
1821+
/// </summary>
1822+
[Test]
1823+
[Category("Zip")]
1824+
public void HandlesEmptyFileName()
1825+
{
1826+
using var ms = new MemoryStream();
1827+
using (var zos = new ZipOutputStream(ms){IsStreamOwner = false})
1828+
{
1829+
zos.PutNextEntry(new ZipEntry(String.Empty));
1830+
Utils.WriteDummyData(zos, 64);
1831+
}
1832+
ms.Seek(0, SeekOrigin.Begin);
1833+
using (var zis = new ZipInputStream(ms){IsStreamOwner = false})
1834+
{
1835+
var entry = zis.GetNextEntry();
1836+
Assert.That(entry.Name, Is.Empty);
1837+
Assert.That(zis.ReadBytes(64).Length, Is.EqualTo(64));
1838+
}
1839+
}
18181840
}
18191841
}

0 commit comments

Comments
 (0)