package ru.job4j.threads;

import java.io.*;
import java.util.List;
import java.util.zip.ZipEntry;
import java.util.zip.ZipOutputStream;

/**
 * Class PackZip.
 * @author  shustovakv
 * @since 06/02/2019
 */
public class PackZip {

    public void zip(Args args) throws IOException {
        String zip_file = args.output();
        ZipOutputStream zout = null;
        try {
            zout = new ZipOutputStream(new FileOutputStream(args.directory() + "//" + zip_file));
        } catch (FileNotFoundException e) {
            e.printStackTrace();
        }
        File fileSource = new File(args.directory());
        List<String> exc = args.exclude();
        this.addDirectory(zout, fileSource, exc);
    }

    private void addDirectory(ZipOutputStream zout, File fileSource, List<String> exc) throws IOException {

        File[] files = fileSource.listFiles();
        for (int i = 0; i < files.length; i++) {
            if (files[i].isDirectory()) {
                addDirectory(zout, files[i],exc);
                continue;
            } if (exclude(files[i], exc)) {
                FileInputStream fis = new FileInputStream(files[i]);
                zout.putNextEntry(new ZipEntry(files[i].getPath()));

                byte[] buffer = new byte[2048];
                int length;
                while((length = fis.read(buffer)) > 0) {
                    zout.write(buffer, 0, length);
                }
                zout.closeEntry();
                fis.close();

            }
        }
    }

    private boolean exclude(File file, List<String> ext) {
        boolean res = true;
        if (ext != null) {
            for (String value : ext) {
                if (file.getName().endsWith(value)) {
                    res = false;
                    break;
                }
            }
        }
        return res;
    }
}

@@ -54,17 +54,117 @@
 ava.
-util.List
+net.URI;%0Aimport java.util.LinkedList;%0Aimport java.util.List;%0Aimport java.util.Objects;%0Aimport java.util.Queue
 ;%0Aim
@@ -355,35 +355,16 @@
 gs args)
- throws IOException
  %7B%0A     
@@ -407,16 +407,21 @@
         
+try (
 ZipOutpu
@@ -438,47 +438,8 @@
 ut =
- null;%0A        try %7B%0A            zout =
  new
@@ -516,97 +516,16 @@
 le))
-;%0A        %7D catch (FileNotFoundException e) %7B%0A            e.printStackTrace();%0A        %7D%0A
+) %7B%0A    
     
@@ -574,16 +574,20 @@
 ory());%0A
+    
         
@@ -629,16 +629,20 @@
         
+    
 this.add
@@ -671,24 +671,186 @@
 urce, exc);%0A
+        %7D%0A        catch (FileNotFoundException e) %7B%0A            e.printStackTrace();%0A        %7D catch (IOException e) %7B%0A            e.printStackTrace();%0A        %7D%0A
     %7D%0A%0A    p
@@ -952,25 +952,24 @@
 ption %7B%0A
-%0A
         
 File%5B%5D f
@@ -964,214 +964,668 @@
     
-File%5B%5D files = fileSource.listFiles();%0A        for (int i = 0; i %3C files.length; i++) %7B%0A            if (files%5Bi%5D.isDirectory()) %7B%0A                addDirectory(zout, files%5Bi%5D,exc);%0A                continue;%0A
+Queue%3CFile%3E queue = new LinkedList%3C%3E();%0A        queue.offer(fileSource);%0A        URI base = fileSource.toURI();%0A        while (!queue.isEmpty()) %7B%0A            File%5B%5D files = Objects.requireNonNull(queue.poll()).listFiles();%0A            assert files != null;%0A            for (int i = 0; i %3C files.length; i++) %7B%0A                final File file = files%5Bi%5D;%0A                String name = base.relativize(file.toURI()).getPath();%0A                if (file.isDirectory()) %7B%0A                    name = name.endsWith(%22/%22) ? name : name + %22/%22;%0A                    zout.putNextEntry(new ZipEntry(name));%0A                    ((LinkedList%3CFile%3E) queue).addFirst(file);%0A   
     
@@ -1632,17 +1632,23 @@
         
-%7D
+ %7D else
  if (exc
@@ -1660,22 +1660,60 @@
 file
-s%5Bi%5D
 , exc)
-) %7B%0A
+ && !(file.getName().contains(%22.zip%22))) %7B%0A    
     
@@ -1774,15 +1774,15 @@
 file
-s%5Bi%5D);%0A
+);%0A    
     
@@ -1828,26 +1828,12 @@
 try(
-files%5Bi%5D.getPath()
+name
 ));%0A
@@ -1849,16 +1849,20 @@
         
+ 
+   
 byte%5B%5D b
@@ -1901,16 +1901,20 @@
         
+    
 int leng
@@ -1929,24 +1929,28 @@
             
+ 
+   
 while((lengt
@@ -1974,24 +1974,28 @@
 er)) %3E 0) %7B%0A
+    
             
@@ -2041,34 +2041,42 @@
                 
-%7D%0A
+    %7D%0A    
                 
@@ -2110,16 +2110,20 @@
         
+    
 fis.clos
@@ -2127,16 +2127,33 @@
 lose();%0A
+                %7D
 %0A       

Архивировать проект. [#861]