-
Notifications
You must be signed in to change notification settings - Fork 87
/
PakFile.java
93 lines (75 loc) · 2.83 KB
/
PakFile.java
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
/*
** 2011 November 4
**
** The author disclaims copyright to this source code. In place of
** a legal notice, here is a blessing:
** May you do good and not evil.
** May you find forgiveness for yourself and forgive others.
** May you share freely, never taking more than you give.
*/
package info.ata4.bsplib;
import info.ata4.bsplib.lump.Lump;
import info.ata4.bsplib.lump.LumpType;
import info.ata4.log.LogUtils;
import java.io.IOException;
import java.io.InputStream;
import java.nio.file.Files;
import java.nio.file.Path;
import java.util.List;
import java.util.logging.Level;
import java.util.logging.Logger;
import org.apache.commons.compress.archivers.zip.ZipArchiveEntry;
import org.apache.commons.compress.archivers.zip.ZipArchiveInputStream;
/**
* Class to read BSP-embedded zip files (pakiles).
*
* @author Nico Bergemann <barracuda415 at yahoo.de>
*/
public class PakFile {
private static final Logger L = LogUtils.getLogger();
private final Lump pakLump;
public PakFile(BspFile bspFile) {
pakLump = bspFile.getLump(LumpType.LUMP_PAKFILE);
}
public ZipArchiveInputStream getArchiveInputStream() {
return new ZipArchiveInputStream(pakLump.getInputStream(), "Cp437", false);
}
public void unpack(Path dest) throws IOException {
unpack(dest, false);
}
public void unpack(Path dest, boolean direct) throws IOException {
if (direct) {
L.log(Level.INFO, "Extracting pakfile to {0}", dest);
try (InputStream is = pakLump.getInputStream()) {
Files.copy(is, dest);
}
} else {
unpack(dest, null);
}
}
public void unpack(Path dest, List<String> names) throws IOException {
Files.createDirectories(dest);
try (ZipArchiveInputStream zis = getArchiveInputStream()) {
for (ZipArchiveEntry ze; (ze = zis.getNextZipEntry()) != null;) {
String zipName = ze.getName();
if (names != null && !names.contains(zipName)) {
continue;
}
// some maps have embedded files with absolute paths, for
// whatever reason...
zipName = zipName.replace(':', '_');
Path entryFile = dest.resolve(zipName);
if (Files.notExists(entryFile.getParent())) {
Files.createDirectories(entryFile.getParent());
}
// don't overwrite any files
if (Files.exists(entryFile)) {
L.log(Level.INFO, "Skipped {0}", ze.getName());
continue;
}
L.log(Level.INFO, "Extracting {0}", ze.getName());
Files.copy(zis, entryFile);
}
}
}
}