Skip to content

Commit 3dee115

Browse files
committed
try-w-r to avoid "Cleaning up unclosed ZipFile" during tests
1 parent 7d00439 commit 3dee115

File tree

1 file changed

+29
-28
lines changed

1 file changed

+29
-28
lines changed

opengrok-indexer/src/test/java/org/opengrok/indexer/util/FileUtilities.java

Lines changed: 29 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -47,35 +47,36 @@
4747
public class FileUtilities {
4848

4949
public static void extractArchive(File sourceBundle, File root) throws IOException {
50-
ZipFile zipfile = new ZipFile(sourceBundle);
51-
52-
Enumeration<ZipArchiveEntry> e = zipfile.getEntries();
53-
54-
while (e.hasMoreElements()) {
55-
ZipArchiveEntry ze = e.nextElement();
56-
File file = new File(root, ze.getName());
57-
if (ze.isUnixSymlink()) {
58-
File target = new File(file.getParent(), zipfile.getUnixSymlink(ze));
59-
/*
60-
* A weirdness is that an object may already have been exploded
61-
* before the symlink entry is reached in the ZipFile. So
62-
* unlink any existing entry to avoid an exception on creating
63-
* the symlink.
64-
*/
65-
if (file.isDirectory()) {
66-
removeDirs(file);
67-
} else if (file.exists()) {
68-
file.delete();
69-
}
70-
Files.createSymbolicLink(file.toPath(), target.toPath());
71-
} else if (ze.isDirectory()) {
72-
file.mkdirs();
73-
} else {
74-
try (InputStream in = zipfile.getInputStream(ze); OutputStream out = new FileOutputStream(file)) {
75-
if (in == null) {
76-
throw new IOException("Cannot get InputStream for " + ze);
50+
try (ZipFile zipfile = new ZipFile(sourceBundle)) {
51+
Enumeration<ZipArchiveEntry> e = zipfile.getEntries();
52+
53+
while (e.hasMoreElements()) {
54+
ZipArchiveEntry ze = e.nextElement();
55+
File file = new File(root, ze.getName());
56+
if (ze.isUnixSymlink()) {
57+
File target = new File(file.getParent(), zipfile.getUnixSymlink(ze));
58+
/*
59+
* A weirdness is that an object may already have been
60+
* exploded before the symlink entry is reached in the
61+
* ZipFile. So unlink any existing entry to avoid an
62+
* exception on creating the symlink.
63+
*/
64+
if (file.isDirectory()) {
65+
removeDirs(file);
66+
} else if (file.exists()) {
67+
file.delete();
68+
}
69+
Files.createSymbolicLink(file.toPath(), target.toPath());
70+
} else if (ze.isDirectory()) {
71+
file.mkdirs();
72+
} else {
73+
try (InputStream in = zipfile.getInputStream(ze);
74+
OutputStream out = new FileOutputStream(file)) {
75+
if (in == null) {
76+
throw new IOException("Cannot get InputStream for " + ze);
77+
}
78+
copyFile(in, out);
7779
}
78-
copyFile(in, out);
7980
}
8081
}
8182
}

0 commit comments

Comments
 (0)