Commit 2f70cd01 authored by Rina Sergeeva's avatar Rina Sergeeva

create UnzipUtil

parent 06b47c1f
......@@ -50,7 +50,7 @@ public class FileSearcher {
return instance;
}
private FileSearcher() {
FileSearcher() {
}
public void addFile(File file) {
......
......@@ -12,45 +12,38 @@ public class UnzipUtil {
public static void unzipArchive(File archive) {
//String archiveDir = archive.getAbsolutePath();
// File destinationDir = new File(destinationPath);
if (!archive.exists() || !archive.canRead()) {
System.out.println("Archive cannot be unzip!");
return;
}
File unzipDir = new File(archive.getParent(), archive.getName().substring(0, archive.getName().lastIndexOf(".")));
unzipDir.mkdirs();
try (ZipFile zip = new ZipFile(archive)) {
Enumeration entries = zip.entries();
while (entries.hasMoreElements()) {
ZipEntry entry = (ZipEntry) entries.nextElement();
System.out.println(entry);
String path = archive.getParent();
if (isZip(new File(entry.getName()))) {
unzipArchive(new File(entry.getName()));
} else if (entry.isDirectory()) {
File entryUnzipFile = new File(unzipDir.getParent(),entry.getName().replace("/","\\"));
if (entry.isDirectory()) {
new File(archive.getParent(), entry.getName()).mkdirs();
} else {
//TODO
write(zip.getInputStream(entry),
new BufferedOutputStream(new FileOutputStream(
new File(archive.getParent(), entry.getName()))));
new BufferedOutputStream(new FileOutputStream(entryUnzipFile)));
}
}
} catch (IOException e) {
e.printStackTrace();
}
}
public static boolean isZip(File file) {
static boolean isZip(File file) {
MimeUtil.registerMimeDetector("eu.medsea.mimeutil.detector.MagicMimeMimeDetector");
Collection<?> mimeTypes = MimeUtil.getMimeTypes(file);
System.out.println(mimeTypes);
return true;
String mimeType = mimeTypes.toString();
return mimeType.equals("application/zip") || mimeType.equals("application/gzip") ||
mimeType.equals("application/x-rar-compressed");
}
private static void write(InputStream inputStream, OutputStream outputStream) throws IOException {
......
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