Added sqlite support and fixed some issues
This commit is contained in:
parent
78205d97af
commit
fcbaef3e76
26 changed files with 348 additions and 371 deletions
|
|
@ -23,49 +23,68 @@
|
|||
package zutil.io.file;
|
||||
|
||||
import java.io.File;
|
||||
import java.io.FileInputStream;
|
||||
import java.io.IOException;
|
||||
import java.io.InputStream;
|
||||
import java.net.MalformedURLException;
|
||||
import java.net.URL;
|
||||
import java.util.ArrayList;
|
||||
import java.util.Arrays;
|
||||
import java.util.Enumeration;
|
||||
import java.util.Iterator;
|
||||
import java.util.List;
|
||||
import java.util.zip.ZipEntry;
|
||||
import java.util.zip.ZipFile;
|
||||
|
||||
public class FileSearch implements Iterable<File>{
|
||||
import zutil.io.InputStreamCloser;
|
||||
|
||||
public class FileSearch implements Iterable<FileSearchItem>{
|
||||
// Constants
|
||||
private static final List<String> compressedFileExtensions = Arrays.asList(new String[]{
|
||||
"jar", "zip"
|
||||
});
|
||||
|
||||
// Constructor params
|
||||
private File root;
|
||||
|
||||
|
||||
// Search parameters
|
||||
private String fileName;
|
||||
private String extension;
|
||||
private boolean recursive;
|
||||
//private int depth;
|
||||
private boolean searchFiles;
|
||||
private boolean searchCompressedFiles;
|
||||
private boolean searchFolders;
|
||||
|
||||
|
||||
|
||||
|
||||
public FileSearch(File root){
|
||||
this.root = root;
|
||||
searchFiles = true;
|
||||
searchFolders = true;
|
||||
}
|
||||
|
||||
|
||||
|
||||
|
||||
/**
|
||||
* @param file Sets the exact file name to search for (includes extension)
|
||||
*/
|
||||
public void setFileName(String file){
|
||||
fileName = file;
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* Sets the file extensions to search for (should not include . at the beggining)
|
||||
*/
|
||||
public void setExtension(String ext){
|
||||
|
||||
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* Sets if the search should go into sub-folders
|
||||
*/
|
||||
public void setRecursive(boolean recursive){
|
||||
this.recursive = recursive;
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* Sets how deep into folders the search should go
|
||||
* (Recursion needs to be enabled for this attribute to be used)
|
||||
|
|
@ -73,75 +92,153 @@ public class FileSearch implements Iterable<File>{
|
|||
//public void setDepth(int depth){
|
||||
// this.depth = depth;
|
||||
//}
|
||||
|
||||
|
||||
public void searchFiles(boolean searchFiles){
|
||||
this.searchFiles = searchFiles;
|
||||
}
|
||||
public void searchCompressedFiles(boolean searchCompressedFiles){
|
||||
this.searchCompressedFiles = searchCompressedFiles;
|
||||
}
|
||||
public void searchFolders(boolean searchFolders){
|
||||
this.searchFolders = searchFolders;
|
||||
}
|
||||
|
||||
|
||||
|
||||
@Override
|
||||
public Iterator<File> iterator() {
|
||||
public Iterator<FileSearchItem> iterator() {
|
||||
return new FileSearchIterator();
|
||||
}
|
||||
|
||||
|
||||
protected class FileSearchIterator implements Iterator<File>{
|
||||
private ArrayList<File> fileList;
|
||||
|
||||
|
||||
protected class FileSearchIterator implements Iterator<FileSearchItem>{
|
||||
private ArrayList<FileSearchItem> fileList;
|
||||
private int currentIndex;
|
||||
|
||||
public FileSearchIterator(){
|
||||
fileList = new ArrayList<File>();
|
||||
fileList = new ArrayList<FileSearchItem>();
|
||||
currentIndex = 0;
|
||||
|
||||
addToFileList(root.listFiles());
|
||||
|
||||
addFiles(root.list());
|
||||
next();
|
||||
}
|
||||
|
||||
|
||||
@Override
|
||||
public boolean hasNext() {
|
||||
return currentIndex != fileList.size();
|
||||
}
|
||||
|
||||
|
||||
@Override
|
||||
public void remove() {
|
||||
throw new UnsupportedOperationException();
|
||||
}
|
||||
|
||||
@Override
|
||||
public File next() {
|
||||
public FileSearchItem next() {
|
||||
if(currentIndex < 0)
|
||||
return null;
|
||||
// Temporarily save the current file
|
||||
File ret = fileList.get(currentIndex);
|
||||
|
||||
FileSearchItem ret = fileList.get(currentIndex);
|
||||
|
||||
// Find the next file
|
||||
for(; currentIndex<fileList.size(); currentIndex++){
|
||||
File file = fileList.get(currentIndex);
|
||||
FileSearchItem file = fileList.get(currentIndex);
|
||||
if(recursive && file.isDirectory()){
|
||||
addToFileList(file.listFiles());
|
||||
addFiles(file.listFiles());
|
||||
if(searchFolders && file.getName().equalsIgnoreCase(fileName))
|
||||
break;
|
||||
|
||||
break;
|
||||
}
|
||||
else if(searchCompressedFiles && file.isFile() &&
|
||||
compressedFileExtensions.contains(FileUtil.getFileExtension(file.getName()).toLowerCase())){
|
||||
try {
|
||||
String url = file.getUrl().getFile();
|
||||
ZipFile zipFile = new ZipFile(url);
|
||||
Enumeration<? extends ZipEntry> e = zipFile.entries();
|
||||
while(e.hasMoreElements()){
|
||||
ZipEntry entry = e.nextElement();
|
||||
fileList.add(new FileSearchZipItem(url, entry));
|
||||
}
|
||||
zipFile.close();
|
||||
} catch (IOException e) {
|
||||
e.printStackTrace();
|
||||
}
|
||||
}
|
||||
else if(searchFiles && file.isFile()){
|
||||
if(extension != null && FileUtil.getFileExtension(file).equalsIgnoreCase(extension))
|
||||
if(extension != null && FileUtil.getFileExtension(file.getName()).equalsIgnoreCase(extension))
|
||||
break;
|
||||
else if(fileName != null && file.getName().equalsIgnoreCase(fileName))
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
return ret;
|
||||
}
|
||||
|
||||
private void addToFileList(File[] list){
|
||||
for(File file : list){
|
||||
fileList.add(file);
|
||||
|
||||
private void addFiles(String[] list){
|
||||
for(String file : list){
|
||||
fileList.add(new FileSearchFileItem(new File(file)));
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
interface FileSearchItem{
|
||||
/** @return a file or folder name **/
|
||||
public String getName();
|
||||
/** @return a URL to the file or folder, in case of a compressed file the URL to the package will be returned **/
|
||||
public URL getUrl() throws MalformedURLException ;
|
||||
|
||||
public boolean isCompressed();
|
||||
public boolean isFile();
|
||||
public boolean isDirectory();
|
||||
|
||||
/** @return an InputStream if this is a file otherwise null **/
|
||||
public InputStream getInputStream() throws IOException;
|
||||
/** @return an String array with all files if this is a folder otherwise null **/
|
||||
public String[] listFiles();
|
||||
}
|
||||
|
||||
|
||||
class FileSearchFileItem implements FileSearchItem{
|
||||
private File file;
|
||||
|
||||
protected FileSearchFileItem(File file){
|
||||
this.file = file;
|
||||
}
|
||||
|
||||
public String getName() { return file.getName(); }
|
||||
public URL getUrl() throws MalformedURLException { return new URL(file.getAbsolutePath()); }
|
||||
|
||||
public boolean isCompressed() { return false; }
|
||||
public boolean isFile() { return file.isFile(); }
|
||||
public boolean isDirectory() { return file.isDirectory(); }
|
||||
|
||||
public InputStream getInputStream() throws IOException { return new FileInputStream(file); }
|
||||
public String[] listFiles() { return file.list(); }
|
||||
|
||||
}
|
||||
|
||||
class FileSearchZipItem implements FileSearchItem{
|
||||
private String file;
|
||||
private ZipEntry entry;
|
||||
|
||||
protected FileSearchZipItem(String file, ZipEntry entry){
|
||||
this.file = file;
|
||||
this.entry = entry;
|
||||
}
|
||||
|
||||
public String getName() { return entry.getName(); }
|
||||
public URL getUrl() throws MalformedURLException { return new URL(file); }
|
||||
|
||||
public boolean isCompressed() { return true; }
|
||||
public boolean isFile() { return !entry.isDirectory(); }
|
||||
public boolean isDirectory() { return entry.isDirectory(); }
|
||||
|
||||
public InputStream getInputStream() throws IOException {
|
||||
ZipFile zip = new ZipFile(file);
|
||||
return new InputStreamCloser(zip.getInputStream(entry), zip);
|
||||
}
|
||||
public String[] listFiles() { return null; }
|
||||
|
||||
}
|
||||
Loading…
Add table
Add a link
Reference in a new issue