-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathFileIO.java
More file actions
483 lines (378 loc) · 12.4 KB
/
Copy pathFileIO.java
File metadata and controls
483 lines (378 loc) · 12.4 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
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
// FileIO.java
// Andrew Davison, ad@fivedots.coe.psu.ac.th, March 2015
/* A growing collection of utility functions to make Office
easier to use. They are currently divided into the following
groups:
* File IO
* file creation / deletion
* saving/writing to a file
* zip access
*/
import java.io.*;
import java.nio.file.*;
import java.util.*;
import java.util.zip.*;
import java.text.*;
import java.net.*;
import com.sun.star.io.*;
import com.sun.star.ucb.*;
import com.sun.star.packages.zip.*;
import com.sun.star.container.*;
public class FileIO
{
public static String getUtilsFolder()
// http://stackoverflow.com/questions/320542/how-to-get-the-path-of-a-running-jar-file
{
try {
return FileIO.class.getProtectionDomain().getCodeSource().
getLocation().toURI().getPath();
}
catch(URISyntaxException e) {
System.out.println(e);
return null;
}
} // end of getUtilsFolder()
public static String getAbsolutePath(String fnm)
{ return new File(fnm).getAbsolutePath(); }
public static String urlToPath(String url)
{
try {
return Paths.get(new URL(url).toURI()).toString();
}
catch (java.lang.Exception e)
{ System.out.println("Could not parse " + url);
return null;
}
} // end of urlToPath()
public static boolean isOpenable(String fnm)
// convert a file path to URL format
{
File f = new File(fnm);
if (!f.exists()) {
System.out.println(fnm + " does not exist");
return false;
}
if (!f.isFile()) {
System.out.println(fnm + " is not a file");
return false;
}
if (!f.canRead()) {
System.out.println(fnm + " is not readable");
return false;
}
return true;
} // end of isOpenable()
public static String fnmToURL(String fnm)
// convert a file path to URL format
{
try {
StringBuffer sb = null;
String path = new File(fnm).getCanonicalPath();
sb = new StringBuffer("file:///");
sb.append(path.replace('\\', '/'));
return sb.toString();
}
catch (java.io.IOException e)
{ System.out.println("Could not access " + fnm);
return null;
}
} // end of fnmToURL()
public static String URI2Path(String URIfnm)
{
try {
File file = new File(new URI(URIfnm).getSchemeSpecificPart());
return file.getCanonicalPath();
// e.g. C:\Program Files (x86)\LibreOffice 4\program\addin
}
catch (java.lang.Exception e) {
System.out.println("Could not translate settings path");
return URIfnm;
}
} // end of URI2Path()
public static boolean makeDirectory(String dir)
{
File file = new File(dir);
if (!file.exists()) {
if (file.mkdir()) {
System.out.println("Created " + dir);
return true;
}
else {
System.out.println("Could not create " + dir);
return false;
}
}
else {
// System.out.println(dir + " already exists");
return true;
}
} // end of makeDirectory()
public static String[] getFileNames(String dir)
{
File[] files = new File(dir).listFiles();
if (files == null) {
System.out.println("No directory found called " + dir);
return null;
}
ArrayList<String> results = new ArrayList<String>();
for (File file : files) {
if (file.isFile())
results.add(file.getName());
}
int numFiles = results.size();
if (numFiles == 0) {
System.out.println("No files found in the directory " + dir);
return null;
}
String[] fnms = new String[numFiles];
for(int i=0; i < numFiles; i++)
fnms[i] = results.get(i);
return fnms;
} // end of getFileNames()
public static String getFnm(String path)
{ return (new File(path)).getName(); }
// ---- ------------- file creation / deletion --------------
public static String createTempFile(String imFormat)
{
try {
File temp = File.createTempFile("loTemp", "." + imFormat);
temp.deleteOnExit(); // should delete file at JVM exit
return temp.getAbsolutePath();
}
catch(java.io.IOException e) {
System.out.println("Could not create temp file");
return null;
}
} // end of createTempFile();
public static void deleteFiles(ArrayList<String> dbFnms)
{
System.out.println();
for (int i = 0; i < dbFnms.size(); i++)
deleteFile( dbFnms.get(i) );
} // end of deleteFiles()
public static void deleteFile(String fnm)
{
File file = new File(fnm);
if (file.delete())
System.out.println(fnm + " deleted");
else
System.out.println(fnm + "could not be deleted");
} // end of deleteFile()
// ---------------- saving/writing to a file --------------------
public static void saveString(String fnm, String s)
{
if (s == null) {
System.out.println("No data to save in " + fnm);
return;
}
try {
FileWriter fw = new FileWriter( new File(fnm));
fw.write(s);
fw.close();
System.out.println("Saved string to file: " + fnm);
}
catch (java.io.IOException ex) {
System.out.println("Could not save string to file: " + fnm);
}
} // end of saveString()
public static void saveBytes(String fnm, byte[] bytes)
{
if (bytes == null) {
System.out.println("No data to save in " + fnm);
return;
}
try {
FileOutputStream fos = new FileOutputStream(fnm);
fos.write(bytes);
fos.close();
System.out.println("Saved bytes to file: " + fnm);
}
catch (java.io.IOException ex) {
System.out.println("Could not save bytes to file: " + fnm);
}
} // end of saveBytes()
public static void saveArray(String fnm, Object[][] arr)
{
if (arr == null) {
System.out.println("No data to save in " + fnm);
return;
}
try {
BufferedWriter bw = new BufferedWriter(new FileWriter(fnm));
int numCols = arr[0].length;
int numRows = arr.length;
for(int j=0; j < numRows; j++) {
for (int i = 0; i < numCols; i++)
bw.write((String)arr[j][i] + "\t");
bw.write("\n");
}
bw.close();
System.out.println("Saved array to file: " + fnm);
}
catch (java.io.IOException ex) {
System.out.println("Could not save array to file: " + fnm);
}
} // end of saveArray()
public static void saveArray(String fnm, double[][] arr)
// repeated code but for saving a doubles array
{
if (arr == null) {
System.out.println("No data to save in " + fnm);
return;
}
try {
BufferedWriter bw = new BufferedWriter(new FileWriter(fnm));
int numCols = arr[0].length;
int numRows = arr.length;
for(int j=0; j < numRows; j++) {
for (int i = 0; i < numCols; i++)
bw.write(arr[j][i] + "\t");
bw.write("\n");
}
bw.close();
System.out.println("Saved array to file: " + fnm);
}
catch (java.io.IOException ex) {
System.out.println("Could not save array to file: " + fnm);
}
} // end of saveArray()
/*
public static boolean saveFile(String fnm, byte[] data)
{
String openFileURL = FileIO.fnmToURL(fnm);
boolean res = false;
try {
XSimpleFileAccess sfa =
Lo.createInstanceMCF(XSimpleFileAccess.class,
"com.sun.star.comp.ucb.SimpleFileAccess");
System.out.println("sf 0");
XOutputStream os = sfa.openFileWrite(openFileURL);
System.out.println("sf 1");
os.writeBytes(data);
os.flush();
os.closeOutput();
res = true;
}
catch (java.lang.Exception e) {
System.out.println(e);
}
return res;
} // end of saveFile
*/
public static void appendTo(String fnm, String msg)
{
try {
FileWriter fw = new FileWriter(fnm, true);
fw.write(msg + "\n");
fw.close();
}
catch(java.io.IOException e)
{ System.out.println("Unable to append to " + fnm); }
} // end of appendTo()
// ----------------------- zip access ---------------------------------------
public static XZipFileAccess zipAccess(String fnm)
// get zip access to the document using Office API
{
return Lo.createInstanceMCF(XZipFileAccess.class,
"com.sun.star.packages.zip.ZipFileAccess",
new Object[]{ fnmToURL(fnm) });
}
public static void zipListUno(String fnm)
// replaced by more detailed Java version; see below
{
XZipFileAccess zfa = zipAccess(fnm);
XNameAccess nmAccess = Lo.qi(XNameAccess.class, zfa);
String[] names = nmAccess.getElementNames();
System.out.println("\nZipped Contents of " + fnm);
Lo.printNames(names, 1);
} // end of zipListUno()
public static void unzipFile(XZipFileAccess zfa, String fnm)
{
String fileName = Info.getName(fnm);
String ext = Info.getExt(fnm);
try {
System.out.println("Extracting " + fnm);
XInputStream inStream = zfa.getStreamByPattern("*" + fnm);
XSimpleFileAccess3 fileAcc = Lo.createInstanceMCF(XSimpleFileAccess3.class,
"com.sun.star.ucb.SimpleFileAccess");
String copyFnm = (ext == null) ? (fileName + "Copy") :
(fileName + "Copy." + ext);
System.out.println("Saving to " + copyFnm);
fileAcc.writeFile(FileIO.fnmToURL(copyFnm), inStream);
}
catch (com.sun.star.uno.Exception e)
{ System.out.println(e); }
} // end of unzipFile()
public static String getMimeType(XZipFileAccess zfa)
// return the contents of mimetype
// also see Info.getMIMEType() for use of MimetypesFileTypeMap
{
try {
// System.out.println("Extracting mime.type");
XInputStream inStream = zfa.getStreamByPattern("mimetype");
String[] lines = FileIO.readLines(inStream);
if (lines != null)
// System.out.println(" " + lines[0]);
return lines[0].trim();
}
catch (com.sun.star.uno.Exception e)
{ System.out.println(e); }
System.out.println("No mimetype found");
return null;
} // end of getMimeType()
public static String[] readLines(XInputStream is)
// return the contents of an Office input stream as an array of lines
{
String[] linesArr = null;
ArrayList<String> lines = new ArrayList<String>();
try {
XTextInputStream tis = Lo.createInstanceMCF(XTextInputStream.class,
"com.sun.star.io.TextInputStream");
XActiveDataSink sink = Lo.qi(XActiveDataSink.class, tis);
sink.setInputStream(is);
while (!tis.isEOF())
lines.add( tis.readLine());
tis.closeInput();
linesArr = new String[ lines.size() ];
lines.toArray(linesArr);
}
catch (Exception e)
{ System.out.println(e); }
return linesArr;
} // end of readLines()
// ----------------- switch to Java's zip APIs ------------
public static void zipList(String fnm)
// from http://www.drdobbs.com/jvm/java-and-the-zip-file-format/184410339
{
DateFormat df= DateFormat.getDateInstance(); // date format
DateFormat tf= DateFormat.getTimeInstance(); // time format
tf.setTimeZone( TimeZone.getDefault() );
try {
ZipFile zfile = new ZipFile(fnm);
System.out.println("Listing of " + zfile.getName() + ":");
System.out.println("Raw Size Size Date Time Name");
System.out.println("-------- ------- ------- ------- --------");
Enumeration<? extends java.util.zip.ZipEntry> zfs = zfile.entries();
while (zfs.hasMoreElements()) {
java.util.zip.ZipEntry entry = (java.util.zip.ZipEntry) zfs.nextElement();
Date d = new Date(entry.getTime());
System.out.print( padSpaces(entry.getSize(), 9) + " ");
System.out.print( padSpaces(entry.getCompressedSize(), 7) + " ");
System.out.print(" " + df.format(d) + " ");
System.out.print(" " + tf.format(d) + " ");
System.out.println(" " + entry.getName());
}
System.out.println();
}
catch (java.io.IOException e)
{ System.out.println(e); }
} // end of zipList()
private static String padSpaces(long l, int width)
// Used to print a long integer using a specific width
{
String s = new Long(l).toString();
while (s.length() < width)
s += " ";
return s;
} // end of padSpaces()
} // end of FileIO class