Skip to content

Commit ebc4787

Browse files
author
koeeenig
authored
Merge pull request #14 from icarus-consulting/i12-extraction-fails-depends-on-zip
Tests with ZIPs from different tools
2 parents bd98d26 + d35e0a1 commit ebc4787

21 files changed

+419
-40
lines changed

README.md

Lines changed: 45 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -20,15 +20,29 @@ It does not work with simple compressed streams of data.
2020
You can create a new ZIP archive like this:
2121

2222
```csharp
23-
var zipArchive =
23+
var file =
2424
new ZipExtracted(
2525
new Zipped(
2626
"small.dat", new InputOf("I feel so compressed")
2727
),
2828
"small.dat"
2929
).Stream();
3030
```
31-
31+
or with multiple files
32+
```csharp
33+
new Zipped(
34+
new Dictionary<string, IInput>()
35+
{
36+
{"small.dat", new InputOf("I feel so compressed") },
37+
{"smaller.dat", new InputOf("I'm so compressed") }
38+
}
39+
);
40+
41+
new Zipped(
42+
new KeyValuePair<string, IInput>("small.dat", new InputOf("I feel so compressed")),
43+
new KeyValuePair<string, IInput>("smaller.dat", new InputOf("I'm so compressed"))
44+
);
45+
```
3246
Update a file in the ZIP:
3347

3448
```csharp
@@ -38,6 +52,15 @@ var updated =
3852
"Brave Citizens.txt", new InputOf("Edward Snowden")
3953
).Stream();
4054

55+
```
56+
or add a file to the ZIP if it doesn't exist
57+
```csharp
58+
var updated =
59+
new ZipUpdated(
60+
new Zipped("Brave Citizens.txt", new InputOf("Edward Snowden")),
61+
"Concerned Citizens.txt", new InputOf("Donald")
62+
).Stream();
63+
4164
```
4265

4366
Extract a file in the zip:
@@ -51,7 +74,7 @@ var extracted =
5174
).Stream();
5275
```
5376

54-
List the contents of an archive:
77+
List the contents (directories and files) of an archive:
5578

5679
```csharp
5780
IEnumerable<string> contents =
@@ -60,6 +83,15 @@ IEnumerable<string> contents =
6083
);
6184
```
6285

86+
List all files only of an archive:
87+
88+
```csharp
89+
IEnumerable<string> files =
90+
new ZipFiles(
91+
new InputOf(File.OpenRead("c:/some-zip-archive.zip")),
92+
);
93+
```
94+
6395
Remove a file from the archive:
6496

6597
```csharp
@@ -89,6 +121,16 @@ var isZip =
89121
).Value();
90122
```
91123

124+
Test if a file exists in a zip:
125+
126+
```csharp
127+
var exists =
128+
new ZipContains(
129+
new InputOf(File.OpenRead("c:/some-zip-archive.zip")),
130+
"someFileInTheZip.txt")
131+
).Value();
132+
```
133+
92134
Test if a file in a zip is protected with a password:
93135

94136
```csharp

src/Yaapii.Zip/ZipUpdated.cs

Lines changed: 24 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -3,22 +3,22 @@
33
using System.IO.Compression;
44
using Yaapii.Atoms;
55
using Yaapii.Atoms.Enumerable;
6+
using Yaapii.Atoms.Error;
67
using Yaapii.Atoms.IO;
78
using Yaapii.Atoms.Scalar;
8-
using Yaapii.Atoms.Text;
99

1010
namespace Yaapii.Zip
1111
{
1212
/// <summary>
13-
/// A zip from which a file has been removed.
13+
/// A zip from which a file has been updated or added.
1414
/// If the file to update does not exist, it is created.
1515
/// </summary>
1616
public sealed class ZipUpdated : IInput
1717
{
1818
private readonly IScalar<Stream> zip;
1919

2020
/// <summary>
21-
/// A zip in which a file has been updated.
21+
/// A zip in which a file has been updated or added.
2222
/// If the file to update does not exist, it is created.
2323
/// </summary>
2424
public ZipUpdated(IInput input, string pathToUpdate, IInput update, bool leaveOpen = true) : this(
@@ -27,13 +27,20 @@ public ZipUpdated(IInput input, string pathToUpdate, IInput update, bool leaveOp
2727
{ }
2828

2929
/// <summary>
30-
/// A zip from which a file has been removed.
30+
/// A zip from which a file has been updated or added.
3131
/// If the file to update does not exist, it is created.
3232
/// </summary>
3333
public ZipUpdated(IScalar<Stream> zip, string pathToUpdate, IInput update, bool leaveOpen)
3434
{
3535
this.zip = new Solid<Stream>(() =>
3636
{
37+
new FailWhen(
38+
() => ExistsAndCrypted(new InputOf(zip.Value()), pathToUpdate),
39+
new InvalidOperationException(
40+
$"Cannot update '{pathToUpdate}' because the file is password protected"
41+
)
42+
).Go();
43+
3744
lock (zip.Value())
3845
{
3946
var stream = zip.Value();
@@ -76,5 +83,18 @@ private string Normalized(string path)
7683
var file = Path.GetFileName(path).ToLower();
7784
return Path.Combine(dir, file);
7885
}
86+
87+
private bool ExistsAndCrypted(IInput zip, string filepath)
88+
{
89+
bool result = false;
90+
if(new ZipContains(zip, filepath).Value())
91+
{
92+
if(new HasPassword(zip, filepath).Value())
93+
{
94+
result = true;
95+
}
96+
}
97+
return result;
98+
}
7999
}
80100
}

tests/Test.Yaapii.Zip/Datum/7zip.zip

3.62 KB
Binary file not shown.
3.81 KB
Binary file not shown.
4.4 KB
Binary file not shown.
2.38 KB
Binary file not shown.
3.65 KB
Binary file not shown.
4.08 KB
Binary file not shown.
4.68 KB
Binary file not shown.

tests/Test.Yaapii.Zip/HasPasswordTests.cs

Lines changed: 29 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,4 @@
11
using System;
2-
using System.Collections.Generic;
3-
using System.Text;
42
using Xunit;
53
using Yaapii.Atoms.IO;
64

@@ -23,6 +21,21 @@ public void ReturnsTrueOnPassword()
2321
);
2422
}
2523

24+
[Theory]
25+
[InlineData("Datum/7zip_crypt.zip")]
26+
[InlineData("Datum/7zip_crypt_aes.zip")]
27+
[InlineData("Datum/winrar_crypt.zip")]
28+
[InlineData("Datum/winrar_crypt_aes.zip")]
29+
public void HasPasswordFromDifferentZips(string path)
30+
{
31+
Assert.True(
32+
new HasPassword(
33+
new ResourceOf(path, this.GetType()),
34+
@"c\Y\test-a-y-1.txt"
35+
).Value()
36+
);
37+
}
38+
2639
[Fact]
2740
public void ReturnsFalseOnNoPassword()
2841
{
@@ -37,6 +50,20 @@ public void ReturnsFalseOnNoPassword()
3750
);
3851
}
3952

53+
[Theory]
54+
[InlineData("Datum/windows.zip")]
55+
[InlineData("Datum/7zip.zip")]
56+
[InlineData("Datum/winrar.zip")]
57+
public void HasNoPasswordFromDifferentZips(string path)
58+
{
59+
Assert.False(
60+
new HasPassword(
61+
new ResourceOf(path, this.GetType()),
62+
@"c\Y\test-a-y-1.txt"
63+
).Value()
64+
);
65+
}
66+
4067
[Fact]
4168
public void ThrowsOnNoZip()
4269
{

0 commit comments

Comments
 (0)