package com.javarush.task.task31.task3106;
import java.io.*;
import java.nio.file.Files;
import java.nio.file.Path;
import java.util.Arrays;
import java.util.Comparator;
import java.util.function.Supplier;
import java.util.stream.Collector;
import java.util.stream.Collectors;
import java.util.zip.ZipEntry;
import java.util.zip.ZipInputStream;
/*
Разархивируем файл
*/
public class Solution {
public static void main(String[] args) {
String [] zips = new String[args.length-1];
for (int i = 0; i < zips.length; i++) {
zips[i] = args[i+1];
}
zips = sortedArguments(zips);
// Arrays.stream(zips).forEach(System.out::println);
try (FileOutputStream out = new FileOutputStream(args[0], true);)
{
for (int i = 0; i < zips.length; i++) {
try (ZipInputStream in = new ZipInputStream(new FileInputStream(zips[i])))
{
ZipEntry zip = in.getNextEntry();
byte[] bytes = new byte[1024];
long b = zip.getSize(), a = 0;
while (b > a) {
int x = in.read(bytes);
a += x;
out.write(bytes, 0, x);
}
in.closeEntry();
}
catch (Exception e){
e.printStackTrace();
}
}
}
catch (Exception e){
e.printStackTrace();
}
}
public static String[] sortedArguments(String ... args){
Comparator<String> comparator = new Comparator<String>() {
@Override
public int compare(String o1, String o2) {
int a1 = Integer.parseInt(o1.substring(o1.lastIndexOf(".")+1));
int a2 = Integer.parseInt(o2.substring(o2.lastIndexOf(".")+1));
return a1-a2;
}
};
return Arrays.stream(args).sorted(comparator).map(o1 -> o1.substring(0,o1.lastIndexOf("."))).toArray(String[]::new);
}
}