Commit 42dec140 authored by Rina Sergeeva's avatar Rina Sergeeva

before merging

parent 2f70cd01
......@@ -3,30 +3,42 @@ package application.util;
import java.io.File;
import java.io.IOException;
import java.nio.file.*;
import java.util.ArrayList;
import static java.nio.file.StandardWatchEventKinds.*;
public class FileMonitor extends Thread {
private FileSearcher fileSearcher;
private WatchService watcher;
private WatchService watcher = FileSystems.getDefault().newWatchService();
private boolean proceed = false;
private ArrayList<WatchKey> keys = new ArrayList<>();
public FileMonitor() {
try {
watcher = FileSystems.getDefault().newWatchService();
} catch (IOException e) {
private static FileMonitor instance;
private FileMonitor() throws IOException {
}
public static synchronized FileMonitor getInstance() {
if (instance == null) {
try {
instance = new FileMonitor();
} catch (IOException e) {
System.out.println("Error while initializing monitor occured");
return null;
}
}
return instance;
}
public void registerDir(File newDir) {
proceed = true;
try {
Path dir = Paths.get(newDir.getAbsolutePath());
dir.register(watcher,
keys.add(dir.register(watcher,
ENTRY_CREATE,
ENTRY_DELETE
/*ENTRY_MODIFY*/);
/*ENTRY_MODIFY*/));
} catch (IOException x) {
System.err.println(x);
}
......@@ -34,6 +46,11 @@ public class FileMonitor extends Thread {
public void stopMonitoring() {
proceed = false;
for (WatchKey key : keys) {
key.cancel();
}
keys.clear();
System.out.println("Stop monitor thread!");
}
public void run() {
......
......@@ -12,14 +12,22 @@ public class FileSearcher {
private int filesFound;
public List<File> prepareFiles(File root) {
public List<File> prepareFiles(File root) {
filesFound = 0;
monitor = new FileMonitor();
monitor.start();
monitor.registerDir(root);
List<File> foundedFiles = scanDirectory(root);
foundedFiles.addAll(monitoredFiles);
monitor.stopMonitoring();
List<File> foundedFiles = null;
monitor = FileMonitor.getInstance();
if (monitor != null) {
monitor.setName("Monitor Thread");
monitor.start();
monitor.registerDir(root);
foundedFiles = scanDirectory(root);
foundedFiles.addAll(monitoredFiles);
monitor.stopMonitoring();
}
return foundedFiles;
}
......
......@@ -3,6 +3,7 @@ package application.util;
import eu.medsea.mimeutil.MimeUtil;
import java.io.*;
import java.util.ArrayList;
import java.util.Collection;
import java.util.Enumeration;
import java.util.zip.ZipEntry;
......@@ -10,6 +11,8 @@ import java.util.zip.ZipFile;
public class UnzipUtil {
private static ArrayList<File> unzipDirs = new ArrayList<>();
public static void unzipArchive(File archive) {
if (!archive.exists() || !archive.canRead()) {
......@@ -18,7 +21,11 @@ public class UnzipUtil {
}
File unzipDir = new File(archive.getParent(), archive.getName().substring(0, archive.getName().lastIndexOf(".")));
unzipDir.mkdirs();
if(!unzipDir.mkdirs()){
System.out.println("Directory " + unzipDir +" does not created!");
}else{
unzipDirs.add(unzipDir);
}
try (ZipFile zip = new ZipFile(archive)) {
Enumeration entries = zip.entries();
......@@ -54,4 +61,8 @@ public class UnzipUtil {
outputStream.close();
inputStream.close();
}
public static void deleteUnzipFiles(){
unzipDirs.forEach((p) ->{if(!p.delete()){System.out.print("Directory " + p + " does not deleted!");}});
}
}
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment