Skip to content

Commit b120e3b

Browse files
committed
Path zamiast File w AutoUpdateTask
1 parent d80238c commit b120e3b

File tree

4 files changed

+43
-59
lines changed

4 files changed

+43
-59
lines changed

eMetrykant.iml

Lines changed: 0 additions & 21 deletions
This file was deleted.

src/main/java/pl/koder95/eme/Files.java

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,8 @@
1818
package pl.koder95.eme;
1919

2020
import java.io.File;
21+
import java.nio.file.Path;
22+
import java.nio.file.Paths;
2123

2224
/**
2325
* Przechowuje dane na temat plików.
@@ -48,11 +50,11 @@ public final class Files {
4850
*/
4951
public static final File INDICES_XML = new File(Files.XML_DIR, "indices.xml");
5052

51-
public static final File TEMP_DIR = new File(System.getProperty("java.io.tmpdir"), "eMetrykant");
53+
public static final Path TEMP_DIR = Paths.get(System.getProperty("java.io.tmpdir"), "eMetrykant");
5254

5355
public static final File UPDATE_WIN = new File(Files.WORKDIR, "update.bat");
5456
public static final File UPDATE_UNIX = new File(Files.WORKDIR, "update");
55-
public static final File SELF = new File(Files.WORKDIR, "eMetrykant.jar");
57+
public static final Path SELF = WORKDIR.toPath().resolve("eMetrykant.jar");
5658

5759
private Files() {}
5860
}

src/main/java/pl/koder95/eme/au/AutoUpdateTask.java

Lines changed: 38 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@
1313
import java.nio.channels.Channels;
1414
import java.nio.channels.FileChannel;
1515
import java.nio.channels.ReadableByteChannel;
16+
import java.nio.file.Path;
1617
import java.text.NumberFormat;
1718
import java.util.Enumeration;
1819
import java.util.HashMap;
@@ -43,61 +44,63 @@ protected Object call() throws Exception {
4344
}
4445

4546
// pobieranie zip'a
46-
File downloaded = downloadZip(zip.getBrowserDownloadUrl(), TEMP_DIR, zip.getName(), zip.getSize());
47+
Path downloaded = downloadZip(zip.getBrowserDownloadUrl(), TEMP_DIR, zip.getName(), zip.getSize());
4748
// rozpakowywanie do folderu tymczasowego
4849
if (extractZip(downloaded, TEMP_DIR, true)) {
4950
// TODO: tworzenie mapy aktualizacji
50-
Map<File, File> updateMap = new HashMap<>();
51-
updateMap.put(new File(TEMP_DIR, "eMetrykant.jar"), SELF);
51+
Map<Path, Path> updateMap = new HashMap<>();
52+
updateMap.put(TEMP_DIR.resolve("eMetrykant.jar"), SELF);
53+
5254
restart(updateMap);
5355
}
5456
return null;
5557
}
5658

