1
1
// Copyright (c) Microsoft. All rights reserved.
2
2
// Licensed under the MIT license. See LICENSE file in the project root for full license information.
3
3
4
- using System . IO ;
4
+ using System . Text ;
5
5
using System . Threading . Tasks ;
6
6
using Xunit ;
7
7
@@ -10,7 +10,7 @@ namespace System.IO.Compression.Test
10
10
public partial class ZipTest
11
11
{
12
12
[ Fact ]
13
- public static async Task CreateFromDirectoryNormal ( )
13
+ public async Task CreateFromDirectoryNormal ( )
14
14
{
15
15
await TestCreateDirectory ( zfolder ( "normal" ) , true ) ;
16
16
if ( Interop . IsWindows ) // [ActiveIssue(846, PlatformID.AnyUnix)]
@@ -19,22 +19,22 @@ public static async Task CreateFromDirectoryNormal()
19
19
}
20
20
}
21
21
22
- private static async Task TestCreateDirectory ( String folderName , Boolean testWithBaseDir )
22
+ private async Task TestCreateDirectory ( string folderName , Boolean testWithBaseDir )
23
23
{
24
- String noBaseDir = StreamHelpers . GetTmpFileName ( ) ;
24
+ string noBaseDir = GetTmpFilePath ( ) ;
25
25
ZipFile . CreateFromDirectory ( folderName , noBaseDir ) ;
26
26
27
27
await IsZipSameAsDirAsync ( noBaseDir , folderName , ZipArchiveMode . Read , true , true ) ;
28
28
29
29
if ( testWithBaseDir )
30
30
{
31
- String withBaseDir = StreamHelpers . GetTmpFileName ( ) ;
31
+ string withBaseDir = GetTmpFilePath ( ) ;
32
32
ZipFile . CreateFromDirectory ( folderName , withBaseDir , CompressionLevel . Optimal , true ) ;
33
33
SameExceptForBaseDir ( noBaseDir , withBaseDir , folderName ) ;
34
34
}
35
35
}
36
36
37
- private static void SameExceptForBaseDir ( String zipNoBaseDir , String zipBaseDir , String baseDir )
37
+ private static void SameExceptForBaseDir ( string zipNoBaseDir , string zipBaseDir , string baseDir )
38
38
{
39
39
//b has the base dir
40
40
using ( ZipArchive a = ZipFile . Open ( zipNoBaseDir , ZipArchiveMode . Read ) ,
@@ -62,7 +62,7 @@ private static void SameExceptForBaseDir(String zipNoBaseDir, String zipBaseDir,
62
62
}
63
63
64
64
[ Fact ]
65
- public static void ExtractToDirectoryNormal ( )
65
+ public void ExtractToDirectoryNormal ( )
66
66
{
67
67
TestExtract ( zfile ( "normal.zip" ) , zfolder ( "normal" ) ) ;
68
68
if ( Interop . IsWindows ) // [ActiveIssue(846, PlatformID.AnyUnix)]
@@ -77,38 +77,54 @@ public static void ExtractToDirectoryNormal()
77
77
TestExtract ( zfile ( "noexplicitdir.zip" ) , zfolder ( "explicitdir" ) ) ;
78
78
}
79
79
80
- private static void TestExtract ( String zipFileName , String folderName )
80
+ private void TestExtract ( string zipFileName , string folderName )
81
81
{
82
- String tempFolder = StreamHelpers . GetTmpPath ( true ) ;
82
+ string tempFolder = GetTmpDirPath ( true ) ;
83
83
ZipFile . ExtractToDirectory ( zipFileName , tempFolder ) ;
84
84
DirsEqual ( tempFolder , folderName ) ;
85
+
86
+ Assert . Throws < ArgumentNullException > ( ( ) => ZipFile . ExtractToDirectory ( null , tempFolder ) ) ;
85
87
}
86
88
87
89
#region "Extension Methods"
88
90
89
- [ Fact ]
90
- public static async Task CreateEntryFromFileTest ( )
91
+ [ Theory ]
92
+ [ InlineData ( true ) ]
93
+ [ InlineData ( false ) ]
94
+ public async Task CreateEntryFromFileTest ( bool withCompressionLevel )
91
95
{
92
96
//add file
93
- String testArchive = StreamHelpers . CreateTempCopyFile ( zfile ( "normal.zip" ) ) ;
97
+ string testArchive = CreateTempCopyFile ( zfile ( "normal.zip" ) ) ;
94
98
95
99
using ( ZipArchive archive = ZipFile . Open ( testArchive , ZipArchiveMode . Update ) )
96
100
{
97
- ZipArchiveEntry e = archive . CreateEntryFromFile ( zmodified ( Path . Combine ( "addFile" , "added.txt" ) ) , "added.txt" ) ;
101
+ string entryName = "added.txt" ;
102
+ string sourceFilePath = zmodified ( Path . Combine ( "addFile" , entryName ) ) ;
103
+
104
+ Assert . Throws < ArgumentNullException > ( ( ) => ( ( ZipArchive ) null ) . CreateEntryFromFile ( sourceFilePath , entryName ) ) ;
105
+ Assert . Throws < ArgumentNullException > ( ( ) => archive . CreateEntryFromFile ( null , entryName ) ) ;
106
+ Assert . Throws < ArgumentNullException > ( ( ) => archive . CreateEntryFromFile ( sourceFilePath , null ) ) ;
107
+
108
+ ZipArchiveEntry e = withCompressionLevel ?
109
+ archive . CreateEntryFromFile ( sourceFilePath , entryName ) :
110
+ archive . CreateEntryFromFile ( sourceFilePath , entryName , CompressionLevel . Fastest ) ;
98
111
Assert . NotNull ( e ) ;
99
112
}
100
113
101
114
await IsZipSameAsDirAsync ( testArchive , zmodified ( "addFile" ) , ZipArchiveMode . Read , true , true ) ;
102
115
}
103
116
104
117
[ Fact ]
105
- public static void ExtractToFileTest ( )
118
+ public void ExtractToFileTest ( )
106
119
{
107
120
using ( ZipArchive archive = ZipFile . Open ( zfile ( "normal.zip" ) , ZipArchiveMode . Read ) )
108
121
{
109
- String file = StreamHelpers . GetTmpFileName ( ) ;
122
+ string file = GetTmpFilePath ( ) ;
110
123
ZipArchiveEntry e = archive . GetEntry ( "first.txt" ) ;
111
124
125
+ Assert . Throws < ArgumentNullException > ( ( ) => ( ( ZipArchiveEntry ) null ) . ExtractToFile ( file ) ) ;
126
+ Assert . Throws < ArgumentNullException > ( ( ) => e . ExtractToFile ( null ) ) ;
127
+
112
128
//extract when there is nothing there
113
129
e . ExtractToFile ( file ) ;
114
130
@@ -133,11 +149,13 @@ public static void ExtractToFileTest()
133
149
}
134
150
135
151
[ Fact ]
136
- public static void ExtractToDirectoryTest ( )
152
+ public void ExtractToDirectoryTest ( )
137
153
{
138
154
using ( ZipArchive archive = ZipFile . Open ( zfile ( "normal.zip" ) , ZipArchiveMode . Read ) )
139
155
{
140
- String tempFolder = StreamHelpers . GetTmpPath ( false ) ;
156
+ string tempFolder = GetTmpDirPath ( false ) ;
157
+ Assert . Throws < ArgumentNullException > ( ( ) => ( ( ZipArchive ) null ) . ExtractToDirectory ( tempFolder ) ) ;
158
+ Assert . Throws < ArgumentNullException > ( ( ) => archive . ExtractToDirectory ( null ) ) ;
141
159
archive . ExtractToDirectory ( tempFolder ) ;
142
160
143
161
DirsEqual ( tempFolder , zfolder ( "normal" ) ) ;
@@ -147,14 +165,48 @@ public static void ExtractToDirectoryTest()
147
165
{
148
166
using ( ZipArchive archive = ZipFile . OpenRead ( zfile ( "unicode.zip" ) ) )
149
167
{
150
- String tempFolder = StreamHelpers . GetTmpPath ( false ) ;
168
+ string tempFolder = GetTmpDirPath ( false ) ;
151
169
archive . ExtractToDirectory ( tempFolder ) ;
152
170
153
171
DirsEqual ( tempFolder , zfolder ( "unicode" ) ) ;
154
172
}
155
173
}
156
174
}
157
175
176
+ [ Fact ]
177
+ public void CreatedEmptyDirectoriesRoundtrip ( )
178
+ {
179
+ DirectoryInfo rootDir = new DirectoryInfo ( GetTmpDirPath ( create : true ) ) ;
180
+ rootDir . CreateSubdirectory ( "empty1" ) ;
181
+
182
+ string archivePath = GetTmpFilePath ( ) ;
183
+ ZipFile . CreateFromDirectory (
184
+ rootDir . FullName , archivePath ,
185
+ CompressionLevel . Optimal , false , Encoding . UTF8 ) ;
186
+
187
+ using ( ZipArchive archive = ZipFile . OpenRead ( archivePath ) )
188
+ {
189
+ Assert . Equal ( 1 , archive . Entries . Count ) ;
190
+ Assert . True ( archive . Entries [ 0 ] . FullName . StartsWith ( "empty1" ) ) ;
191
+ }
192
+ }
193
+
194
+ [ Fact ]
195
+ public void CreatedEmptyRootDirectoryRoundtrips ( )
196
+ {
197
+ DirectoryInfo emptyRoot = new DirectoryInfo ( GetTmpDirPath ( create : true ) ) ;
198
+
199
+ string archivePath = GetTmpFilePath ( ) ;
200
+ ZipFile . CreateFromDirectory (
201
+ emptyRoot . FullName , archivePath ,
202
+ CompressionLevel . Optimal , true ) ;
203
+
204
+ using ( ZipArchive archive = ZipFile . OpenRead ( archivePath ) )
205
+ {
206
+ Assert . Equal ( 1 , archive . Entries . Count ) ;
207
+ }
208
+ }
209
+
158
210
#endregion
159
211
}
160
212
}
0 commit comments