-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathFileUtils.java
More file actions
81 lines (68 loc) · 2.51 KB
/
FileUtils.java
File metadata and controls
81 lines (68 loc) · 2.51 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
import org.codehaus.plexus.archiver.tar.TarGZipUnArchiver;
import org.codehaus.plexus.logging.console.ConsoleLoggerManager;
import java.util.logging.Level;
import java.util.logging.Logger;
import java.io.*;
import java.net.MalformedURLException;
import java.net.URL;
import java.nio.file.*;
class FileUtils {
private static final Path TEMP_DIR = Paths.get("temp/");
private static final Path DOCS_DIR = TEMP_DIR.resolve("docs/");
static final Path INDEX_DIR = TEMP_DIR.resolve("index/");
static final Path DOCS_FILE = DOCS_DIR.resolve("cran.all.1400");
static final Path QUERY_FILE = DOCS_DIR.resolve("cran.qry");
static final Path BASELINE_FILE = DOCS_DIR.resolve("cranqrel");
private static final URL DOCS_URL;
static {
try {
DOCS_URL = new URL("http://ir.dcs.gla.ac.uk/resources/test_collections/cran/cran.tar.gz");
} catch (MalformedURLException e) {
throw new Error(e);
}
}
static void initialize() {
createDirectory(INDEX_DIR);
createDirectory(DOCS_DIR);
Path gzipFile = fetchDocs(DOCS_URL, TEMP_DIR);
decompress(gzipFile, DOCS_DIR);
}
private static void decompress(Path gzipFile, Path dir) {
final TarGZipUnArchiver unarchiver = new TarGZipUnArchiver();
ConsoleLoggerManager manager = new ConsoleLoggerManager();
manager.initialize();
unarchiver.enableLogging(manager.getLoggerForComponent(null));
unarchiver.setSourceFile(gzipFile.toFile());
unarchiver.setDestDirectory(dir.toFile());
unarchiver.extract();
}
private static void createDirectory(Path path) {
if (Files.notExists(path)) {
try {
Files.createDirectories(path);
} catch (IOException e) {
e.printStackTrace();
System.exit(1);
}
}
}
private static Path fetchDocs(URL url, Path toDir) {
String urlStr = url.toString();
int index = urlStr.lastIndexOf('/');
String fileName = urlStr.substring(index + 1);
Path path = toDir.resolve(fileName);
try {
if (Files.notExists(path)) {
InputStream in = url.openStream();
Files.copy(in, path);
in.close();
}
return path;
} catch (IOException e) {
e.printStackTrace();
Logger.getGlobal().log(Level.SEVERE, "Fetch documents failed");
System.exit(1);
}
return path;
}
}