57-
private File downloadZip(String url, File dir, String name, long size) throws IOException {
59+
private Path downloadZip(String url, Path dir, String name, long size) throws IOException {
5860
NumberFormat pF = NumberFormat.getPercentInstance();
5961
String title = "Pobieranie " + name;
6062
updateTitle(title);
6163
updateProgress(0, 1);
6264

6365
URLConnection connection = new URL(url).openConnection();
64-
File forDownload = new File(dir, name);
66+
Path forDownload = dir.resolve(name);
67+
java.nio.file.Files.createFile(forDownload);
6568

6669
try (InputStream in = connection.getInputStream();
6770
ReadableByteChannel rbc = Channels.newChannel(in);
68-
FileOutputStream out = new FileOutputStream(forDownload);
69-
FileChannel channel = out.getChannel()) {
70-
channel.truncate(size);
71-
in.mark((int) size);
72-
long workDone = 0;
73-
long count = size > 100? size / 100: 1;
74-
while (workDone < size) {
75-
long transferred = channel.transferFrom(rbc, workDone, count);
76-
workDone += transferred;
77-
updateMessage(pF.format((double) workDone/ size));
78-
updateProgress(workDone, size);
71+
FileChannel channel = FileChannel.open(forDownload)) {
72+
channel.truncate(size);
73+
in.mark((int) size);
74+
long workDone = 0;
75+
long count = size > 100? size / 100: 1;
76+
while (workDone < size) {
77+
long transferred = channel.transferFrom(rbc, workDone, count);
78+
workDone += transferred;
79+
updateMessage(pF.format((double) workDone/ size));
80+
updateProgress(workDone, size);
7981
}
8082
channel.force(true);
8183
} catch (Exception ex) { ex.printStackTrace(); }
8284

8385
return forDownload;
8486
}
8587

86-
private boolean extractZip(File forExtract, File dir, boolean deleteZip) throws IOException {
88+
private boolean extractZip(Path forExtract, Path dir, boolean deleteZip) throws IOException {
8789
NumberFormat pF = NumberFormat.getPercentInstance();
88-
updateTitle("Rozpakowywanie " + forExtract.getName());
90+
updateTitle("Rozpakowywanie " + forExtract.getFileName());
8991
updateProgress(0, 1);
9092
updateMessage("");
91-
try (ZipFile zip = new ZipFile(forExtract)) {
92-
updateProgress(0, forExtract.length());
93+
try (ZipFile zip = new ZipFile(forExtract.toFile())) {
94+
updateProgress(0, java.nio.file.Files.size(forExtract));
9395
double total = 0;
94-
if (dir == null) dir = forExtract.getParentFile();
96+
if (dir == null) dir = forExtract.getParent();
9597
Enumeration<? extends ZipEntry> entries = zip.entries();
9698
while (entries.hasMoreElements()) {
9799
ZipEntry entry = entries.nextElement();
98100
System.out.println(entry);
99101
if (entry.isDirectory()) {
100-
if (new File(dir, entry.getName()).mkdirs()) System.out.println("Directory created!");
102+
if (java.nio.file.Files.isDirectory(java.nio.file.Files.createDirectories(dir.resolve(entry.getName()))))
103+
System.out.println("Directory created!");
101104
}
102105

103106
InputStream input = zip.getInputStream(entry);
@@ -108,18 +111,18 @@ private boolean extractZip(File forExtract, File dir, boolean deleteZip) throws
108111
double work = 0;
109112
while (entries.hasMoreElements()) {
110113
ZipEntry entry = entries.nextElement();
111-
File outFile = new File(dir, entry.getName());
114+
Path outFile = dir.resolve(entry.getName());
112115

113116
if (!entry.isDirectory()) {
114-
if (outFile.createNewFile()) {
117+
if (java.nio.file.Files.isRegularFile(java.nio.file.Files.createFile(outFile))) {
115118
System.out.println("New file created: " + outFile);
116119
}
117120
}
118121
else continue;
119122

120123
System.out.println("Entry: " + entry);
121124
InputStream input = zip.getInputStream(entry);
122-
try (FileOutputStream output = new FileOutputStream(outFile)) {
125+
try (OutputStream output = java.nio.file.Files.newOutputStream(outFile)) {
123126
while (input.available() > 0) {
124127
int b = input.read();
125128
output.write(b);
@@ -131,11 +134,11 @@ private boolean extractZip(File forExtract, File dir, boolean deleteZip) throws
131134
}
132135
}
133136
updateProgress(Double.NaN, 0);
134-
if (deleteZip) return forExtract.delete();
137+
if (deleteZip) return java.nio.file.Files.deleteIfExists(forExtract);
135138
return false;
136139
}
137140

138-
private static void restart(Map<File, File> updateMap) {
141+
private static void restart(Map<Path, Path> updateMap) {
139142
Platform.exit();
140143
try {
141144
File update = Files.UPDATE_WIN;
@@ -147,28 +150,28 @@ private static void restart(Map<File, File> updateMap) {
147150
System.exit(0);
148151
}
149152

150-
private static void generateUpdateScript(File update, Map<File, File> updateMap) {
153+
private static void generateUpdateScript(File update, Map<Path, Path> updateMap) {
151154
try (BufferedWriter writer = new BufferedWriter(new FileWriter(update))) {
152155
generateWinUpdateScript(writer, updateMap);
153156
} catch (IOException e) {
154157
e.printStackTrace();
155158
}
156159
}
157160

158-
private static void generateWinUpdateScript(BufferedWriter writer, Map<File, File> updateMap)
161+
private static void generateWinUpdateScript(BufferedWriter writer, Map<Path, Path> updateMap)
159162
throws IOException {
160163
writer.write("@echo off");
161164
writer.newLine();
162165
writer.write("timeout /T 5 /nobreak > nul");
163166
writer.newLine();
164-
for (Map.Entry<File, File> entry : updateMap.entrySet()) {
165-
File oldFile = entry.getValue();
166-
File newFile = entry.getKey();
167-
if (newFile.exists()) {
167+
for (Map.Entry<Path, Path> entry : updateMap.entrySet()) {
168+
Path oldFile = entry.getValue();
169+
Path newFile = entry.getKey();
170+
if (java.nio.file.Files.exists(newFile)) {
168171
writer.write("copy " + '"');
169-
writer.write(newFile.getPath());
172+
writer.write(newFile.toString());
170173
writer.write('"' + " " + '"');
171-
writer.write(oldFile.getPath());
174+
writer.write(oldFile.toString());
172175
writer.write('"' + " /y");
173176
writer.newLine();
174177
} else throw new FileNotFoundException("Nie znaleziono odpowiednich plików do aktualizacji.");

src/main/java/pl/koder95/eme/au/GitHubRepositoryController.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ public void onError(IOException e, HttpURLConnection uc) {
1414
e.printStackTrace();
1515
System.out.println(uc);
1616
}
17-
}).withOAuthToken("35fcc8651d5fed19ca80b9fc8fbd17e4599c7499").build();
17+
}).build();
1818
}
1919

2020
private GitHub connection = null;

0 commit comments

Comments
 (0)