Repository navigation
Expand file tree
/
Copy pathSolution.java
More file actions
113 lines (98 loc) · 4.13 KB
/
Copy pathSolution.java
File metadata and controls
113 lines (98 loc) · 4.13 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
112
113
//Greet Asyncritus, Phlegon, Hermes, Patrobas, Hermas, and the brothers who are with them (Romans 16:14)
package com.javarush.task.task21.task2110;
import java.io.BufferedWriter;
import java.io.IOException;
import java.nio.charset.Charset;
import java.nio.charset.StandardCharsets;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.util.Enumeration;
import java.util.zip.ZipEntry;
import java.util.zip.ZipFile;
/*
Рефакторинг методов
*/
public class Solution {
public static void writeZipEntriesToFile(String zipFileName, String outputFileName) {
Charset charset = StandardCharsets.UTF_8;
Path outputFilePath = Paths.get(outputFileName);
try {
try(ZipFile zip = new ZipFile(zipFileName);
BufferedWriter writer = Files.newBufferedWriter(outputFilePath, charset);) {
String newLine = System.getProperty("line.separator");
for (Enumeration entries = zip.entries(); entries.hasMoreElements(); ) {
// Берем имя файла из архива и записываем его в результирующий файл
// Get the entry name and write it to the output file
String zipEntryName = ((ZipEntry) entries.nextElement()).getName() + newLine;
writer.write(zipEntryName, 0, zipEntryName.length());
}
}
} catch (IOException e) {
e.printStackTrace();
}
}
public static void main(String[] args) {
}
}
/*
Рефакторинг методов
Отрефакторите метод writeZipEntriesToFile в соответствии с java7 try-with-resources.
Требования:
1. Метод writeZipEntriesToFile должен быть объявлен с модификатором доступа public.
2. Метод writeZipEntriesToFile должен быть статическим.
3. Тип возвращаемого значения метода writeZipEntriesToFile должен быть void.
4. Метод writeZipEntriesToFile должен корректно использовать try-with-resources. В программе не должно быть закомментированного кода.
package com.javarush.task.task21.task2110;
import java.io.BufferedWriter;
import java.io.IOException;
import java.nio.charset.Charset;
import java.nio.charset.StandardCharsets;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.util.Enumeration;
import java.util.zip.ZipEntry;
import java.util.zip.ZipFile;
*
Рефакторинг методов
*
public class Solution {
public static void writeZipEntriesToFile(String zipFileName, String outputFileName) {
Charset charset = StandardCharsets.UTF_8;
Path outputFilePath = Paths.get(outputFileName);
BufferedWriter writer = null;
ZipFile zip = null;
try {
zip = new ZipFile(zipFileName);
writer = Files.newBufferedWriter(outputFilePath, charset);
String newLine = System.getProperty("line.separator");
for (Enumeration entries = zip.entries(); entries.hasMoreElements(); ) {
// Берем имя файла из архива и записываем его в результирующий файл
// Get the entry name and write it to the output file
String zipEntryName = ((ZipEntry) entries.nextElement()).getName() + newLine;
writer.write(zipEntryName, 0, zipEntryName.length());
}
} catch (IOException e) {
e.printStackTrace();
} finally {
if (writer != null) {
try {
writer.close();
} catch (IOException e1) {
e1.printStackTrace();
}
}
if (zip != null) {
try {
zip.close();
} catch (IOException e1) {
e1.printStackTrace();
}
}
}
}
public static void main(String[] args) {
}
}
*/