From 85e305bf63b3954e6266801175579a8b81769709 Mon Sep 17 00:00:00 2001 From: John Bauer Date: Fri, 26 Nov 2021 10:13:18 -0800 Subject: [PATCH] Create a ZipFile in a context so it will be automatically closed --- src/edu/stanford/nlp/ui/JarFileChooser.java | 19 ++++++++++--------- 1 file changed, 10 insertions(+), 9 deletions(-) diff --git a/src/edu/stanford/nlp/ui/JarFileChooser.java b/src/edu/stanford/nlp/ui/JarFileChooser.java index 141989a31f..ec0053e158 100644 --- a/src/edu/stanford/nlp/ui/JarFileChooser.java +++ b/src/edu/stanford/nlp/ui/JarFileChooser.java @@ -63,7 +63,7 @@ public String showListSelectionDialog(List files, Point location) { //frame.setLocation(location); final JDialog dialog = new JDialog(frame, "Jar File Chooser", true); dialog.setLocation(location); - final JList fileList = new JList(new Vector<>(files)); + final JList fileList = new JList<>(new Vector<>(files)); fileList.setSelectionMode(ListSelectionModel.SINGLE_SELECTION); MouseListener mouseListener = new MouseAdapter() { public void mouseClicked(MouseEvent e) { @@ -126,16 +126,17 @@ public List getFiles(File jarFile) //System.out.println("Looking at " + jarFile); List files = new ArrayList<>(); - ZipFile zin = new ZipFile(jarFile); - Enumeration entries = zin.entries(); - while (entries.hasMoreElements()) { - ZipEntry entry = entries.nextElement(); - String name = entry.getName(); - if (name.matches(pattern)) { - files.add(name); + try (ZipFile zin = new ZipFile(jarFile)) { + Enumeration entries = zin.entries(); + while (entries.hasMoreElements()) { + ZipEntry entry = entries.nextElement(); + String name = entry.getName(); + if (name.matches(pattern)) { + files.add(name); + } } + Collections.sort(files); } - Collections.sort(files); return files; } }