Commit c75f7718 authored by Tatyana's avatar Tatyana 👽

Merge branch 'feature/archive-scanning-module' into 'master'

Feature/archive scanning module

See merge request !12
parents 73009558 4dc01cca
......@@ -52,7 +52,11 @@
<version>4.12</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.apache.tika</groupId>
<artifactId>tika-core</artifactId>
<version>1.20</version>
</dependency>
<dependency>
<groupId>org.apache.logging.log4j</groupId>
<artifactId>log4j-core</artifactId>
......
......@@ -26,8 +26,6 @@ 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.stream.Collectors;
public class FxAdminController extends FxController {
......@@ -149,6 +147,7 @@ public class FxAdminController extends FxController {
fileSearcher = FileSearcher.getInstance();
Thread fileSearcherThread = new Thread(() -> {
List<File> files = new ArrayList<>(fileSearcher.prepareFiles(root.toPath()));
List<File> peFiles;
......
......@@ -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,14 +114,17 @@ public class FileMonitor extends Thread {
Path name = ev.context();
Path child = dir.resolve(name);
// print out event
logger.info(String.format("%s: %s\n", event.kind().name(), child));
if (event.kind() == ENTRY_CREATE && !child.toAbsolutePath().toFile().isDirectory()) {
fileSearcher.addFile(child.toAbsolutePath().toFile());
UnzipUtil unzipUtil = new UnzipUtil();
File childFile = child.toAbsolutePath().toFile();
if (event.kind() == ENTRY_CREATE && !childFile.isDirectory() && !unzipUtil.isZip(childFile)) {
fileSearcher.addFile(childFile);
}
if (event.kind() == ENTRY_DELETE && !child.toAbsolutePath().toFile().isDirectory()) {
fileSearcher.removeFile(child.toAbsolutePath().toFile());
if (event.kind() == ENTRY_DELETE && !childFile.isDirectory() && !unzipUtil.isZip(childFile)) {
fileSearcher.removeFile(childFile);
}
// if directory is created, and watching recursively, then
......
......@@ -2,10 +2,7 @@ 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.HashSet;
import java.util.Set;
......@@ -16,6 +13,7 @@ public class FileSearcher {
private static FileSearcher instance;
private FileMonitor monitor;
private Set<File> foundedFiles;
private UnzipUtil unzipUtil = new UnzipUtil();
private AtomicInteger filesCounter = new AtomicInteger(0);
......@@ -45,9 +43,15 @@ public class FileSearcher {
* но сейчас нет разницы - обычный это файл или архив
* */
@Override
public FileVisitResult visitFile(Path file, BasicFileAttributes attrs) throws IOException {
foundedFiles.add(file.toFile());
filesCounter.incrementAndGet();
public FileVisitResult visitFile(Path file, BasicFileAttributes attrs){
if(unzipUtil.isZip(file.toFile())) {
Set<File> unzipFiles = unzipUtil.unzipArchive(file.toFile());
foundedFiles.addAll(unzipFiles);
filesCounter.addAndGet(unzipFiles.size());
}else{
foundedFiles.add(file.toFile());
filesCounter.incrementAndGet();
}
return FileVisitResult.CONTINUE;
}
......@@ -57,24 +61,26 @@ public class FileSearcher {
*
*/
@Override
public FileVisitResult visitFileFailed(Path file, IOException exc) throws IOException {
public FileVisitResult visitFileFailed(Path file, IOException exc){
return FileVisitResult.CONTINUE;
}
@Override
public FileVisitResult postVisitDirectory(Path dir, IOException exc) throws IOException {
public FileVisitResult postVisitDirectory(Path dir, IOException exc){
return FileVisitResult.CONTINUE;
}
});
} catch (IOException e) {
System.out.println("ERROR : A problem encountered during walking through directories.");
} finally {
unzipUtil.deleteUnzipFiles();
monitor.finishMonitoring();
}
}
return foundedFiles;
}
public static synchronized FileSearcher getInstance() {
if (instance == null) {
instance = new FileSearcher();
......
package application.util;
import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;
import java.io.*;
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(UnzipUtil.class);
public Set<File> unzipArchive(File archive) {
Set<File> unzipFiles = new HashSet<>();
if (!archive.exists() || !archive.canRead()) {
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!\n");
} else {
unzipDirs.add(0, unzipDir);
}
try (ZipFile zip = new ZipFile(archive)) {
Enumeration entries = zip.entries();
while (entries.hasMoreElements()) {
ZipEntry entry = (ZipEntry) entries.nextElement();
File entryUnzipFile = new File(unzipDir.getParent(), entry.getName().replace("/", "\\"));
if (entry.isDirectory()) {
unzipDirs.add(0, entryUnzipFile);
new File(archive.getParent(), entry.getName()).mkdirs();
} else {
write(zip.getInputStream(entry),
new BufferedOutputStream(new FileOutputStream(entryUnzipFile)));
if (!isZip(entryUnzipFile)) {
unzipFiles.add(entryUnzipFile);
}
}
if (isZip(entryUnzipFile)) {
unzipFiles.addAll(unzipArchive(entryUnzipFile));
}
}
} catch (IOException e) {
logger.error(e.getStackTrace());
}
logger.info("Unzip " + unzipDir + "\n");
return unzipFiles;
}
public boolean isZip(File file) {
if (file == null || !file.exists() && file.isDirectory()) {
return false;
}
String extension;
if (file.getName().contains(".")) {
extension = file.getName().substring(file.getName().indexOf("."));
return extension.equals(".zip") || extension.equals(".zipx") || extension.equals(".gzip");
}
return false;
}
private void write(InputStream inputStream, OutputStream outputStream) throws IOException {
byte[] buffer = new byte[1024];
int len;
while ((len = inputStream.read(buffer)) >= 0)
outputStream.write(buffer, 0, len);
outputStream.close();
inputStream.close();
}
public void deleteUnzipFiles() {
unzipDirs.forEach((dir) ->{
deleteFiles(dir);
if(dir.delete()){
logger.info("Directory " + dir + " was deleted /n");
}
});
unzipDirs.clear();
}
private void deleteFiles(File dir) {
File[] files = dir.listFiles();
// if this dir is not dir OR dir is empty
if (files == null || files.length == 0) {
dir.delete();
return;
}
Arrays.stream(files).forEach((file -> {
if (file.isDirectory() && file.listFiles().length != 0) {
deleteUnzipFiles();
} else {
file.delete();
}
}));
}
}
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