13
13
import java .nio .channels .Channels ;
14
14
import java .nio .channels .FileChannel ;
15
15
import java .nio .channels .ReadableByteChannel ;
16
+ import java .nio .file .Path ;
16
17
import java .text .NumberFormat ;
17
18
import java .util .Enumeration ;
18
19
import java .util .HashMap ;
@@ -43,61 +44,63 @@ protected Object call() throws Exception {
43
44
}
44
45
45
46
// 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 ());
47
48
// rozpakowywanie do folderu tymczasowego
48
49
if (extractZip (downloaded , TEMP_DIR , true )) {
49
50
// 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
+
52
54
restart (updateMap );
53
55
}
54
56
return null ;
55
57
}
56
58
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 {
58
60
NumberFormat pF = NumberFormat .getPercentInstance ();
59
61
String title = "Pobieranie " + name ;
60
62
updateTitle (title );
61
63
updateProgress (0 , 1 );
62
64
63
65
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 );
65
68
66
69
try (InputStream in = connection .getInputStream ();
67
70
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 );
79
81
}
80
82
channel .force (true );
81
83
} catch (Exception ex ) { ex .printStackTrace (); }
82
84
83
85
return forDownload ;
84
86
}
85
87
86
- private boolean extractZip (File forExtract , File dir , boolean deleteZip ) throws IOException {
88
+ private boolean extractZip (Path forExtract , Path dir , boolean deleteZip ) throws IOException {
87
89
NumberFormat pF = NumberFormat .getPercentInstance ();
88
- updateTitle ("Rozpakowywanie " + forExtract .getName ());
90
+ updateTitle ("Rozpakowywanie " + forExtract .getFileName ());
89
91
updateProgress (0 , 1 );
90
92
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 ));
93
95
double total = 0 ;
94
- if (dir == null ) dir = forExtract .getParentFile ();
96
+ if (dir == null ) dir = forExtract .getParent ();
95
97
Enumeration <? extends ZipEntry > entries = zip .entries ();
96
98
while (entries .hasMoreElements ()) {
97
99
ZipEntry entry = entries .nextElement ();
98
100
System .out .println (entry );
99
101
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!" );
101
104
}
102
105
103
106
InputStream input = zip .getInputStream (entry );
@@ -108,18 +111,18 @@ private boolean extractZip(File forExtract, File dir, boolean deleteZip) throws
108
111
double work = 0 ;
109
112
while (entries .hasMoreElements ()) {
110
113
ZipEntry entry = entries .nextElement ();
111
- File outFile = new File ( dir , entry .getName ());
114
+ Path outFile = dir . resolve ( entry .getName ());
112
115
113
116
if (!entry .isDirectory ()) {
114
- if (outFile . createNewFile ( )) {
117
+ if (java . nio . file . Files . isRegularFile ( java . nio . file . Files . createFile ( outFile ) )) {
115
118
System .out .println ("New file created: " + outFile );
116
119
}
117
120
}
118
121
else continue ;
119
122
120
123
System .out .println ("Entry: " + entry );
121
124
InputStream input = zip .getInputStream (entry );
122
- try (FileOutputStream output = new FileOutputStream (outFile )) {
125
+ try (OutputStream output = java . nio . file . Files . newOutputStream (outFile )) {
123
126
while (input .available () > 0 ) {
124
127
int b = input .read ();
125
128
output .write (b );
@@ -131,11 +134,11 @@ private boolean extractZip(File forExtract, File dir, boolean deleteZip) throws
131
134
}
132
135
}
133
136
updateProgress (Double .NaN , 0 );
134
- if (deleteZip ) return forExtract . delete ( );
137
+ if (deleteZip ) return java . nio . file . Files . deleteIfExists ( forExtract );
135
138
return false ;
136
139
}
137
140
138
- private static void restart (Map <File , File > updateMap ) {
141
+ private static void restart (Map <Path , Path > updateMap ) {
139
142
Platform .exit ();
140
143
try {
141
144
File update = Files .UPDATE_WIN ;
@@ -147,28 +150,28 @@ private static void restart(Map<File, File> updateMap) {
147
150
System .exit (0 );
148
151
}
149
152
150
- private static void generateUpdateScript (File update , Map <File , File > updateMap ) {
153
+ private static void generateUpdateScript (File update , Map <Path , Path > updateMap ) {
151
154
try (BufferedWriter writer = new BufferedWriter (new FileWriter (update ))) {
152
155
generateWinUpdateScript (writer , updateMap );
153
156
} catch (IOException e ) {
154
157
e .printStackTrace ();
155
158
}
156
159
}
157
160
158
- private static void generateWinUpdateScript (BufferedWriter writer , Map <File , File > updateMap )
161
+ private static void generateWinUpdateScript (BufferedWriter writer , Map <Path , Path > updateMap )
159
162
throws IOException {
160
163
writer .write ("@echo off" );
161
164
writer .newLine ();
162
165
writer .write ("timeout /T 5 /nobreak > nul" );
163
166
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 )) {
168
171
writer .write ("copy " + '"' );
169
- writer .write (newFile .getPath ());
172
+ writer .write (newFile .toString ());
170
173
writer .write ('"' + " " + '"' );
171
- writer .write (oldFile .getPath ());
174
+ writer .write (oldFile .toString ());
172
175
writer .write ('"' + " /y" );
173
176
writer .newLine ();
174
177
} else throw new FileNotFoundException ("Nie znaleziono odpowiednich plików do aktualizacji." );
0 commit comments