|
@@ -12,15 +12,6 @@ |
12 |
12 |
|
*/ |
13 |
13 |
|
public class Paths |
14 |
14 |
|
{ |
15 |
|
- |
static private final Comparator<FilePath> LONGEST_TO_SHORTEST = new Comparator<FilePath>() |
16 |
|
- |
{ |
17 |
|
- |
@Override |
18 |
|
- |
public int compare( FilePath s1, FilePath s2 ) |
19 |
|
- |
{ |
20 |
|
- |
return s2.canonical().length() - s1.canonical().length(); |
21 |
|
- |
} |
22 |
|
- |
}; |
23 |
|
- |
|
24 |
15 |
|
static private List<String> sDefaultGlobExcludes = new ArrayList<String>(); |
25 |
16 |
|
|
26 |
17 |
|
/** |
|
@@ -39,7 +30,6 @@ |
39 |
30 |
|
* Creates a Paths object and calls {@link #glob(String, String[])} with the specified arguments. |
40 |
31 |
|
*/ |
41 |
32 |
|
public Paths( String dir, String... patterns ) |
42 |
|
- |
throws IOException |
43 |
33 |
|
{ |
44 |
34 |
|
glob( dir, patterns ); |
45 |
35 |
|
} |
|
@@ -88,7 +78,6 @@ |
88 |
78 |
|
*/ |
89 |
79 |
|
@SuppressWarnings({"UnusedDeclaration"}) |
90 |
80 |
|
public void glob( String dir, List<String> patterns ) |
91 |
|
- |
throws IOException |
92 |
81 |
|
{ |
93 |
82 |
|
glob( dir, (patterns == null) ? Util.EMPTY_STRING_ARRAY : patterns.toArray( new String[patterns.size()] ) ); |
94 |
83 |
|
} |
|
@@ -116,7 +105,6 @@ |
116 |
105 |
|
* patterns would select the paths. |
117 |
106 |
|
*/ |
118 |
107 |
|
public void glob( String dir, String... patterns ) |
119 |
|
- |
throws IOException |
120 |
108 |
|
{ |
121 |
109 |
|
new PathPatterns( dir, patterns ).addTo( mPaths ); |
122 |
110 |
|
} |
|
@@ -130,7 +118,6 @@ |
130 |
118 |
|
*/ |
131 |
119 |
|
@SuppressWarnings({"ResultOfMethodCallIgnored"}) |
132 |
120 |
|
public Paths copyTo( String destDir ) |
133 |
|
- |
throws IOException |
134 |
121 |
|
{ |
135 |
122 |
|
File zDest = new File( destDir ); |
136 |
123 |
|
zDest.mkdirs(); |
|
@@ -139,43 +126,24 @@ |
139 |
126 |
|
for ( FilePath path : getPaths() ) |
140 |
127 |
|
{ |
141 |
128 |
|
String zSubPath = path.getFileSubPath(); |
142 |
|
- |
Util.copyFile( path.file(), new File( destDir, zSubPath ) ); |
|
129 |
+ |
FileUtil.copyFile( path.file(), new File( destDir, zSubPath ) ); |
143 |
130 |
|
newPaths.mPaths.add( new FilePath( zDest, zSubPath ) ); |
144 |
131 |
|
} |
145 |
132 |
|
return newPaths; |
146 |
133 |
|
} |
147 |
134 |
|
|
148 |
135 |
|
/** |
149 |
|
- |
* Deletes all the files, directories, and any files in the directories. |
150 |
|
- |
* |
151 |
|
- |
* @return False if any file could not be deleted. |
152 |
|
- |
*/ |
153 |
|
- |
public boolean delete() |
154 |
|
- |
{ |
155 |
|
- |
boolean success = true; |
156 |
|
- |
List<FilePath> pathsCopy = getPaths(); |
157 |
|
- |
Collections.sort( pathsCopy, LONGEST_TO_SHORTEST ); |
158 |
|
- |
for ( FilePath zPath : pathsCopy ) |
159 |
|
- |
{ |
160 |
|
- |
success &= Util.delete( zPath.file() ); |
161 |
|
- |
} |
162 |
|
- |
return success; |
163 |
|
- |
} |
164 |
|
- |
|
165 |
|
- |
/** |
166 |
136 |
|
* Compresses the files and directories specified by the paths into a new zip file at the specified location. If there are no |
167 |
137 |
|
* paths or all the paths are directories, no zip file will be created. |
168 |
138 |
|
* |
169 |
139 |
|
* @return Files Zipped, 0 means Zip File not even created! |
170 |
140 |
|
*/ |
171 |
141 |
|
public int zip( String destFile ) |
172 |
|
- |
throws IOException |
173 |
142 |
|
{ |
174 |
143 |
|
return zip( destFile, ZipFactory.FOR_ZIPS ); |
175 |
144 |
|
} |
176 |
145 |
|
|
177 |
146 |
|
public int zip( String destFile, ZipFactory pFactory ) |
178 |
|
- |
throws IOException |
179 |
147 |
|
{ |
180 |
148 |
|
List<FilePath> zPaths = getPaths(); |
181 |
149 |
|
if ( !zPaths.isEmpty() ) |
|
@@ -186,8 +154,15 @@ |
186 |
154 |
|
{ |
187 |
155 |
|
for ( FilePath path : zPaths ) |
188 |
156 |
|
{ |
189 |
|
- |
out.putNextEntry( pFactory.createZE( path.getFileSubPath().replace( '\\', '/' ) ) ); |
190 |
|
- |
FileInputStream in = new FileInputStream( path.file() ); |
|
157 |
+ |
try |
|
158 |
+ |
{ |
|
159 |
+ |
out.putNextEntry( pFactory.createZE( path.getFileSubPath().replace( '\\', '/' ) ) ); |
|
160 |
+ |
} |
|
161 |
+ |
catch ( IOException e ) |
|
162 |
+ |
{ |
|
163 |
+ |
throw new WrappedIOException( e ); |
|
164 |
+ |
} |
|
165 |
+ |
FileInputStream in = FileUtil.createFileInputStream( path.file() ); |
191 |
166 |
|
try |
192 |
167 |
|
{ |
193 |
168 |
|
for ( int len; (len = in.read( buf )) > -1; ) |
|
@@ -199,15 +174,19 @@ |
199 |
174 |
|
} |
200 |
175 |
|
out.closeEntry(); |
201 |
176 |
|
} |
|
177 |
+ |
catch ( IOException e ) |
|
178 |
+ |
{ |
|
179 |
+ |
throw new WrappedIOException( e ); |
|
180 |
+ |
} |
202 |
181 |
|
finally |
203 |
182 |
|
{ |
204 |
|
- |
in.close(); |
|
183 |
+ |
FileUtil.close( in ); |
205 |
184 |
|
} |
206 |
185 |
|
} |
207 |
186 |
|
} |
208 |
187 |
|
finally |
209 |
188 |
|
{ |
210 |
|
- |
out.close(); |
|
189 |
+ |
FileUtil.close( out ); |
211 |
190 |
|
} |
212 |
191 |
|
} |
213 |
192 |
|
return zPaths.size(); |
|
@@ -327,12 +306,12 @@ |
327 |
306 |
|
} |
328 |
307 |
|
} |
329 |
308 |
|
|
330 |
|
- |
|
331 |
309 |
|
private static class PathPatterns |
332 |
310 |
|
{ |
333 |
|
- |
private File mPath; |
334 |
|
- |
private List<Pattern> mIncludes = new ArrayList<Pattern>(); |
335 |
|
- |
private List<Pattern> mExcludes = new ArrayList<Pattern>(); |
|
311 |
+ |
private final File mPath; |
|
312 |
+ |
private final boolean mIsFile; |
|
313 |
+ |
private final List<Pattern> mIncludes = new ArrayList<Pattern>(); |
|
314 |
+ |
private final List<Pattern> mExcludes = new ArrayList<Pattern>(); |
336 |
315 |
|
|
337 |
316 |
|
public PathPatterns( String pPath, String[] pPatterns ) |
338 |
317 |
|
{ |
|
@@ -347,19 +326,23 @@ |
347 |
326 |
|
pPatterns[i - 1] = split[i].trim(); |
348 |
327 |
|
} |
349 |
328 |
|
} |
350 |
|
- |
mPath = new File( pPath ); |
351 |
|
- |
if ( mPath.isFile() ) |
|
329 |
+ |
File zPath = new File( pPath ); |
|
330 |
+ |
if ( zPath.isFile() ) |
352 |
331 |
|
{ |
353 |
332 |
|
if ( pPatterns.length != 0 ) |
354 |
333 |
|
{ |
355 |
|
- |
throw new IllegalArgumentException( "Files (e.g. " + mPath + ") may NOT have patterns: " + Arrays.asList( pPatterns ) ); |
|
334 |
+ |
throw new IllegalArgumentException( "Files (e.g. " + zPath + ") may NOT have patterns: " + Arrays.asList( pPatterns ) ); |
356 |
335 |
|
} |
|
336 |
+ |
mIsFile = true; |
|
337 |
+ |
mPath = FileUtil.getCanonicalFile( zPath ); |
357 |
338 |
|
return; |
358 |
339 |
|
} |
359 |
|
- |
if ( !mPath.isDirectory() ) |
|
340 |
+ |
if ( !zPath.isDirectory() ) |
360 |
341 |
|
{ |
361 |
|
- |
throw new IllegalArgumentException( "Path Reference not a File or Directory: " + mPath ); |
|
342 |
+ |
throw new IllegalArgumentException( "Path Reference not a File or Directory: " + zPath ); |
362 |
343 |
|
} |
|
344 |
+ |
mIsFile = false; |
|
345 |
+ |
mPath = FileUtil.getCanonicalFile( zPath ); |
363 |
346 |
|
List<String> zIncludes = new ArrayList<String>(); |
364 |
347 |
|
List<String> zExcludes = new ArrayList<String>(); |
365 |
348 |
|
for ( String zPattern : pPatterns ) |
|
@@ -399,42 +382,37 @@ |
399 |
382 |
|
} |
400 |
383 |
|
|
401 |
384 |
|
public void addTo( RootedPathsCollection pPaths ) |
402 |
|
- |
throws IOException |
403 |
385 |
|
{ |
404 |
|
- |
mPath = mPath.getCanonicalFile(); |
405 |
|
- |
if ( mPath.isFile() ) |
|
386 |
+ |
if ( mIsFile ) |
406 |
387 |
|
{ |
407 |
388 |
|
pPaths.add( new FilePath( mPath.getParentFile(), mPath.getName() ) ); |
408 |
389 |
|
return; |
409 |
390 |
|
} |
410 |
|
- |
if ( mPath.isDirectory() ) |
|
391 |
+ |
// Must be a Directory! (See Above) |
|
392 |
+ |
RootedPaths zPaths = new RootedPaths( mPath ); |
|
393 |
+ |
String[] zFileNames = mPath.list(); |
|
394 |
+ |
for ( String zFileName : zFileNames ) |
411 |
395 |
|
{ |
412 |
|
- |
RootedPaths zPaths = new RootedPaths( mPath ); |
413 |
|
- |
String[] zFileNames = mPath.list(); |
414 |
|
- |
for ( String zFileName : zFileNames ) |
|
396 |
+ |
File zFile = new File( mPath, zFileName ); |
|
397 |
+ |
if ( zFile.isDirectory() ) |
415 |
398 |
|
{ |
416 |
|
- |
File zFile = new File( mPath, zFileName ); |
417 |
|
- |
if ( zFile.isDirectory() ) |
|
399 |
+ |
if ( !excludedDir( zFileName ) && acceptableDir( zFileName ) ) |
418 |
400 |
|
{ |
419 |
|
- |
if ( !excludedDir( zFileName ) && acceptableDir( zFileName ) ) |
420 |
|
- |
{ |
421 |
|
- |
addTo( zPaths, zFileName + "/", zFile ); |
422 |
|
- |
} |
|
401 |
+ |
addTo( zPaths, zFileName + "/", zFile ); |
423 |
402 |
|
} |
424 |
|
- |
else |
|
403 |
+ |
} |
|
404 |
+ |
else |
|
405 |
+ |
{ |
|
406 |
+ |
if ( !excludedFile( zFileName ) && acceptableFile( zFileName ) ) |
425 |
407 |
|
{ |
426 |
|
- |
if ( !excludedFile( zFileName ) && acceptableFile( zFileName ) ) |
427 |
|
- |
{ |
428 |
|
- |
zPaths.addCanonicalRelativePath( zFileName ); |
429 |
|
- |
} |
|
408 |
+ |
zPaths.addCanonicalRelativePath( zFileName ); |
430 |
409 |
|
} |
431 |
410 |
|
} |
432 |
|
- |
pPaths.add( zPaths ); |
433 |
411 |
|
} |
|
412 |
+ |
pPaths.add( zPaths ); |
434 |
413 |
|
} |
435 |
414 |
|
|
436 |
415 |
|
private void addTo( RootedPaths pPaths, String pAdditionalDirPath, File pDirectory ) |
437 |
|
- |
throws IOException |
438 |
416 |
|
{ |
439 |
417 |
|
String[] zFileNames = pDirectory.list(); |
440 |
418 |
|
for ( String zFileName : zFileNames ) |
|
@@ -453,7 +431,7 @@ |
453 |
431 |
|
if ( !excludedFile( zPath ) && acceptableFile( zPath ) ) |
454 |
432 |
|
{ |
455 |
433 |
|
|
456 |
|
- |
String zFullCanonicalPath = zFile.getCanonicalPath(); |
|
434 |
+ |
String zFullCanonicalPath = FileUtil.getCanonicalPath( zFile ); |
457 |
435 |
|
String zRelativeCanonicalPath = zFullCanonicalPath.substring( mPath.getPath().length() + 1 ); |
458 |
436 |
|
pPaths.addCanonicalRelativePath( zRelativeCanonicalPath ); |
459 |
437 |
|
} |