Skip to content

Commit ff940f5

Browse files
author
Meerow
authored
feat: new ZipFiles class
feat: new ZipFiles class
2 parents 3033c0a + f774967 commit ff940f5

File tree

6 files changed

+143
-6
lines changed

6 files changed

+143
-6
lines changed

src/Yaapii.Zip/ZipFiles.cs

Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
using System;
2+
using System.Collections;
3+
using System.Collections.Generic;
4+
using System.IO;
5+
using System.IO.Compression;
6+
using System.Text;
7+
using Yaapii.Atoms;
8+
using Yaapii.Atoms.Enumerable;
9+
using Yaapii.Atoms.Scalar;
10+
11+
namespace Yaapii.Zip
12+
{
13+
/// <summary>
14+
/// The files in a ZIP archive.
15+
/// Note: Extraction is sticky.
16+
/// </summary>
17+
public sealed class ZipFiles : IEnumerable<string>
18+
{
19+
private readonly IScalar<IEnumerable<string>> files;
20+
21+
/// <summary>
22+
/// The files in a ZIP archive.
23+
/// Note: Extraction is sticky.
24+
/// </summary>
25+
/// <param name="input"></param>
26+
public ZipFiles(IInput input, bool leaveOpen = true)
27+
{
28+
this.files =
29+
new SolidScalar<IEnumerable<string>>(()=>
30+
new Filtered<string>(path =>
31+
!path.EndsWith("/"),
32+
new ZipPaths(input)
33+
)
34+
);
35+
}
36+
37+
public IEnumerator<string> GetEnumerator()
38+
{
39+
return this.files.Value().GetEnumerator();
40+
}
41+
42+
IEnumerator IEnumerable.GetEnumerator()
43+
{
44+
return GetEnumerator();
45+
}
46+
}
47+
}

src/Yaapii.Zip/ZipPaths.cs

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -9,15 +9,15 @@
99
namespace Yaapii.Zip
1010
{
1111
/// <summary>
12-
/// The files in a ZIP archive.
12+
/// The paths in a ZIP archive.
1313
/// Note: Extraction is sticky.
1414
/// </summary>
1515
public sealed class ZipPaths : IEnumerable<string>
1616
{
1717
private readonly IScalar<IEnumerable<string>> files;
1818

1919
/// <summary>
20-
/// The files in a ZIP archive.
20+
/// The paths in a ZIP archive.
2121
/// Note: Extraction is sticky.
2222
/// </summary>
2323
/// <param name="input"></param>
@@ -37,8 +37,8 @@ public ZipPaths(IInput input, bool leaveOpen = true)
3737
if (zip.Entries.Count > 0)
3838
{
3939
files =
40-
new Mapped<ZipArchiveEntry, string>(
41-
entry => entry.FullName,
40+
new Mapped<ZipArchiveEntry, string>(entry =>
41+
entry.FullName,
4242
zip.Entries
4343
);
4444
}
7.91 KB
Binary file not shown.

tests/Test.Yaapii.Zip/Test.Yaapii.Zip.csproj

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,14 @@
1212
<PlatformTarget>x64</PlatformTarget>
1313
</PropertyGroup>
1414

15+
<ItemGroup>
16+
<None Remove="Datum\example.zip" />
17+
</ItemGroup>
18+
19+
<ItemGroup>
20+
<EmbeddedResource Include="Datum\example.zip" />
21+
</ItemGroup>
22+
1523
<ItemGroup>
1624
<ProjectReference Include="..\..\src\Yaapii.Zip\Yaapii.Zip.csproj" />
1725
</ItemGroup>
Lines changed: 67 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,67 @@
1+
using System;
2+
using System.Collections.Generic;
3+
using System.Text;
4+
using System.Threading.Tasks;
5+
using Xunit;
6+
using Yaapii.Atoms.IO;
7+
8+
namespace Yaapii.Zip.Test
9+
{
10+
public sealed class ZipFilesTests
11+
{
12+
[Fact]
13+
public void ListsFile()
14+
{
15+
Assert.Contains(
16+
"Oh My Dir/My File",
17+
new ZipFiles(
18+
new Zipped(
19+
"Oh My Dir/My File", new InputOf("Mista Singing Club")
20+
)
21+
)
22+
);
23+
}
24+
25+
[Fact]
26+
public void StreamIsAtStart()
27+
{
28+
var zip = new Zipped("Leave me open", new InputOf("Please!"));
29+
new ZipFiles(zip).GetEnumerator();
30+
31+
Assert.Equal(0, zip.Stream().Position);
32+
}
33+
34+
[Fact]
35+
public void LeavesOpen()
36+
{
37+
var zip = new Zipped("Leave me open", new InputOf("Please!"));
38+
new ZipFiles(zip).GetEnumerator();
39+
40+
Assert.True(zip.Stream().CanRead);
41+
}
42+
43+
[Fact]
44+
public void IsThreadSafe()
45+
{
46+
var zip = new Zipped("Leave me open", new InputOf("Please!"));
47+
48+
Parallel.For(0, System.Environment.ProcessorCount << 4, (current) =>
49+
{
50+
Assert.True(new ZipFiles(zip).GetEnumerator().MoveNext());
51+
});
52+
}
53+
54+
[Fact]
55+
public void ListsOnlyFiles()
56+
{
57+
var paths =
58+
new ZipFiles(
59+
new ResourceOf("Datum/example.zip", this.GetType())
60+
);
61+
Assert.Equal(
62+
2,
63+
new Atoms.Enumerable.LengthOf(paths).Value()
64+
);
65+
}
66+
}
67+
}

tests/Test.Yaapii.Zip/ZipContentsTests.cs renamed to tests/Test.Yaapii.Zip/ZipPathsTests.cs

Lines changed: 17 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,12 @@
1-
using System.Threading.Tasks;
1+
using System.Collections.Generic;
2+
using System.Threading.Tasks;
23
using Xunit;
4+
using Yaapii.Atoms;
35
using Yaapii.Atoms.IO;
46

57
namespace Yaapii.Zip.Test
68
{
7-
public sealed class ZipContentsTests
9+
public sealed class ZipPathsTests
810
{
911
[Fact]
1012
public void ListsFile()
@@ -47,5 +49,18 @@ public void IsThreadSafe()
4749
Assert.True(new ZipPaths(zip).GetEnumerator().MoveNext());
4850
});
4951
}
52+
53+
[Fact]
54+
public void ListsAllPaths()
55+
{
56+
var paths =
57+
new ZipPaths(
58+
new ResourceOf("Datum/example.zip", this.GetType())
59+
);
60+
Assert.Equal(
61+
6,
62+
new Atoms.Enumerable.LengthOf(paths).Value()
63+
);
64+
}
5065
}
5166
}

0 commit comments

Comments
 (0)