-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathFileLister.java
More file actions
101 lines (80 loc) · 2.75 KB
/
FileLister.java
File metadata and controls
101 lines (80 loc) · 2.75 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
/*
Copyright (C) 2022 hidenorly
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/
package com.github.hidenorly.jdcli;
import java.io.IOException;
import java.nio.file.attribute.BasicFileAttributes;
import java.nio.file.Files;
import java.nio.file.FileSystem;
import java.nio.file.FileSystems;
import java.nio.file.FileVisitResult;
import java.nio.file.FileVisitor;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.util.ArrayList;
import java.util.List;
public class FileLister implements FileVisitor<Path> {
protected List<Path> mFiles = new ArrayList<Path>();
protected String mFilterRegexp;
protected FileLister(String filterRegexp){
mFilterRegexp = filterRegexp;
}
@Override
public FileVisitResult preVisitDirectory(Path dir, BasicFileAttributes attrs) throws IOException {
//mFiles.add( dir.toString() );
return FileVisitResult.CONTINUE;
}
@Override
public FileVisitResult visitFile(Path file, BasicFileAttributes attrs) throws IOException {
String _path = file.toString();
if( mFilterRegexp.isEmpty() || _path.matches(mFilterRegexp) ){
mFiles.add( file );
}
return FileVisitResult.CONTINUE;
}
@Override
public FileVisitResult postVisitDirectory(Path dir, IOException exc) throws IOException {
return FileVisitResult.CONTINUE;
}
@Override
public FileVisitResult visitFileFailed(Path file, IOException exc) throws IOException {
return FileVisitResult.CONTINUE;
}
protected List<Path> getFiles(){
return mFiles;
}
public static List<Path> getFileList(String path, String filterRegexp){
Path root = null;
try{
if( path.endsWith(".zip") || path.endsWith(".jar") || path.endsWith(".apk") ){
Path zipFile = Paths.get( path );
ClassLoader loader = null;
FileSystem fs = FileSystems.newFileSystem( zipFile, loader );
root = fs.getPath("/");
} else {
root = Paths.get( path );
}
} catch(IOException ex){
}
FileLister visitor = new FileLister( filterRegexp );
if( root != null ){
try{
Files.walkFileTree(root, visitor);
} catch(IOException ex ){
}
}
return visitor.getFiles();
}
public static List<Path> getFileList(String path){
return getFileList( path, "" );
}
}