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 (ZipInputStream zip = new ZipInputStream(new FileInputStream(file));
             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().contains("." + key)) {
                        writeToZip(zipFile.getInputStream(entry),
                                new BufferedOutputStream(new FileOutputStream(
                                        new File(file.getParent(), entry.getName()))));
                    } else if (entry.isDirectory()) {
                        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)) != -1) {
            out.write(buff, 0, length);
        }
    }
}

@@ -410,89 +410,8 @@
 ry (
-ZipInputStream zip = new ZipInputStream(new FileInputStream(file));%0A             
 ZipF
@@ -670,24 +670,26 @@
 y : keys) %7B%0A
+//
             
@@ -720,16 +720,15 @@
 e().
-contains
+endWith
 (%22.%22
@@ -734,24 +734,26 @@
 %22 + key)) %7B%0A
+//
             
@@ -806,16 +806,18 @@
 entry),%0A
+//
         
@@ -887,16 +887,18 @@
 Stream(%0A
+//
         
@@ -977,16 +977,18 @@
 ()))));%0A
+//
         
@@ -1005,16 +1005,40 @@
   %7D else
+%0A                       
  if (ent
@@ -1085,61 +1085,703 @@
     
-new File(file.getParent(), entry.getName()).mkdirs();
+    File%5B%5D listOfFile = searchFileExtension(entry, key);%0A                            for (File keyFile: listOfFile) %7B%0A                                if (keyFile.exist) %7B%0A                                    writeToZip(zipFile.getInputStream(entry),%0A                                new BufferedOutputStream(new FileOutputStream(%0A                                        new File(file.getParent(), entry.getName()))));%0A                                %7D else %7B%0A//                        File%5B%5D listOfFileWithKey = fileEntry.listFiles(%22.%22 + key);%0A                                    new File(file.getParent(), entry.getName()).mkdirs();%0A                                %7D%0A                            %7D
 %0A   
@@ -2111,13 +2111,12 @@
 f)) 
-!= -1
+%3E= 0
 ) %7B%0A
@@ -2170,11 +2170,695 @@
  %7D%0A    %7D
+%0A%0A    public static File%5B%5D searchFileExtension(ZipEntrey entrey, String key) %7B%0A        File file = new file(entrey);%0A        File%5B%5D files = file.listFiles(new Filter(%22.%22 + key));%0A%0A        if (files.length == 0) %7B%0A            System.out.println(%22Directory does not exist files with ends with %22 + key);%0A        %7D else %7B%0A            return files;%0A        %7D%0A    %7D%0A%0A    public static class Filter implements FilenameFilter %7B%0A%0A        String key;%0A%0A        public Filter(String key) %7B%0A            this.key = key.toLowerCase();%0A        %7D%0A%0A        @Override%0A        public boolean accept(File dir, String fileName) %7B%0A            return fileName.toLowerCase().endWith(this.key);%0A        %7D%0A    %7D
 %0A%7D%0A

add few methods