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