Skip to content

Commit aa85c22

Browse files
committed
Fix handling of paths on Linux and Mac (issue #54)
Linux and Mac use a forward slash instead of a backslash to separate directories. This change makes use of the Path.DirectorySeparatorChar instead of hardcoding backslash. This allows to use the same code on all platforms. This change also adjusts the WindowsNameTransform tests so that they also work cross-platform.
1 parent 0da21ba commit aa85c22

2 files changed

Lines changed: 14 additions & 14 deletions

File tree

src/Zip/WindowsNameTransform.cs

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -100,7 +100,7 @@ public string TransformDirectory(string name)
100100
{
101101
name = TransformFile(name);
102102
if (name.Length > 0) {
103-
while ( name.EndsWith(@"\") ) {
103+
while ( name.EndsWith(Path.DirectorySeparatorChar.ToString()) ) {
104104
name = name.Remove(name.Length - 1, 1);
105105
}
106106
}
@@ -186,23 +186,23 @@ public static string MakeValidName(string name, char replacement)
186186
throw new ArgumentNullException("name");
187187
}
188188

189-
name = WindowsPathUtils.DropPathRoot(name.Replace("/", @"\"));
189+
name = WindowsPathUtils.DropPathRoot(name.Replace("/", Path.DirectorySeparatorChar.ToString()));
190190

191191
// Drop any leading slashes.
192-
while ( (name.Length > 0) && (name[0] == '\\')) {
192+
while ( (name.Length > 0) && (name[0] == Path.DirectorySeparatorChar)) {
193193
name = name.Remove(0, 1);
194194
}
195195

196196
// Drop any trailing slashes.
197-
while ( (name.Length > 0) && (name[name.Length - 1] == '\\')) {
197+
while ( (name.Length > 0) && (name[name.Length - 1] == Path.DirectorySeparatorChar)) {
198198
name = name.Remove(name.Length - 1, 1);
199199
}
200200

201201
// Convert consecutive \\ characters to \
202-
int index = name.IndexOf(@"\\");
202+
int index = name.IndexOf(string.Format("{0}{0}", Path.DirectorySeparatorChar));
203203
while (index >= 0) {
204204
name = name.Remove(index, 1);
205-
index = name.IndexOf(@"\\");
205+
index = name.IndexOf(Path.DirectorySeparatorChar);
206206
}
207207

208208
// Convert any invalid characters using the replacement one.
@@ -245,7 +245,7 @@ public char Replacement
245245
}
246246
}
247247

248-
if ((value == '\\') || (value == '/')) {
248+
if ((value == Path.DirectorySeparatorChar) || (value == Path.AltDirectorySeparatorChar)) {
249249
throw new ArgumentException("invalid replacement character");
250250
}
251251

tests/Zip/ZipTests.cs

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1057,10 +1057,10 @@ public void BasicFiles()
10571057
wnt.TrimIncomingPaths = false;
10581058

10591059
TestFile(wnt, "Bogan", "Bogan");
1060-
TestFile(wnt, "absolute/file2", @"absolute\file2");
1061-
TestFile(wnt, "C:/base/////////t", @"base\t");
1062-
TestFile(wnt, "//unc/share/zebidi/and/dylan", @"zebidi\and\dylan");
1063-
TestFile(wnt, @"\\unc\share\/zebidi\/and\/dylan", @"zebidi\and\dylan");
1060+
TestFile(wnt, "absolute/file2", Path.Combine("absolute", "file2"));
1061+
TestFile(wnt, "C:/base/////////t", Path.Combine("base", "t"));
1062+
TestFile(wnt, "//unc/share/zebidi/and/dylan", Path.Combine("zebidi", "and", "dylan"));
1063+
TestFile(wnt, @"\\unc\share\/zebidi\/and\/dylan", Path.Combine("zebidi", "and", "dylan"));
10641064
}
10651065

10661066
[Test]
@@ -1070,7 +1070,7 @@ public void Replacement()
10701070
wnt.TrimIncomingPaths = false;
10711071

10721072
TestFile(wnt, "c::", "_");
1073-
TestFile(wnt, "c\\/>", @"c\_");
1073+
TestFile(wnt, "c\\/>", Path.Combine("c", "_"));
10741074
}
10751075

10761076
[Test]
@@ -1150,12 +1150,12 @@ public void BasicDirectories()
11501150
TestDirectory(wnt, "d", "d");
11511151
TestDirectory(wnt, "absolute/file2", @"absolute\file2");
11521152

1153-
const string BaseDir1 = @"C:\Dir";
1153+
string BaseDir1 = Path.Combine("C:", "Dir");
11541154
wnt.BaseDirectory = BaseDir1;
11551155

11561156
TestDirectory(wnt, "talofa", Path.Combine(BaseDir1 , "talofa"));
11571157

1158-
const string BaseDir2 = @"C:\Dir\";
1158+
string BaseDir2 = string.Format(@"C:{0}Dir{0}", Path.DirectorySeparatorChar);
11591159
wnt.BaseDirectory = BaseDir2;
11601160

11611161
TestDirectory(wnt, "talofa", Path.Combine(BaseDir2, "talofa"));

0 commit comments

Comments
 (0)