package ru.matevosyan;

import java.io.*;
import java.util.Enumeration;
import java.util.zip.ZipEntry;
import java.util.zip.ZipFile;
import java.util.zip.ZipInputStream;

/**
 * Created for zipping file from archive.
 * Created on 19.02.2017.
 * @author Matevosyan Vardan
 * @version 1.0
 */

public class zipWithStructure {

    public void zipping(File file, String ... keys) throws IOException {

        try (ZipFile zipFile = new ZipFile(file)) {
            ZipEntry entry;
            Enumeration entries = zipFile.entries();
            while (entries.hasMoreElements()) {
                entry = (ZipEntry) entries.nextElement();
                for (String key : keys) {
//                    if (entry.getName().endWith("." + key)) {
//                        writeToZip(zipFile.getInputStream(entry),
//                                new BufferedOutputStream(new FileOutputStream(
//                                        new File(file.getParent(), entry.getName()))));
//                    } else
                        if (entry.isDirectory()) {
                            File[] listOfFile = searchFileExtension(entry, key);
                            for (File keyFile: listOfFile) {
                                if (keyFile.exist) {
                                    writeToZip(zipFile.getInputStream(entry),
                                new BufferedOutputStream(new FileOutputStream(
                                        new File(file.getParent(), entry.getName()))));
                                } else {
//                        File[] listOfFileWithKey = fileEntry.listFiles("." + key);
                                    new File(file.getParent(), entry.getName()).mkdirs();
                                }
                            }
                    }
                }
            }
        }  catch (FileNotFoundException fnfe) {
            fnfe.getMessage();
        }
    }

    public void writeToZip(InputStream in, OutputStream out) throws IOException {
        byte[] buff = new byte[1024];
        int length;
        while ((length = in.read(buff)) >= 0) {
            out.write(buff, 0, length);
        }
    }

    public static File[] searchFileExtension(ZipEntrey entrey, String key) {
        File file = new file(entrey);
        File[] files = file.listFiles(new Filter("." + key));

        if (files.length == 0) {
            System.out.println("Directory does not exist files with ends with " + key);
        } else {
            return files;
        }
    }

    public static class Filter implements FilenameFilter {

        String key;

        public Filter(String key) {
            this.key = key.toLowerCase();
        }

        @Override
        public boolean accept(File dir, String fileName) {
            return fileName.toLowerCase().endWith(this.key);
        }
    }
}

@@ -126,16 +126,65 @@
 ipFile;%0A
+import java.nio.file.Files;%0Aimport java.io.File;%0A
 import j
@@ -1297,16 +1297,19 @@
 le.exist
+s()
 ) %7B%0A    
@@ -1702,32 +1702,43 @@
                 
+ File dir =
  new File(file.g
@@ -1765,16 +1765,57 @@
 tName())
+;%0A                                    dir
 .mkdirs(
@@ -2332,16 +2332,14 @@
 Entr
-e
 y entr
-e
 y, S
@@ -2379,19 +2379,34 @@
 new 
-f
+F
 ile(
+String.valueOf(
 entr
-e
 y
+)
 );%0A 
@@ -2471,24 +2471,28 @@
 );%0A%0A        
+    
 if (files.le
@@ -2500,24 +2500,28 @@
 gth == 0) %7B%0A
+    
             
@@ -2596,32 +2596,36 @@
 + key);%0A        
+    
 %7D else %7B%0A       
@@ -2617,16 +2617,20 @@
  else %7B%0A
+    
         
@@ -2647,33 +2647,38 @@
  files;%0A        
-%7D
+    %7D%0A
 %0A    %7D%0A%0A    publ
@@ -2957,24 +2957,25 @@
 erCase().end
+s
 With(this.ke

change mist