@@ -19,6 +19,14 @@ public void Aes128Encryption()
19
19
CreateZipWithEncryptedEntries ( "foo" , 128 ) ;
20
20
}
21
21
22
+ [ Test ]
23
+ [ Category ( "Encryption" ) ]
24
+ [ Category ( "Zip" ) ]
25
+ public void Aes128EncryptionStored ( )
26
+ {
27
+ CreateZipWithEncryptedEntries ( "foo" , 128 , CompressionMethod . Stored ) ;
28
+ }
29
+
22
30
[ Test ]
23
31
[ Category ( "Encryption" ) ]
24
32
[ Category ( "Zip" ) ]
@@ -27,6 +35,14 @@ public void Aes256Encryption()
27
35
CreateZipWithEncryptedEntries ( "foo" , 256 ) ;
28
36
}
29
37
38
+ [ Test ]
39
+ [ Category ( "Encryption" ) ]
40
+ [ Category ( "Zip" ) ]
41
+ public void Aes256EncryptionStored ( )
42
+ {
43
+ CreateZipWithEncryptedEntries ( "foo" , 256 , CompressionMethod . Stored ) ;
44
+ }
45
+
30
46
[ Test ]
31
47
[ Category ( "Encryption" ) ]
32
48
[ Category ( "Zip" ) ]
@@ -88,6 +104,47 @@ public void ZipFileAesRead()
88
104
}
89
105
}
90
106
107
+ /// <summary>
108
+ /// Test using AES encryption on a file whose contents are Stored rather than deflated
109
+ /// </summary>
110
+ [ Test ]
111
+ [ Category ( "Encryption" ) ]
112
+ [ Category ( "Zip" ) ]
113
+ public void ZipFileStoreAes ( )
114
+ {
115
+ string password = "password" ;
116
+
117
+ using ( var memoryStream = new MemoryStream ( ) )
118
+ {
119
+ // Try to create a zip stream
120
+ WriteEncryptedZipToStream ( memoryStream , password , 256 , CompressionMethod . Stored ) ;
121
+
122
+ // reset
123
+ memoryStream . Seek ( 0 , SeekOrigin . Begin ) ;
124
+
125
+ // try to read it
126
+ var zipFile = new ZipFile ( memoryStream , leaveOpen : true )
127
+ {
128
+ Password = password
129
+ } ;
130
+
131
+ foreach ( ZipEntry entry in zipFile )
132
+ {
133
+ if ( ! entry . IsFile ) continue ;
134
+
135
+ // Should be stored rather than deflated
136
+ Assert . That ( entry . CompressionMethod , Is . EqualTo ( CompressionMethod . Stored ) , "Entry should be stored" ) ;
137
+
138
+ using ( var zis = zipFile . GetInputStream ( entry ) )
139
+ using ( var sr = new StreamReader ( zis , Encoding . UTF8 ) )
140
+ {
141
+ var content = sr . ReadToEnd ( ) ;
142
+ Assert . That ( content , Is . EqualTo ( DummyDataString ) , "Decompressed content does not match input data" ) ;
143
+ }
144
+ }
145
+ }
146
+ }
147
+
91
148
private static readonly string [ ] possible7zPaths = new [ ] {
92
149
// Check in PATH
93
150
"7z" , "7za" ,
@@ -135,7 +192,7 @@ public static bool TryGet7zBinPath(out string path7z)
135
192
return false ;
136
193
}
137
194
138
- public void WriteEncryptedZipToStream ( Stream stream , string password , int keySize )
195
+ public void WriteEncryptedZipToStream ( Stream stream , string password , int keySize , CompressionMethod compressionMethod = CompressionMethod . Deflated )
139
196
{
140
197
using ( var zs = new ZipOutputStream ( stream ) )
141
198
{
@@ -146,6 +203,7 @@ public void WriteEncryptedZipToStream(Stream stream, string password, int keySiz
146
203
ZipEntry zipEntry = new ZipEntry ( "test" ) ;
147
204
zipEntry . AESKeySize = keySize ;
148
205
zipEntry . DateTime = DateTime . Now ;
206
+ zipEntry . CompressionMethod = compressionMethod ;
149
207
150
208
zs . PutNextEntry ( zipEntry ) ;
151
209
@@ -160,11 +218,11 @@ public void WriteEncryptedZipToStream(Stream stream, string password, int keySiz
160
218
}
161
219
}
162
220
163
- public void CreateZipWithEncryptedEntries ( string password , int keySize )
221
+ public void CreateZipWithEncryptedEntries ( string password , int keySize , CompressionMethod compressionMethod = CompressionMethod . Deflated )
164
222
{
165
223
using ( var ms = new MemoryStream ( ) )
166
224
{
167
- WriteEncryptedZipToStream ( ms , password , keySize ) ;
225
+ WriteEncryptedZipToStream ( ms , password , keySize , compressionMethod ) ;
168
226
169
227
if ( TryGet7zBinPath ( out string path7z ) )
170
228
{
0 commit comments