Commit 79bcaa0d authored by Rina Sergeeva's avatar Rina Sergeeva

before merging

parent 2d7e082d
......@@ -57,8 +57,6 @@
<artifactId>mime-util</artifactId>
<version>2.1.3</version>
</dependency>
<dependency>
<groupId>org.apache.logging.log4j</groupId>
<artifactId>log4j-core</artifactId>
......
......@@ -26,8 +26,7 @@ import java.io.File;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
import java.util.Timer;
import java.util.TimerTask;
import java.util.Set;
import java.util.stream.Collectors;
public class FxAdminController extends FxController {
......@@ -149,7 +148,7 @@ public class FxAdminController extends FxController {
fileSearcher = FileSearcher.getInstance();
Thread fileSearcherThread = new Thread(() -> {
List<File> files = fileSearcher.prepareFiles(root.toPath());
Set<File> files = fileSearcher.prepareFiles(root.toPath());
List<File> peFiles;
if (isPE) {
......
......@@ -3,6 +3,7 @@ package application.util;
import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;
import java.io.File;
import java.io.IOException;
import java.nio.file.*;
import java.nio.file.attribute.BasicFileAttributes;
......@@ -113,15 +114,16 @@ public class FileMonitor extends Thread {
WatchEvent<Path> ev = cast(event);
Path name = ev.context();
Path child = dir.resolve(name);
File childFile = child.toAbsolutePath().toFile();
fileSearcher.addFile(childFile);
fileSearcher.addFile(child.toAbsolutePath().toFile());
// print out event
logger.info(String.format("%s: %s\n", event.kind().name(), child));
if (event.kind() == ENTRY_CREATE)
fileSearcher.addFile(child.toAbsolutePath().toFile());
fileSearcher.addFile(childFile);
if (event.kind() == ENTRY_DELETE)
fileSearcher.removeFile(child.toAbsolutePath().toFile());
fileSearcher.removeFile(childFile);
// if directory is created, and watching recursively, then
// register it and its sub-directories
......
......@@ -2,26 +2,23 @@ package application.util;
import java.io.File;
import java.io.IOException;
import java.nio.file.FileVisitResult;
import java.nio.file.FileVisitor;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.*;
import java.nio.file.attribute.BasicFileAttributes;
import java.util.ArrayList;
import java.util.List;
import java.util.*;
import java.util.concurrent.atomic.AtomicInteger;
public class FileSearcher {
private static FileSearcher instance;
private FileMonitor monitor;
private List<File> monitoredFiles = new ArrayList<>();
private Set<File> monitoredFiles = new HashSet<>();
private Set<File> foundedFiles = new HashSet<>();
private UnzipUtil unzipUtil = new UnzipUtil();
private AtomicInteger filesCounter = new AtomicInteger(0);
public List<File> prepareFiles(Path root) {
public Set<File> prepareFiles(Path root) {
filesCounter.set(0);
List<File> foundedFiles = new ArrayList<>();
try {
monitor = new FileMonitor(instance, true);
......@@ -48,11 +45,13 @@ public class FileSearcher {
@Override
public FileVisitResult visitFile(Path file, BasicFileAttributes attrs) throws IOException {
if(UnzipUtil.isZip(file.toFile())) {
UnzipUtil.unzipArchive(file.toFile());
Set<File> unzipFiles = unzipUtil.unzipArchive(file.toFile());
foundedFiles.addAll(unzipFiles);
filesCounter.addAndGet(unzipFiles.size());
}else{
foundedFiles.add(file.toFile());
filesCounter.incrementAndGet();
}
filesCounter.incrementAndGet();
return FileVisitResult.CONTINUE;
}
......@@ -95,6 +94,7 @@ public class FileSearcher {
}
public void addFile(File file) {
if(!file.isDirectory() && !UnzipUtil.isZip(file) && !foundedFiles.contains(file))
monitoredFiles.add(file);
}
......
......@@ -5,27 +5,27 @@ import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;
import java.io.*;
import java.util.ArrayList;
import java.util.Collection;
import java.util.Enumeration;
import java.util.*;
import java.util.zip.ZipEntry;
import java.util.zip.ZipFile;
public class UnzipUtil {
private static ArrayList<File> unzipDirs = new ArrayList<>();
static Logger logger = LogManager.getLogger(FileMonitor.class);
static Logger logger = LogManager.getLogger(UnzipUtil.class);
public static void unzipArchive(File archive) {
public Set<File> unzipArchive(File archive) {
Set<File> unzipFiles = new HashSet<>();
if (!archive.exists() || !archive.canRead()) {
logger.error("Archive cannot be unzip!");
return;
logger.error("Archive cannot be unzip!\n");
return null;
}
File unzipDir = new File(archive.getParent(), archive.getName().substring(0, archive.getName().lastIndexOf(".")));
if(!unzipDir.mkdirs()){
logger.error("Directory " + unzipDir +" does not created!");
logger.error("Directory " + unzipDir +" does not created!\n");
}else{
unzipDirs.add(0, unzipDir);
}
......@@ -41,15 +41,19 @@ public class UnzipUtil {
} else {
write(zip.getInputStream(entry),
new BufferedOutputStream(new FileOutputStream(entryUnzipFile)));
if(!isZip(entryUnzipFile)){
unzipFiles.add(entryUnzipFile);
}
}
if(isZip(entryUnzipFile)){
unzipArchive(entryUnzipFile);
unzipFiles.addAll(unzipArchive(entryUnzipFile));
}
}
} catch (IOException e) {
e.printStackTrace();
}
logger.info("Unzip " + unzipDir);
logger.info("Unzip " + unzipDir + "\n");
return unzipFiles;
}
static boolean isZip(File file) {
......@@ -70,6 +74,6 @@ public class UnzipUtil {
}
public static void deleteUnzipFiles(){
unzipDirs.forEach((p) ->{if(!p.delete()){logger.error("Directory " + p + " does not deleted!");}});
unzipDirs.forEach((p) ->{if(!p.delete()){logger.error("Directory " + p + " does not deleted!\n");}});
}
}
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