1 package org.apache.maven.shared.utils;
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22 import java.io.File;
23 import java.io.FileInputStream;
24 import java.io.FileOutputStream;
25 import java.io.IOException;
26 import java.io.InputStream;
27 import java.util.Date;
28 import java.util.zip.ZipEntry;
29 import java.util.zip.ZipInputStream;
30 import org.apache.maven.shared.utils.io.FileUtils;
31
32
33
34
35
36
37 class Expand
38 {
39
40
41
42 private File source;
43
44
45
46
47 private File dest;
48
49
50
51
52 private boolean overwrite = false;
53
54 private static final int BUFFER_SIZE = 2 ^ 16;
55
56
57
58
59
60
61
62 public void setSrc( File sourceArchive )
63 {
64 this.source = sourceArchive;
65 }
66
67
68
69
70
71
72
73
74 public void setDest( File destinationDirectory )
75 {
76 this.dest = destinationDirectory;
77 }
78
79
80
81
82
83
84
85
86 public void setOverwrite( boolean overwrite )
87 {
88 this.overwrite = overwrite;
89 }
90
91
92
93
94
95
96
97 public void execute()
98 throws Exception
99 {
100 expandFile( source, dest );
101 }
102
103
104
105
106
107
108
109
110
111
112 void expandFile( File srcFile, File dest )
113 throws Exception
114 {
115 if ( source == null )
116 {
117 throw new NullPointerException( "Source Archive must not be null!" );
118 }
119
120 File destDir = dest;
121 if ( destDir == null )
122 {
123 destDir = new File( System.getProperty( "user.dir" ) );
124 }
125
126 FileInputStream fileInputStream = new FileInputStream( srcFile );
127 try
128 {
129 ZipInputStream zipInputStream = new ZipInputStream( fileInputStream );
130
131 ZipEntry zipEntry;
132
133 while ( ( zipEntry = zipInputStream.getNextEntry() ) != null )
134 {
135 String zipEntryName = zipEntry.getName();
136 Date zipEntryDate = new Date( zipEntry.getTime() );
137
138 extractFile( source, destDir, zipInputStream, zipEntryName, zipEntryDate, zipEntry.isDirectory() );
139 }
140 }
141 finally
142 {
143 try
144 {
145 fileInputStream.close();
146 }
147 catch ( IOException ioe )
148 {
149
150 }
151 }
152 }
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167 void extractFile( File archive, File destDir, InputStream compressedInputStream, String entryName,
168 Date entryDate, boolean isDirectory )
169 throws Exception
170 {
171 File targetFile = new File( destDir, entryName );
172
173
174
175 if ( overwrite && targetFile.exists() && targetFile.isDirectory() != isDirectory )
176 {
177 deleteFileOrDir( targetFile );
178 }
179
180 if ( !targetFile.exists() || overwrite || targetFile.lastModified() <= entryDate.getTime() )
181 {
182 if ( isDirectory )
183 {
184 targetFile.mkdirs();
185 }
186 else
187 {
188 byte[] buffer = new byte[BUFFER_SIZE];
189 FileOutputStream fileOutputStream = new FileOutputStream( targetFile );
190 try
191 {
192 int len;
193 while ( ( len = compressedInputStream.read( buffer ) ) > 0 )
194 {
195 fileOutputStream.write( buffer, 0, len );
196 }
197 }
198 finally
199 {
200 try
201 {
202 fileOutputStream.close();
203 }
204 catch ( IOException ioe )
205 {
206
207 }
208 }
209 targetFile.setLastModified( entryDate.getTime() );
210 }
211 }
212 }
213
214
215
216
217
218
219
220 private void deleteFileOrDir( File targetFile )
221 throws IOException
222 {
223 if ( targetFile.isDirectory() )
224 {
225 FileUtils.deleteDirectory( targetFile );
226 }
227 else
228 {
229 targetFile.delete();
230 }
231
232 }
233 }