test/java/util/zip/zip.java
Print this page
rev 3516 : 7021582: convert jar/zip code and tests to use try-with-resources
Reviewed-by: alanb, dholmes, sherman
@@ -320,24 +320,25 @@
}
}
void create(OutputStream out) throws IOException
{
- ZipOutputStream zos = new ZipOutputStream(out, cs);
+ try (ZipOutputStream zos = new ZipOutputStream(out, cs)) {
if (flag0) {
zos.setMethod(ZipOutputStream.STORED);
}
for (File file: entries) {
addFile(zos, file);
}
- zos.close();
+ }
}
boolean update(InputStream in, OutputStream out) throws IOException
{
- ZipInputStream zis = new ZipInputStream(in, cs);
- ZipOutputStream zos = new ZipOutputStream(out, cs);
+ try (ZipInputStream zis = new ZipInputStream(in, cs);
+ ZipOutputStream zos = new ZipOutputStream(out, cs))
+ {
ZipEntry e = null;
byte[] buf = new byte[1024];
int n = 0;
boolean updateOk = true;
@@ -369,12 +370,11 @@
// add the remaining new files
for (File f: entries) {
addFile(zos, f);
}
- zis.close();
- zos.close();
+ }
return updateOk;
}
private String entryName(String name) {
name = name.replace(File.separatorChar, '/');
@@ -515,11 +515,11 @@
}
updateLastModifiedTime(dirs);
}
void extract(String fname, String files[]) throws IOException {
- ZipFile zf = new ZipFile(fname, cs);
+ try (ZipFile zf = new ZipFile(fname, cs)) {
Set<ZipEntry> dirs = newDirSet();
Enumeration<? extends ZipEntry> zes = zf.entries();
while (zes.hasMoreElements()) {
ZipEntry e = zes.nextElement();
InputStream is;
@@ -533,11 +533,11 @@
break;
}
}
}
}
- zf.close();
+ }
updateLastModifiedTime(dirs);
}
ZipEntry extractFile(InputStream is, ZipEntry e) throws IOException {
ZipEntry rc = null;
@@ -605,16 +605,16 @@
printEntry(e, files);
}
}
void list(String fname, String files[]) throws IOException {
- ZipFile zf = new ZipFile(fname, cs);
+ try (ZipFile zf = new ZipFile(fname, cs)) {
Enumeration<? extends ZipEntry> zes = zf.entries();
while (zes.hasMoreElements()) {
printEntry(zes.nextElement(), files);
}
- zf.close();
+ }
}
void printEntry(ZipEntry e, String[] files) throws IOException {
if (files == null) {
printEntry(e);