Skip to content

Commit aefb01b

Browse files
authored
Fix leading slash in ZIP archive (#103)
* Fix leading slash in ZIP archive * Typo
1 parent fa1ac45 commit aefb01b

10 files changed

Lines changed: 215 additions & 38 deletions

File tree

src/Zio.Tests/FileSystems/TestZipArchiveFileSystem.cs

Lines changed: 50 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -231,8 +231,8 @@ public void TestOpenStreamsMultithreaded()
231231

232232

233233
[Theory]
234-
[InlineData("TestData/Linux.zip")]
235-
[InlineData("TestData/Windows.zip")]
234+
[InlineData("TestData/OsZips/Linux.zip")]
235+
[InlineData("TestData/OsZips/Windows.zip")]
236236
public void TestCaseInSensitiveZip(string path)
237237
{
238238
using var stream = File.OpenRead(path);
@@ -253,8 +253,8 @@ public void TestCaseInSensitiveZip(string path)
253253
}
254254

255255
[Theory]
256-
[InlineData("TestData/Linux.zip")]
257-
[InlineData("TestData/Windows.zip")]
256+
[InlineData("TestData/OsZips/Linux.zip")]
257+
[InlineData("TestData/OsZips/Windows.zip")]
258258
public void TestCaseSensitiveZip(string path)
259259
{
260260
using var stream = File.OpenRead(path);
@@ -329,4 +329,50 @@ public void TestSaveFile()
329329
File.Delete(path);
330330
}
331331
}
332+
333+
[Theory]
334+
[InlineData("TestData/RootFiles/WithoutRootSlash.zip", false)]
335+
[InlineData("TestData/RootFiles/WithRootSlash.zip", true)]
336+
public void TestReadDifferentSlash(string zipPath, bool leadingRootSlash)
337+
{
338+
using var fs = new ZipArchiveFileSystem(zipPath);
339+
340+
Assert.Equal(leadingRootSlash, fs.LeadingSlashInArchive);
341+
342+
Assert.True(fs.FileExists("/Test.txt"));
343+
Assert.Equal("Test", fs.ReadAllText("/Test.txt"));
344+
}
345+
346+
[Theory]
347+
[InlineData(false)]
348+
[InlineData(true)]
349+
public void TestWriteDifferentSlash(bool leadingRootSlash)
350+
{
351+
using var memStream = new MemoryStream();
352+
353+
using (var fs = new ZipArchiveFileSystem(memStream, leaveOpen: true))
354+
{
355+
fs.LeadingSlashInArchive = leadingRootSlash;
356+
357+
Assert.Equal(leadingRootSlash, fs.LeadingSlashInArchive);
358+
359+
fs.WriteAllText("/Test.txt", "Test");
360+
}
361+
362+
memStream.Seek(0, SeekOrigin.Begin);
363+
364+
using (var fs = new ZipArchiveFileSystem(memStream, leaveOpen: true))
365+
{
366+
Assert.Equal(leadingRootSlash, fs.LeadingSlashInArchive);
367+
368+
Assert.True(fs.FileExists("/Test.txt"));
369+
Assert.Equal("Test", fs.ReadAllText("/Test.txt"));
370+
}
371+
372+
memStream.Seek(0, SeekOrigin.Begin);
373+
374+
using var zipArchive = new ZipArchive(memStream, ZipArchiveMode.Read);
375+
376+
Assert.Equal(leadingRootSlash, zipArchive.Entries[0].FullName.StartsWith("/"));
377+
}
332378
}

src/Zio.Tests/FileSystems/TestZipArchiveFileSystemCompact.cs

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -91,6 +91,43 @@ public void TestDirectoryExceptions()
9191
#endif
9292
}
9393

94+
[Fact]
95+
public void TestDirectoryDelete()
96+
{
97+
fs.CreateDirectory("/dir");
98+
fs.WriteAllText("/dir/file.txt", "test");
99+
100+
fs.CreateDirectory("/dir2");
101+
fs.WriteAllText("/dir2/file2.txt", "test");
102+
103+
fs.DeleteDirectory("/dir", isRecursive: true);
104+
105+
Assert.False(fs.DirectoryExists("/dir"));
106+
107+
Assert.True(fs.DirectoryExists("/dir2"));
108+
Assert.True(fs.FileExists("/dir2/file2.txt"));
109+
}
110+
111+
[Fact]
112+
public void TestDirectoryMove()
113+
{
114+
fs.CreateDirectory("/dir");
115+
fs.WriteAllText("/dir/file.txt", "test");
116+
117+
fs.CreateDirectory("/dir2");
118+
fs.WriteAllText("/dir2/file2.txt", "test");
119+
120+
fs.MoveDirectory("/dir", "/moved");
121+
122+
Assert.False(fs.DirectoryExists("/dir"));
123+
124+
Assert.True(fs.DirectoryExists("/moved"));
125+
Assert.True(fs.FileExists("/moved/file.txt"));
126+
127+
Assert.True(fs.DirectoryExists("/dir2"));
128+
Assert.True(fs.FileExists("/dir2/file2.txt"));
129+
}
130+
94131
[Fact]
95132
public void TestFile()
96133
{
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
These ZIP files contains the following structure:
2+
Folder/
3+
File.txt [Content: Test]
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
These ZIP files contain a single file.
2+
3+
In WithRootSlash, the file is contained in the root folder (/Test.txt).
4+
This was the behavior in Zio 0.21.
5+
6+
In WithoutRootSlash, the file is contained directly in the ZIP file (Test.txt).
7+
This is the current behavior.
122 Bytes
Binary file not shown.
120 Bytes
Binary file not shown.

src/Zio.Tests/Zio.Tests.csproj

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22

33
<PropertyGroup>
44
<TargetFrameworks>net8.0;net9.0</TargetFrameworks>
5+
<TargetFrameworks Condition="'$(OS)' == 'Windows_NT'">$(TargetFrameworks);net472</TargetFrameworks>
56
<IsPackable>false</IsPackable>
67
<LangVersion>10</LangVersion>
78
</PropertyGroup>

0 commit comments

Comments
 (0)