|
| 1 | + |
| 2 | +//And I heard a great voice out of heaven saying, Behold, the tabernacle of God is with men, |
| 3 | +//and he will dwell with them, and they shall be his people, and God himself shall be with them, and be their God. (Revelation 21:3) |
| 4 | + |
| 5 | +public class Solution { |
| 6 | + public static void main(String[] args) throws IOException { |
| 7 | + // Это решение позаимствовано отсюда |
| 8 | + // https://www.snip2code.com/Snippet/1178691/Level-31--Lesson-06--Bonus-01 |
| 9 | + |
| 10 | + String resultFileName = args[0]; |
| 11 | + int filePartCount = args.length - 1; |
| 12 | + String[] fileNamePart = new String[filePartCount]; |
| 13 | + for (int i = 0; i < filePartCount; i++) { |
| 14 | + fileNamePart[i] = args[i + 1]; |
| 15 | + } |
| 16 | + Arrays.sort(fileNamePart); |
| 17 | + |
| 18 | + List<FileInputStream> fisList = new ArrayList<>(); |
| 19 | + for (int i = 0; i < filePartCount; i++) { |
| 20 | + fisList.add(new FileInputStream(fileNamePart[i])); |
| 21 | + } |
| 22 | + SequenceInputStream seqInStream = new SequenceInputStream(Collections.enumeration(fisList)); |
| 23 | + ZipInputStream zipInStream = new ZipInputStream(seqInStream); |
| 24 | + FileOutputStream fileOutStream = new FileOutputStream(resultFileName); |
| 25 | + byte[] buf = new byte[1024 * 1024]; |
| 26 | + while (zipInStream.getNextEntry() != null) { |
| 27 | + int count; |
| 28 | + while ((count = zipInStream.read(buf)) != -1) { |
| 29 | + fileOutStream.write(buf, 0, count); |
| 30 | + } |
| 31 | + } |
| 32 | + seqInStream.close(); |
| 33 | + zipInStream.close(); |
| 34 | + fileOutStream.close(); |
| 35 | + } |
| 36 | +/* |
| 37 | + //Мое работающее решение не принятое валидатором |
| 38 | + Path pathToResultFile = Paths.get (args[0]); |
| 39 | + Path pathToArchiveFile = Paths.get(pathToResultFile.toString() + ".zip"); |
| 40 | + //Fill and sort list of part |
| 41 | + TreeSet <String> fileNamesParts = new TreeSet<>(new Sort()); |
| 42 | + for (int i=1; i<args.length;i++) |
| 43 | + fileNamesParts.add(args[i]); |
| 44 | + //Create ZIP Archive |
| 45 | + if (Files.exists(pathToArchiveFile)) |
| 46 | + Files.delete(pathToArchiveFile); |
| 47 | + Files.createFile(pathToArchiveFile); |
| 48 | + //Copy parts to Arhive |
| 49 | + for (String parts :fileNamesParts) |
| 50 | + Files.write(pathToArchiveFile, Files.readAllBytes(Paths.get(parts)), StandardOpenOption.APPEND); |
| 51 | + //Unzip |
| 52 | + ZipInputStream zip = new ZipInputStream(new FileInputStream(pathToArchiveFile.toFile())); |
| 53 | + ZipEntry zipEntry = zip.getNextEntry(); |
| 54 | + if (zipEntry !=null) |
| 55 | + Files.copy(zip, pathToResultFile); |
| 56 | + zip.closeEntry(); |
| 57 | + zip.close(); |
| 58 | + }*/ |
| 59 | +} |
| 60 | + |
| 61 | +class Sort implements Comparator<String> { |
| 62 | + public int compare(String a, String b) { |
| 63 | + int a1 = Integer.parseInt(a.substring(a.lastIndexOf(".") + 1)); |
| 64 | + int b1 = Integer.parseInt(b.substring(b.lastIndexOf(".") + 1)); |
| 65 | + return a.compareTo(b); |
| 66 | + } |
| 67 | +} |
| 68 | + |
| 69 | +/* |
| 70 | +Разархивируем файл |
| 71 | +
|
| 72 | +В метод main приходит список аргументов. |
| 73 | +
|
| 74 | +Первый аргумент - имя результирующего файла resultFileName, остальные аргументы - имена файлов fileNamePart. |
| 75 | +
|
| 76 | +Каждый файл (fileNamePart) - это кусочек zip архива. Нужно разархивировать целый файл, собрав его из кусочков. |
| 77 | +
|
| 78 | +Записать разархивированный файл в resultFileName. |
| 79 | +
|
| 80 | +Архив внутри может содержать файл большой длины, например, 50Mb. |
| 81 | +
|
| 82 | +Внутри архива может содержаться файл с любым именем. |
| 83 | +
|
| 84 | +
|
| 85 | +
|
| 86 | +Пример входных данных. Внутри архива находится один файл с именем abc.mp3: |
| 87 | +
|
| 88 | +C:/result.mp3 |
| 89 | +
|
| 90 | +C:/pathToTest/test.zip.003 |
| 91 | +
|
| 92 | +C:/pathToTest/test.zip.001 |
| 93 | +
|
| 94 | +C:/pathToTest/test.zip.004 |
| 95 | +
|
| 96 | +C:/pathToTest/test.zip.002 |
| 97 | +
|
| 98 | +
|
| 99 | +
|
| 100 | +
|
| 101 | +
|
| 102 | +Требования: |
| 103 | +
|
| 104 | +1. В методе main нужно создать ZipInputStream для архива, собранного из кусочков файлов. Файлы приходят аргументами в main, начиная со второго. |
| 105 | +
|
| 106 | +2. Создай поток для записи в файл, который приходит первым аргументом в main. Запиши туда содержимое файла из архива. |
| 107 | +
|
| 108 | +3. Поток для чтения из архива должен быть закрыт. |
| 109 | +
|
| 110 | +4. Поток для записи в файл должен быть закрыт. |
| 111 | +*/ |
0 commit comments