Skip to content

Commit f3f546a

Browse files
committed
refactor: ZipPasswordExtracted also extracts unencrypted content
1 parent 7296a28 commit f3f546a

File tree

2 files changed

+21
-15
lines changed

2 files changed

+21
-15
lines changed

src/Yaapii.Zip/ZipPasswordExtracted.cs

Lines changed: 17 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -28,18 +28,7 @@ public ZipPasswordExtracted(IInput zip, string virtualPath, string password)
2828

2929
public Stream Stream()
3030
{
31-
new FailWhen(
32-
() => !new IsZipArchive(this.zip).Value(),
33-
new ArgumentException(
34-
"Can not extract zip because no zip was provided."
35-
)
36-
).Go();
37-
new FailWhen(
38-
() => !new HasPassword(this.zip, this.virtualPath).Value(),
39-
new InvalidOperationException(
40-
"Can not extract zip because the file is not protected with a password."
41-
)
42-
).Go();
31+
Validate();
4332
zip.Stream().Seek(0, SeekOrigin.Begin);
4433
IInput result;
4534
using (var stream = new MemoryStream())
@@ -57,5 +46,21 @@ public Stream Stream()
5746
}
5847
return result.Stream();
5948
}
49+
50+
private void Validate()
51+
{
52+
new FailWhen(
53+
() => !new IsZipArchive(this.zip).Value(),
54+
new ArgumentException(
55+
"Can not extract zip because no zip was provided."
56+
)
57+
).Go();
58+
// ZipFile.ContainsEntry() by Ionic does not work with backslashes,
59+
// even if the path is normalized. Misterious
60+
new FailWhen(
61+
() => !new ZipContains(this.zip, this.virtualPath).Value(),
62+
new ArgumentException($"Cannot check for password because file '{this.virtualPath} doesn't exists in zip.")
63+
).Go();
64+
}
6065
}
6166
}

tests/Test.Yaapii.Zip/ZipPasswordExtractedTests.cs

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -62,14 +62,15 @@ public void ExtractsFromDifferentZips(string path)
6262
}
6363

6464
[Fact]
65-
public void FailsOnNoPassword()
65+
public void ExtractsUnencryptedZip()
6666
{
67-
Assert.Throws<InvalidOperationException>(() =>
67+
Assert.Equal(
68+
"unsafe",
6869
new TextOf(
6970
new ZipPasswordExtracted(
7071
new Zipped(
7172
"text.txt",
72-
new InputOf("safe")
73+
new InputOf("unsafe")
7374
),
7475
"text.txt",
7576
"password"

0 commit comments

Comments
 (0)