@@ -27,6 +27,36 @@ public void Aes256Encryption()
2727 CreateZipWithEncryptedEntries ( "foo" , 256 ) ;
2828 }
2929
30+ [ Test ]
31+ [ Category ( "Encryption" ) ]
32+ [ Category ( "Zip" ) ]
33+ public void ZipFileAesDecryption ( )
34+ {
35+ var password = "password" ;
36+
37+ using ( var ms = new MemoryStream ( ) )
38+ {
39+ WriteEncryptedZipToStream ( ms , password , 256 ) ;
40+
41+ var zipFile = new ZipFile ( ms )
42+ {
43+ Password = password
44+ } ;
45+
46+ foreach ( ZipEntry entry in zipFile )
47+ {
48+ if ( ! entry . IsFile ) continue ;
49+
50+ using ( var zis = zipFile . GetInputStream ( entry ) )
51+ using ( var sr = new StreamReader ( zis , Encoding . UTF8 ) )
52+ {
53+ var content = sr . ReadToEnd ( ) ;
54+ Assert . AreEqual ( DummyDataString , content , "Decompressed content does not match input data" ) ;
55+ }
56+ }
57+ }
58+ }
59+
3060 private static readonly string [ ] possible7zPaths = new [ ] {
3161 // Check in PATH
3262 "7z" , "7za" ,
@@ -74,35 +104,37 @@ public static bool TryGet7zBinPath(out string path7z)
74104 return false ;
75105 }
76106
77- public void CreateZipWithEncryptedEntries ( string password , int keySize )
107+ public void WriteEncryptedZipToStream ( Stream stream , string password , int keySize )
78108 {
79- using ( var ms = new MemoryStream ( ) )
109+ using ( var zs = new ZipOutputStream ( stream ) )
80110 {
81- using ( var zs = new ZipOutputStream ( ms ) )
82- {
83- zs . IsStreamOwner = false ;
84- zs . SetLevel ( 9 ) ; // 0-9, 9 being the highest level of compression
85- zs . Password = password ; // optional. Null is the same as not setting. Required if using AES.
111+ zs . IsStreamOwner = false ;
112+ zs . SetLevel ( 9 ) ; // 0-9, 9 being the highest level of compression
113+ zs . Password = password ; // optional. Null is the same as not setting. Required if using AES.
86114
87- ZipEntry zipEntry = new ZipEntry ( "test" ) ;
88- zipEntry . AESKeySize = keySize ;
89- zipEntry . DateTime = DateTime . Now ;
115+ ZipEntry zipEntry = new ZipEntry ( "test" ) ;
116+ zipEntry . AESKeySize = keySize ;
117+ zipEntry . DateTime = DateTime . Now ;
90118
91- zs . PutNextEntry ( zipEntry ) ;
119+ zs . PutNextEntry ( zipEntry ) ;
92120
93- byte [ ] dummyData = Encoding . UTF8 . GetBytes ( @"Lorem ipsum dolor sit amet, consectetur adipiscing elit.
94- Fusce bibendum diam ac nunc rutrum ornare. Maecenas blandit elit ligula, eget suscipit lectus rutrum eu.
95- Maecenas aliquam, purus mattis pulvinar pharetra, nunc orci maximus justo, sed facilisis massa dui sed lorem.
96- Vestibulum id iaculis leo. Duis porta ante lorem. Duis condimentum enim nec lorem tristique interdum. Fusce in faucibus libero." ) ;
121+ byte [ ] dummyData = Encoding . UTF8 . GetBytes ( DummyDataString ) ;
97122
98- using ( var dummyStream = new MemoryStream ( dummyData ) )
99- {
100- dummyStream . CopyTo ( zs ) ;
101- }
102-
103- zs . CloseEntry ( ) ;
123+ using ( var dummyStream = new MemoryStream ( dummyData ) )
124+ {
125+ dummyStream . CopyTo ( zs ) ;
104126 }
105127
128+ zs . CloseEntry ( ) ;
129+ }
130+ }
131+
132+ public void CreateZipWithEncryptedEntries ( string password , int keySize )
133+ {
134+ using ( var ms = new MemoryStream ( ) )
135+ {
136+ WriteEncryptedZipToStream ( ms , password , keySize ) ;
137+
106138 if ( TryGet7zBinPath ( out string path7z ) )
107139 {
108140 Console . WriteLine ( $ "Using 7z path: \" { path7z } \" ") ;
@@ -140,5 +172,10 @@ public void CreateZipWithEncryptedEntries(string password, int keySize)
140172 }
141173
142174 }
175+
176+ const string DummyDataString = @"Lorem ipsum dolor sit amet, consectetur adipiscing elit.
177+ Fusce bibendum diam ac nunc rutrum ornare. Maecenas blandit elit ligula, eget suscipit lectus rutrum eu.
178+ Maecenas aliquam, purus mattis pulvinar pharetra, nunc orci maximus justo, sed facilisis massa dui sed lorem.
179+ Vestibulum id iaculis leo. Duis porta ante lorem. Duis condimentum enim nec lorem tristique interdum. Fusce in faucibus libero." ;
143180 }
144181}
0 commit comments