Commit 4dc01cca authored by Rina Sergeeva's avatar Rina Sergeeva

fix all bugs

parent da49ff72
......@@ -73,6 +73,7 @@ public class FileSearcher {
} catch (IOException e) {
System.out.println("ERROR : A problem encountered during walking through directories.");
} finally {
unzipUtil.deleteUnzipFiles();
monitor.finishMonitoring();
}
}
......
......@@ -4,10 +4,7 @@ import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;
import java.io.*;
import java.util.ArrayList;
import java.util.Enumeration;
import java.util.HashSet;
import java.util.Set;
import java.util.*;
import java.util.zip.ZipEntry;
import java.util.zip.ZipFile;
......@@ -39,6 +36,7 @@ public class UnzipUtil {
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),
......@@ -59,11 +57,15 @@ public class UnzipUtil {
}
public boolean isZip(File file) {
if(file.isDirectory()){
if (file == null || !file.exists() && file.isDirectory()) {
return false;
}
String extension = file.getName().substring(file.getName().indexOf("."));
return extension.equals(".zip") || extension.equals(".zipx") || extension.equals(".gzip") || extension.equals(".rar");
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 {
......@@ -76,12 +78,31 @@ public class UnzipUtil {
}
public void deleteUnzipFiles() {
unzipDirs.forEach((p) -> {
if (!p.delete()) {
logger.error("Directory " + p + " does not deleted!\n");
}else{
unzipDirs.remove(p);
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