You need to sign in or sign up before continuing.
提交 47a4bc28 编写于 作者: S smarks

7021582: convert jar/zip code and tests to use try-with-resources

Reviewed-by: alanb, dholmes, sherman
上级 ad5d7da0
...@@ -376,9 +376,9 @@ class JarFile extends ZipFile { ...@@ -376,9 +376,9 @@ class JarFile extends ZipFile {
*/ */
private byte[] getBytes(ZipEntry ze) throws IOException { private byte[] getBytes(ZipEntry ze) throws IOException {
byte[] b = new byte[(int)ze.getSize()]; byte[] b = new byte[(int)ze.getSize()];
DataInputStream is = new DataInputStream(super.getInputStream(ze)); try (DataInputStream is = new DataInputStream(super.getInputStream(ze))) {
is.readFully(b, 0, b.length); is.readFully(b, 0, b.length);
is.close(); }
return b; return b;
} }
...@@ -480,10 +480,10 @@ class JarFile extends ZipFile { ...@@ -480,10 +480,10 @@ class JarFile extends ZipFile {
JarEntry manEntry = getManEntry(); JarEntry manEntry = getManEntry();
if (manEntry != null) { if (manEntry != null) {
byte[] b = new byte[(int)manEntry.getSize()]; byte[] b = new byte[(int)manEntry.getSize()];
DataInputStream dis = new DataInputStream( try (DataInputStream dis = new DataInputStream(
super.getInputStream(manEntry)); super.getInputStream(manEntry))) {
dis.readFully(b, 0, b.length); dis.readFully(b, 0, b.length);
dis.close(); }
int last = b.length - src.length; int last = b.length - src.length;
int i = 0; int i = 0;
......
...@@ -40,22 +40,21 @@ public class GetMethodsReturnClones { ...@@ -40,22 +40,21 @@ public class GetMethodsReturnClones {
System.getProperty("file.separator"); System.getProperty("file.separator");
public static void main(String[] args) throws Exception { public static void main(String[] args) throws Exception {
JarFile jf = new JarFile(BASE + "test.jar", true); List<JarEntry> entries = new ArrayList<>();
try (JarFile jf = new JarFile(BASE + "test.jar", true)) {
byte[] buffer = new byte[8192]; byte[] buffer = new byte[8192];
Enumeration<JarEntry> e = jf.entries(); Enumeration<JarEntry> e = jf.entries();
List<JarEntry> entries = new ArrayList<JarEntry>();
while (e.hasMoreElements()) { while (e.hasMoreElements()) {
JarEntry je = e.nextElement(); JarEntry je = e.nextElement();
entries.add(je); entries.add(je);
InputStream is = jf.getInputStream(je); try (InputStream is = jf.getInputStream(je)) {
while (is.read(buffer, 0, buffer.length) != -1) { while (is.read(buffer, 0, buffer.length) != -1) {
// we just read. this will throw a SecurityException // we just read. this will throw a SecurityException
// if a signature/digest check fails. // if a signature/digest check fails.
} }
is.close();
} }
jf.close(); }
}
for (JarEntry je : entries) { for (JarEntry je : entries) {
Certificate[] certs = je.getCertificates(); Certificate[] certs = je.getCertificates();
......
...@@ -37,25 +37,25 @@ import java.util.jar.*; ...@@ -37,25 +37,25 @@ import java.util.jar.*;
public class ScanSignedJar { public class ScanSignedJar {
public static void main(String[] args) throws Exception { public static void main(String[] args) throws Exception {
JarFile file = new JarFile(new File(System.getProperty("test.src","."),
"bogus-signerinfo-attr.jar"));
byte[] buffer = new byte[8192];
boolean isSigned = false; boolean isSigned = false;
try (JarFile file = new JarFile(new File(System.getProperty("test.src","."),
"bogus-signerinfo-attr.jar"))) {
byte[] buffer = new byte[8192];
for (Enumeration entries = file.entries(); entries.hasMoreElements();) { for (Enumeration entries = file.entries(); entries.hasMoreElements();) {
JarEntry entry = (JarEntry) entries.nextElement(); JarEntry entry = (JarEntry) entries.nextElement();
InputStream jis = file.getInputStream(entry); try (InputStream jis = file.getInputStream(entry)) {
while (jis.read(buffer, 0, buffer.length) != -1) { while (jis.read(buffer, 0, buffer.length) != -1) {
// read the jar entry // read the jar entry
} }
jis.close(); }
if (entry.getCertificates() != null) { if (entry.getCertificates() != null) {
isSigned = true; isSigned = true;
} }
System.out.println((isSigned ? "[signed] " : "\t ") + System.out.println((isSigned ? "[signed] " : "\t ") +
entry.getName()); entry.getName());
} }
file.close(); }
if (isSigned) { if (isSigned) {
System.out.println("\nJAR file has signed entries"); System.out.println("\nJAR file has signed entries");
......
...@@ -44,14 +44,17 @@ public class Available { ...@@ -44,14 +44,17 @@ public class Available {
File f = new File(System.getProperty("test.src", "."), "input.jar"); File f = new File(System.getProperty("test.src", "."), "input.jar");
// test ZipInputStream // test ZipInputStream
ZipInputStream z = new ZipInputStream(new FileInputStream(f)); try (FileInputStream fis = new FileInputStream(f);
ZipInputStream z = new ZipInputStream(fis))
{
z.getNextEntry(); z.getNextEntry();
tryAvail(z); tryAvail(z);
}
// test InflaterInputStream // test InflaterInputStream
ZipFile zfile = new ZipFile(f); try (ZipFile zfile = new ZipFile(f)) {
tryAvail(zfile.getInputStream(zfile.getEntry("Available.java"))); tryAvail(zfile.getInputStream(zfile.getEntry("Available.java")));
z.close(); }
} }
static void tryAvail(InputStream in) throws Exception { static void tryAvail(InputStream in) throws Exception {
...@@ -67,7 +70,7 @@ public class Available { ...@@ -67,7 +70,7 @@ public class Available {
// To reproduce 4401122 // To reproduce 4401122
private static void test2() throws Exception { private static void test2() throws Exception {
File f = new File(System.getProperty("test.src", "."), "input.jar"); File f = new File(System.getProperty("test.src", "."), "input.jar");
ZipFile zf = new ZipFile(f); try (ZipFile zf = new ZipFile(f)) {
InputStream in = zf.getInputStream(zf.getEntry("Available.java")); InputStream in = zf.getInputStream(zf.getEntry("Available.java"));
int initialAvailable = in.available(); int initialAvailable = in.available();
...@@ -82,5 +85,6 @@ public class Available { ...@@ -82,5 +85,6 @@ public class Available {
if (in.available() != 0) if (in.available() != 0)
throw new RuntimeException(); throw new RuntimeException();
} }
}
} }
...@@ -53,8 +53,7 @@ public class FileBuilder { ...@@ -53,8 +53,7 @@ public class FileBuilder {
filetype.equals("SlightlyCompressible"))) filetype.equals("SlightlyCompressible")))
usageError(); usageError();
RandomAccessFile raf = new RandomAccessFile(filename, "rw"); try (RandomAccessFile raf = new RandomAccessFile(filename, "rw")) {
if (filetype.equals("SlightlyCompressible")) { if (filetype.equals("SlightlyCompressible")) {
byte[] randomBytes = new byte[16384]; byte[] randomBytes = new byte[16384];
byte[] nullBytes = new byte[randomBytes.length/10]; byte[] nullBytes = new byte[randomBytes.length/10];
...@@ -72,6 +71,6 @@ public class FileBuilder { ...@@ -72,6 +71,6 @@ public class FileBuilder {
raf.seek(filesize-filenameBytes.length); raf.seek(filesize-filenameBytes.length);
raf.write(filenameBytes); raf.write(filenameBytes);
raf.setLength(filesize); raf.setLength(filesize);
raf.close(); }
} }
} }
...@@ -64,16 +64,13 @@ public class Accordion { ...@@ -64,16 +64,13 @@ public class Accordion {
System.out.println("count="+count); System.out.println("count="+count);
Thread compressor = new Thread() { public void run() { Thread compressor = new Thread() { public void run() {
try { try (GZIPOutputStream s = new GZIPOutputStream(out)) {
final GZIPOutputStream s = new GZIPOutputStream(out);
for (long i = 0; i < count; i++) for (long i = 0; i < count; i++)
s.write(data, 0, data.length); s.write(data, 0, data.length);
s.close();
} catch (Throwable t) { trouble = t; }}}; } catch (Throwable t) { trouble = t; }}};
Thread uncompressor = new Thread() { public void run() { Thread uncompressor = new Thread() { public void run() {
try { try (GZIPInputStream s = new GZIPInputStream(in)) {
final GZIPInputStream s = new GZIPInputStream(in);
final byte[] maybeBytes = new byte[data.length]; final byte[] maybeBytes = new byte[data.length];
for (long i = 0; i < count; i++) { for (long i = 0; i < count; i++) {
readFully(s, maybeBytes); readFully(s, maybeBytes);
...@@ -82,7 +79,6 @@ public class Accordion { ...@@ -82,7 +79,6 @@ public class Accordion {
} }
if (s.read(maybeBytes, 0, 1) > 0) if (s.read(maybeBytes, 0, 1) > 0)
throw new Exception("Unexpected NON-EOF"); throw new Exception("Unexpected NON-EOF");
s.close();
} catch (Throwable t) { trouble = t; }}}; } catch (Throwable t) { trouble = t; }}};
compressor.start(); uncompressor.start(); compressor.start(); uncompressor.start();
......
...@@ -44,9 +44,9 @@ public class GZIPInputStreamRead { ...@@ -44,9 +44,9 @@ public class GZIPInputStreamRead {
rnd.nextBytes(src); rnd.nextBytes(src);
srcBAOS.write(src); srcBAOS.write(src);
GZIPOutputStream gzos = new GZIPOutputStream(dstBAOS); try (GZIPOutputStream gzos = new GZIPOutputStream(dstBAOS)) {
gzos.write(src); gzos.write(src);
gzos.close(); }
} }
byte[] srcBytes = srcBAOS.toByteArray(); byte[] srcBytes = srcBAOS.toByteArray();
byte[] dstBytes = dstBAOS.toByteArray(); byte[] dstBytes = dstBAOS.toByteArray();
...@@ -75,9 +75,9 @@ public class GZIPInputStreamRead { ...@@ -75,9 +75,9 @@ public class GZIPInputStreamRead {
int readBufSize, int gzisBufSize) int readBufSize, int gzisBufSize)
throws Throwable throws Throwable
{ {
GZIPInputStream gzis = new GZIPInputStream( try (ByteArrayInputStream bais = new ByteArrayInputStream(dst);
new ByteArrayInputStream(dst), GZIPInputStream gzis = new GZIPInputStream(bais, gzisBufSize))
gzisBufSize); {
byte[] result = new byte[src.length + 10]; byte[] result = new byte[src.length + 10];
byte[] buf = new byte[readBufSize]; byte[] buf = new byte[readBufSize];
int n = 0; int n = 0;
...@@ -95,6 +95,6 @@ public class GZIPInputStreamRead { ...@@ -95,6 +95,6 @@ public class GZIPInputStreamRead {
", src.len=" + src.length + ", src.len=" + src.length +
", read=" + off); ", read=" + off);
} }
gzis.close(); }
} }
} }
...@@ -134,14 +134,14 @@ public class InflateIn_DeflateOut { ...@@ -134,14 +134,14 @@ public class InflateIn_DeflateOut {
PairedOutputStream pos = new PairedOutputStream(pis); PairedOutputStream pos = new PairedOutputStream(pis);
pis.setPairedOutputStream(pos); pis.setPairedOutputStream(pos);
DeflaterOutputStream dos = new DeflaterOutputStream(pos, true);
byte[] data = new byte[random.nextInt(1024 * 1024)]; byte[] data = new byte[random.nextInt(1024 * 1024)];
byte[] buf = new byte[data.length]; byte[] buf = new byte[data.length];
random.nextBytes(data); random.nextBytes(data);
try (DeflaterOutputStream dos = new DeflaterOutputStream(pos, true)) {
dos.write(data); dos.write(data);
dos.close(); }
check(readFully(iis, buf, buf.length)); check(readFully(iis, buf, buf.length));
check(Arrays.equals(data, buf)); check(Arrays.equals(data, buf));
} }
......
...@@ -85,7 +85,7 @@ public class InfoZip { ...@@ -85,7 +85,7 @@ public class InfoZip {
//---------------------------------------------------------------- //----------------------------------------------------------------
File f = new File("InfoZip.zip"); File f = new File("InfoZip.zip");
OutputStream os = new FileOutputStream(f); try (OutputStream os = new FileOutputStream(f)) {
os.write(new byte[] os.write(new byte[]
{'P', 'K', 3, 4, 10, 0, 0, 0, 0, 0, -68, 8, 'k', {'P', 'K', 3, 4, 10, 0, 0, 0, 0, 0, -68, 8, 'k',
'2', 'V', -7, 'm', 9, 20, 0, 0, 0, 20, 0, 0, 0, '2', 'V', -7, 'm', 9, 20, 0, 0, 0, 20, 0, 0, 0,
...@@ -100,26 +100,22 @@ public class InfoZip { ...@@ -100,26 +100,22 @@ public class InfoZip {
'i', 'l', 'e', 'U', 'T', 5, 0, 3, 't', '_', '1', 'B', 'U', 'i', 'l', 'e', 'U', 'T', 5, 0, 3, 't', '_', '1', 'B', 'U',
'x', 0, 0, 'P', 'K', 5, 6, 0, 0, 0, 0, 1, 0, 'x', 0, 0, 'P', 'K', 5, 6, 0, 0, 0, 0, 1, 0,
1, 0, 'C', 0, 0, 0, 'O', 0, 0, 0, 0, 0, }); 1, 0, 'C', 0, 0, 0, 'O', 0, 0, 0, 0, 0, });
os.close(); }
ZipFile zf = new ZipFile(f);
ZipEntry ze = null; ZipEntry ze = null;
try { try (ZipFile zf = new ZipFile(f)) {
Enumeration<? extends ZipEntry> entries = zf.entries(); Enumeration<? extends ZipEntry> entries = zf.entries();
ze = entries.nextElement(); ze = entries.nextElement();
check(! entries.hasMoreElements()); check(! entries.hasMoreElements());
checkZipEntry(ze, contents(zf, ze)); checkZipEntry(ze, contents(zf, ze));
} finally {
zf.close();
} }
ZipInputStream is = new ZipInputStream(new FileInputStream(f)); try (FileInputStream fis = new FileInputStream(f);
try { ZipInputStream is = new ZipInputStream(fis))
{
ze = is.getNextEntry(); ze = is.getNextEntry();
checkZipEntry(ze, contents(is)); checkZipEntry(ze, contents(is));
check(is.getNextEntry() == null); check(is.getNextEntry() == null);
} finally {
is.close();
} }
f.delete(); f.delete();
System.out.printf("passed = %d, failed = %d%n", passed, failed); System.out.printf("passed = %d, failed = %d%n", passed, failed);
......
...@@ -98,8 +98,10 @@ public class LargeZip { ...@@ -98,8 +98,10 @@ public class LargeZip {
} }
data = baos.toByteArray(); data = baos.toByteArray();
ZipOutputStream zos = new ZipOutputStream( try (FileOutputStream fos = new FileOutputStream(largeFile);
new BufferedOutputStream(new FileOutputStream(largeFile))); BufferedOutputStream bos = new BufferedOutputStream(fos);
ZipOutputStream zos = new ZipOutputStream(bos))
{
long length = 0; long length = 0;
while (length < fileSize) { while (length < fileSize) {
ZipEntry ze = new ZipEntry("entry-" + length); ZipEntry ze = new ZipEntry("entry-" + length);
...@@ -110,7 +112,7 @@ public class LargeZip { ...@@ -110,7 +112,7 @@ public class LargeZip {
length = largeFile.length(); length = largeFile.length();
} }
System.out.println("Last entry written is " + lastEntryName); System.out.println("Last entry written is " + lastEntryName);
zos.close(); }
} }
static void readLargeZip1() throws Throwable { static void readLargeZip1() throws Throwable {
...@@ -143,8 +145,10 @@ public class LargeZip { ...@@ -143,8 +145,10 @@ public class LargeZip {
static void readLargeZip2() throws Throwable { static void readLargeZip2() throws Throwable {
ZipInputStream zis = new ZipInputStream( try (FileInputStream fis = new FileInputStream(largeFile);
new BufferedInputStream(new FileInputStream(largeFile))); BufferedInputStream bis = new BufferedInputStream(fis);
ZipInputStream zis = new ZipInputStream(bis))
{
ZipEntry entry = null; ZipEntry entry = null;
String entryName = null; String entryName = null;
int count = 0; int count = 0;
...@@ -169,7 +173,7 @@ public class LargeZip { ...@@ -169,7 +173,7 @@ public class LargeZip {
baos.close(); baos.close();
check(Arrays.equals(data, baos.toByteArray())); check(Arrays.equals(data, baos.toByteArray()));
check(zis.getNextEntry() == null); check(zis.getNextEntry() == null);
zis.close(); }
} }
......
...@@ -78,67 +78,49 @@ public class TestEmptyZip { ...@@ -78,67 +78,49 @@ public class TestEmptyZip {
pass(); pass();
} }
} }
ZipInputStream zis = null; try (FileInputStream fis = new FileInputStream(f);
try { ZipInputStream zis = new ZipInputStream(fis))
zis = new ZipInputStream(new FileInputStream(f)); {
ZipEntry ze = zis.getNextEntry(); ZipEntry ze = zis.getNextEntry();
check(ze == null); check(ze == null);
} catch (IOException ex) { } catch (IOException ex) {
unexpected(ex); unexpected(ex);
} finally {
if (zis != null) zis.close();
} }
} }
static void write(File f) throws Exception { static void write(File f) throws Exception {
ZipOutputStream zos = null; try (FileOutputStream fis = new FileOutputStream(f);
try { ZipOutputStream zos = new ZipOutputStream(fis))
zos = new ZipOutputStream(new FileOutputStream(f)); {
zos.finish(); zos.finish();
zos.close();
pass(); pass();
} catch (Exception ex) { } catch (Exception ex) {
unexpected(ex); unexpected(ex);
} finally {
if (zos != null) {
zos.close();
}
} }
} }
static void readFile(File f) throws Exception { static void readFile(File f) throws Exception {
ZipFile zf = null; try (ZipFile zf = new ZipFile(f)) {
try {
zf = new ZipFile(f);
Enumeration e = zf.entries(); Enumeration e = zf.entries();
while (e.hasMoreElements()) { while (e.hasMoreElements()) {
ZipEntry entry = (ZipEntry) e.nextElement(); ZipEntry entry = (ZipEntry) e.nextElement();
fail(); fail();
} }
zf.close();
pass(); pass();
} catch (Exception ex) { } catch (Exception ex) {
unexpected(ex); unexpected(ex);
} finally {
if (zf != null) {
zf.close();
}
} }
} }
static void readStream(File f) throws Exception { static void readStream(File f) throws Exception {
ZipInputStream zis = null; try (FileInputStream fis = new FileInputStream(f);
try { ZipInputStream zis = new ZipInputStream(fis))
zis = new ZipInputStream(new FileInputStream(f)); {
ZipEntry ze = zis.getNextEntry(); ZipEntry ze = zis.getNextEntry();
check(ze == null); check(ze == null);
byte[] buf = new byte[1024]; byte[] buf = new byte[1024];
check(zis.read(buf, 0, 1024) == -1); check(zis.read(buf, 0, 1024) == -1);
} finally {
if (zis != null) {
zis.close();
}
} }
} }
......
...@@ -57,7 +57,7 @@ public class ZipCoding { ...@@ -57,7 +57,7 @@ public class ZipCoding {
String name, String comment, byte[] bb) String name, String comment, byte[] bb)
throws Exception throws Exception
{ {
ZipInputStream zis = new ZipInputStream(is, cs); try (ZipInputStream zis = new ZipInputStream(is, cs)) {
ZipEntry e = zis.getNextEntry(); ZipEntry e = zis.getNextEntry();
if (e == null || ! name.equals(e.getName())) if (e == null || ! name.equals(e.getName()))
throw new RuntimeException("ZipIS name doesn't match!"); throw new RuntimeException("ZipIS name doesn't match!");
...@@ -67,14 +67,14 @@ public class ZipCoding { ...@@ -67,14 +67,14 @@ public class ZipCoding {
!Arrays.equals(bb, Arrays.copyOf(bBuf, n))) { !Arrays.equals(bb, Arrays.copyOf(bBuf, n))) {
throw new RuntimeException("ZipIS content doesn't match!"); throw new RuntimeException("ZipIS content doesn't match!");
} }
zis.close(); }
} }
static void testZipFile(File f, Charset cs, static void testZipFile(File f, Charset cs,
String name, String comment, byte[] bb) String name, String comment, byte[] bb)
throws Exception throws Exception
{ {
ZipFile zf = new ZipFile(f, cs); try (ZipFile zf = new ZipFile(f, cs)) {
Enumeration<? extends ZipEntry> zes = zf.entries(); Enumeration<? extends ZipEntry> zes = zf.entries();
ZipEntry e = (ZipEntry)zes.nextElement(); ZipEntry e = (ZipEntry)zes.nextElement();
if (! name.equals(e.getName()) || if (! name.equals(e.getName()) ||
...@@ -93,23 +93,22 @@ public class ZipCoding { ...@@ -93,23 +93,22 @@ public class ZipCoding {
!Arrays.equals(bb, Arrays.copyOf(bBuf, n))) { !Arrays.equals(bb, Arrays.copyOf(bBuf, n))) {
throw new RuntimeException("ZipFile content doesn't match!"); throw new RuntimeException("ZipFile content doesn't match!");
} }
zf.close(); }
} }
static void test(String csn, String name, String comment) static void test(String csn, String name, String comment)
throws Exception throws Exception
{ {
byte[] bb = "This is the conent of the zipfile".getBytes("ISO-8859-1"); byte[] bb = "This is the content of the zipfile".getBytes("ISO-8859-1");
Charset cs = Charset.forName(csn); Charset cs = Charset.forName(csn);
ByteArrayOutputStream baos = new ByteArrayOutputStream(); ByteArrayOutputStream baos = new ByteArrayOutputStream();
ZipOutputStream zos = new ZipOutputStream(baos, cs); try (ZipOutputStream zos = new ZipOutputStream(baos, cs)) {
ZipEntry e = new ZipEntry(name); ZipEntry e = new ZipEntry(name);
e.setComment(comment); e.setComment(comment);
zos.putNextEntry(e); zos.putNextEntry(e);
zos.write(bb, 0, bb.length); zos.write(bb, 0, bb.length);
zos.closeEntry(); zos.closeEntry();
zos.close(); }
ByteArrayInputStream bis = new ByteArrayInputStream(baos.toByteArray()); ByteArrayInputStream bis = new ByteArrayInputStream(baos.toByteArray());
testZipInputStream(bis, cs, name, comment, bb); testZipInputStream(bis, cs, name, comment, bb);
...@@ -121,9 +120,9 @@ public class ZipCoding { ...@@ -121,9 +120,9 @@ public class ZipCoding {
File f = new File(new File(System.getProperty("test.dir", ".")), File f = new File(new File(System.getProperty("test.dir", ".")),
"zfcoding.zip"); "zfcoding.zip");
FileOutputStream fos = new FileOutputStream(f); try (FileOutputStream fos = new FileOutputStream(f)) {
baos.writeTo(fos); baos.writeTo(fos);
fos.close(); }
testZipFile(f, cs, name, comment, bb); testZipFile(f, cs, name, comment, bb);
if ("utf-8".equals(csn)) { if ("utf-8".equals(csn)) {
testZipFile(f, Charset.forName("MS932"), name, comment, bb); testZipFile(f, Charset.forName("MS932"), name, comment, bb);
......
...@@ -201,13 +201,12 @@ public class Assortment { ...@@ -201,13 +201,12 @@ public class Assortment {
//---------------------------------------------------------------- //----------------------------------------------------------------
// Write zip file using ZipOutputStream // Write zip file using ZipOutputStream
//---------------------------------------------------------------- //----------------------------------------------------------------
ZipOutputStream zos = new ZipOutputStream( try (FileOutputStream fos = new FileOutputStream(zipName);
new FileOutputStream(zipName)); ZipOutputStream zos = new ZipOutputStream(fos))
{
for (Entry e : entries) for (Entry e : entries)
e.write(zos); e.write(zos);
}
zos.close();
//---------------------------------------------------------------- //----------------------------------------------------------------
// Verify zip file contents using JarFile class // Verify zip file contents using JarFile class
......
...@@ -57,16 +57,15 @@ public class Comment { ...@@ -57,16 +57,15 @@ public class Comment {
private static void writeZipFile(String name, String comment) private static void writeZipFile(String name, String comment)
throws IOException throws IOException
{ {
ZipOutputStream zos = new ZipOutputStream(new FileOutputStream(name)); try (FileOutputStream fos = new FileOutputStream(name);
try { ZipOutputStream zos = new ZipOutputStream(fos))
{
zos.setComment(comment); zos.setComment(comment);
ZipEntry ze = new ZipEntry(entryName); ZipEntry ze = new ZipEntry(entryName);
ze.setMethod(ZipEntry.DEFLATED); ze.setMethod(ZipEntry.DEFLATED);
zos.putNextEntry(ze); zos.putNextEntry(ze);
new DataOutputStream(zos).writeUTF(entryContents); new DataOutputStream(zos).writeUTF(entryContents);
zos.closeEntry(); zos.closeEntry();
} finally {
zos.close();
} }
} }
...@@ -74,15 +73,16 @@ public class Comment { ...@@ -74,15 +73,16 @@ public class Comment {
throws Exception throws Exception
{ {
// Check that Zip entry was correctly written. // Check that Zip entry was correctly written.
ZipFile zipFile = new ZipFile(name); try (ZipFile zipFile = new ZipFile(name)) {
ZipEntry zipEntry = zipFile.getEntry(entryName); ZipEntry zipEntry = zipFile.getEntry(entryName);
InputStream is = zipFile.getInputStream(zipEntry); InputStream is = zipFile.getInputStream(zipEntry);
String result = new DataInputStream(is).readUTF(); String result = new DataInputStream(is).readUTF();
if (!result.equals(entryContents)) if (!result.equals(entryContents))
throw new Exception("Entry contents corrupted"); throw new Exception("Entry contents corrupted");
}
try (RandomAccessFile file = new RandomAccessFile(name, "r")) {
// Check that comment length was correctly written. // Check that comment length was correctly written.
RandomAccessFile file = new RandomAccessFile(name, "r");
file.seek(file.length() - comment.length() file.seek(file.length() - comment.length()
- ZipFile.ENDHDR + ZipFile.ENDCOM); - ZipFile.ENDHDR + ZipFile.ENDCOM);
int b1 = file.readUnsignedByte(); int b1 = file.readUnsignedByte();
...@@ -94,11 +94,10 @@ public class Comment { ...@@ -94,11 +94,10 @@ public class Comment {
file.seek(file.length() - comment.length()); file.seek(file.length() - comment.length());
byte [] bytes = new byte [comment.length()]; byte [] bytes = new byte [comment.length()];
file.readFully(bytes); file.readFully(bytes);
zipFile.close();
file.close();
if (! comment.equals(new String(bytes, "UTF8"))) if (! comment.equals(new String(bytes, "UTF8")))
throw new Exception("Zip file comment corrupted"); throw new Exception("Zip file comment corrupted");
} }
}
private static String buildComment(int length) { private static String buildComment(int length) {
StringBuffer result = new StringBuffer(); StringBuffer result = new StringBuffer();
......
...@@ -31,8 +31,8 @@ import java.util.zip.*; ...@@ -31,8 +31,8 @@ import java.util.zip.*;
public class CopyJar { public class CopyJar {
public static void main(String args[]) throws Exception { public static void main(String args[]) throws Exception {
ZipFile zf = new ZipFile(new File(System.getProperty("test.src", "."), try (ZipFile zf = new ZipFile(new File(System.getProperty("test.src", "."),
"input.jar")); "input.jar"))) {
ZipEntry ze = zf.getEntry("ReleaseInflater.java"); ZipEntry ze = zf.getEntry("ReleaseInflater.java");
ZipOutputStream zos = new ZipOutputStream(new ByteArrayOutputStream()); ZipOutputStream zos = new ZipOutputStream(new ByteArrayOutputStream());
InputStream in = zf.getInputStream(ze); InputStream in = zf.getInputStream(ze);
...@@ -43,6 +43,6 @@ public class CopyJar { ...@@ -43,6 +43,6 @@ public class CopyJar {
zos.write(b, 0, n); zos.write(b, 0, n);
} }
zos.close(); zos.close();
zf.close(); }
} }
} }
...@@ -47,21 +47,19 @@ public class CorruptedZipFiles { ...@@ -47,21 +47,19 @@ public class CorruptedZipFiles {
} }
public static void main(String[] args) throws Exception { public static void main(String[] args) throws Exception {
ZipOutputStream zos = new ZipOutputStream(new FileOutputStream("x.zip")); try (FileOutputStream fos = new FileOutputStream("x.zip");
try { ZipOutputStream zos = new ZipOutputStream(fos))
{
ZipEntry e = new ZipEntry("x"); ZipEntry e = new ZipEntry("x");
zos.putNextEntry(e); zos.putNextEntry(e);
zos.write((int)'x'); zos.write((int)'x');
} finally {
zos.close();
} }
int len = (int)(new File("x.zip").length()); int len = (int)(new File("x.zip").length());
byte[] good = new byte[len]; byte[] good = new byte[len];
FileInputStream fis = new FileInputStream("x.zip"); try (FileInputStream fis = new FileInputStream("x.zip")) {
fis.read(good); fis.read(good);
fis.close(); }
fis = null;
new File("x.zip").delete(); new File("x.zip").delete();
int endpos = len - ENDHDR; int endpos = len - ENDHDR;
...@@ -150,17 +148,14 @@ public class CorruptedZipFiles { ...@@ -150,17 +148,14 @@ public class CorruptedZipFiles {
boolean getInputStream) { boolean getInputStream) {
String zipName = "bad" + (uniquifier++) + ".zip"; String zipName = "bad" + (uniquifier++) + ".zip";
try { try {
FileOutputStream fos = new FileOutputStream(zipName); try (FileOutputStream fos = new FileOutputStream(zipName)) {
fos.write(data); fos.write(data);
fos.close(); }
ZipFile zf = new ZipFile(zipName); try (ZipFile zf = new ZipFile(zipName)) {
try {
if (getInputStream) { if (getInputStream) {
InputStream is = zf.getInputStream(new ZipEntry("x")); InputStream is = zf.getInputStream(new ZipEntry("x"));
is.read(); is.read();
} }
} finally {
zf.close();
} }
fail("Failed to throw expected ZipException"); fail("Failed to throw expected ZipException");
} catch (ZipException e) { } catch (ZipException e) {
...@@ -170,8 +165,7 @@ public class CorruptedZipFiles { ...@@ -170,8 +165,7 @@ public class CorruptedZipFiles {
unexpected(e); unexpected(e);
} catch (Throwable t) { } catch (Throwable t) {
unexpected(t); unexpected(t);
} } finally {
finally {
new File(zipName).delete(); new File(zipName).delete();
} }
} }
......
...@@ -53,20 +53,20 @@ public class DeleteTempJar ...@@ -53,20 +53,20 @@ public class DeleteTempJar
{ {
final File zf = File.createTempFile("deletetemp", ".jar"); final File zf = File.createTempFile("deletetemp", ".jar");
zf.deleteOnExit(); zf.deleteOnExit();
JarOutputStream jos = new JarOutputStream( try (FileOutputStream fos = new FileOutputStream(zf);
new FileOutputStream(zf)); JarOutputStream jos = new JarOutputStream(fos))
{
JarEntry je = new JarEntry("entry"); JarEntry je = new JarEntry("entry");
jos.putNextEntry(je); jos.putNextEntry(je);
jos.write("hello, world".getBytes("ASCII")); jos.write("hello, world".getBytes("ASCII"));
jos.close(); }
HttpServer server = HttpServer.create( HttpServer server = HttpServer.create(
new InetSocketAddress((InetAddress) null, 0), 0); new InetSocketAddress((InetAddress) null, 0), 0);
HttpContext context = server.createContext("/", HttpContext context = server.createContext("/",
new HttpHandler() { new HttpHandler() {
public void handle(HttpExchange e) { public void handle(HttpExchange e) {
try { try (FileInputStream fis = new FileInputStream(zf)) {
FileInputStream fis = new FileInputStream(zf);
e.sendResponseHeaders(200, zf.length()); e.sendResponseHeaders(200, zf.length());
OutputStream os = e.getResponseBody(); OutputStream os = e.getResponseBody();
byte[] buf = new byte[1024]; byte[] buf = new byte[1024];
...@@ -74,10 +74,10 @@ public class DeleteTempJar ...@@ -74,10 +74,10 @@ public class DeleteTempJar
while ((count = fis.read(buf)) != -1) { while ((count = fis.read(buf)) != -1) {
os.write(buf, 0, count); os.write(buf, 0, count);
} }
fis.close();
e.close();
} catch (Exception ex) { } catch (Exception ex) {
unexpected(ex); unexpected(ex);
} finally {
e.close();
} }
} }
}); });
......
...@@ -33,10 +33,12 @@ import java.util.Enumeration; ...@@ -33,10 +33,12 @@ import java.util.Enumeration;
public class EnumAfterClose { public class EnumAfterClose {
public static void main(String args[]) throws Exception { public static void main(String args[]) throws Exception {
ZipFile zf = new ZipFile(new File(System.getProperty("test.src", "."), Enumeration e;
"input.zip")); try (ZipFile zf = new ZipFile(new File(System.getProperty("test.src", "."),
Enumeration e = zf.entries(); "input.zip"))) {
zf.close(); e = zf.entries();
}
// ensure that the ZipFile is closed before checking the Enumeration
try { try {
if (e.hasMoreElements()) { if (e.hasMoreElements()) {
ZipEntry ze = (ZipEntry)e.nextElement(); ZipEntry ze = (ZipEntry)e.nextElement();
......
...@@ -32,12 +32,12 @@ import java.util.zip.*; ...@@ -32,12 +32,12 @@ import java.util.zip.*;
public class GetDirEntry { public class GetDirEntry {
public static void main(String args[]) throws Exception { public static void main(String args[]) throws Exception {
ZipFile zf = new ZipFile(new File(System.getProperty("test.src", "."), try (ZipFile zf = new ZipFile(new File(System.getProperty("test.src", "."),
"input.jar")); "input.jar"))) {
ZipEntry ze = zf.getEntry("META-INF"); ZipEntry ze = zf.getEntry("META-INF");
if (ze == null) { if (ze == null) {
throw new Exception("failed to find a directory entry"); throw new Exception("failed to find a directory entry");
} }
zf.close(); }
} }
} }
...@@ -93,9 +93,10 @@ public class LargeZipFile { ...@@ -93,9 +93,10 @@ public class LargeZipFile {
baos.write(bb.array(), 0, DATA_SIZE); baos.write(bb.array(), 0, DATA_SIZE);
} }
data = baos.toByteArray(); data = baos.toByteArray();
try (FileOutputStream fos = new FileOutputStream(largeFile);
ZipOutputStream zos = new ZipOutputStream( BufferedOutputStream bos = new BufferedOutputStream(fos);
new BufferedOutputStream(new FileOutputStream(largeFile))); ZipOutputStream zos = new ZipOutputStream(bos))
{
long length = 0; long length = 0;
while (length < fileSize) { while (length < fileSize) {
ZipEntry ze = new ZipEntry("entry-" + length); ZipEntry ze = new ZipEntry("entry-" + length);
...@@ -106,11 +107,11 @@ public class LargeZipFile { ...@@ -106,11 +107,11 @@ public class LargeZipFile {
length = largeFile.length(); length = largeFile.length();
} }
System.out.println("Last entry written is " + lastEntryName); System.out.println("Last entry written is " + lastEntryName);
zos.close(); }
} }
static void readLargeZip() throws Throwable { static void readLargeZip() throws Throwable {
ZipFile zipFile = new ZipFile(largeFile); try (ZipFile zipFile = new ZipFile(largeFile)) {
ZipEntry entry = null; ZipEntry entry = null;
String entryName = null; String entryName = null;
int count = 0; int count = 0;
...@@ -135,9 +136,7 @@ public class LargeZipFile { ...@@ -135,9 +136,7 @@ public class LargeZipFile {
is.close(); is.close();
check(Arrays.equals(data, baos.toByteArray())); check(Arrays.equals(data, baos.toByteArray()));
} }
try { }
zipFile.close();
} catch (IOException ioe) {/* what can you do */ }
} }
//--------------------- Infrastructure --------------------------- //--------------------- Infrastructure ---------------------------
......
...@@ -55,10 +55,10 @@ public class ManyEntries { ...@@ -55,10 +55,10 @@ public class ManyEntries {
File zipFile = new File(++uniquifier + ".zip"); File zipFile = new File(++uniquifier + ".zip");
try { try {
zipFile.delete(); zipFile.delete();
ZipOutputStream zos = new ZipOutputStream( try (FileOutputStream fos = new FileOutputStream(zipFile);
new BufferedOutputStream( BufferedOutputStream bos = new BufferedOutputStream(fos);
new FileOutputStream(zipFile))); ZipOutputStream zos = new ZipOutputStream(bos))
try { {
for (int i = 0; i < N; i++) { for (int i = 0; i < N; i++) {
ZipEntry e = new ZipEntry("DIR/"+i); ZipEntry e = new ZipEntry("DIR/"+i);
e.setMethod(method); e.setMethod(method);
...@@ -75,13 +75,9 @@ public class ManyEntries { ...@@ -75,13 +75,9 @@ public class ManyEntries {
zos.putNextEntry(e); zos.putNextEntry(e);
zos.write(i); zos.write(i);
} }
} finally {
zos.close();
zos = null;
} }
ZipFile zip = zip = new ZipFile(zipFile); try (ZipFile zip = new ZipFile(zipFile)) {
try {
if (! (zip.size() == N)) if (! (zip.size() == N))
throw new Exception("Bad ZipFile size: " + zip.size()); throw new Exception("Bad ZipFile size: " + zip.size());
Enumeration entries = zip.entries(); Enumeration entries = zip.entries();
...@@ -104,11 +100,8 @@ public class ManyEntries { ...@@ -104,11 +100,8 @@ public class ManyEntries {
} }
if (entries.hasMoreElements()) if (entries.hasMoreElements())
throw new Exception("too many elements"); throw new Exception("too many elements");
} finally {
zip.close();
}
} }
finally { } finally {
zipFile.delete(); zipFile.delete();
} }
} }
......
...@@ -51,14 +51,14 @@ public class ManyZipFiles { ...@@ -51,14 +51,14 @@ public class ManyZipFiles {
// Create some zip data // Create some zip data
ByteArrayOutputStream baos = new ByteArrayOutputStream(); ByteArrayOutputStream baos = new ByteArrayOutputStream();
ZipOutputStream zos = new ZipOutputStream(baos); try (ZipOutputStream zos = new ZipOutputStream(baos)) {
ZipEntry ze = new ZipEntry("test"); ZipEntry ze = new ZipEntry("test");
zos.putNextEntry(ze); zos.putNextEntry(ze);
byte[] hello = "hello, world".getBytes("ASCII"); byte[] hello = "hello, world".getBytes("ASCII");
zos.write(hello, 0, hello.length); zos.write(hello, 0, hello.length);
zos.closeEntry(); zos.closeEntry();
zos.finish(); zos.finish();
zos.close(); }
byte[] data = baos.toByteArray(); byte[] data = baos.toByteArray();
ZipFile zips[] = new ZipFile[numFiles]; ZipFile zips[] = new ZipFile[numFiles];
...@@ -90,9 +90,9 @@ public class ManyZipFiles { ...@@ -90,9 +90,9 @@ public class ManyZipFiles {
for (int i = 0; i < numFiles; i++) { for (int i = 0; i < numFiles; i++) {
File f = File.createTempFile("test", ".zip", tmpdir); File f = File.createTempFile("test", ".zip", tmpdir);
f.deleteOnExit(); f.deleteOnExit();
FileOutputStream fos = new FileOutputStream(f); try (FileOutputStream fos = new FileOutputStream(f)) {
fos.write(data, 0, data.length); fos.write(data, 0, data.length);
fos.close(); }
try { try {
zips[i] = new ZipFile(f); zips[i] = new ZipFile(f);
} catch (Throwable t) { } catch (Throwable t) {
...@@ -102,11 +102,12 @@ public class ManyZipFiles { ...@@ -102,11 +102,12 @@ public class ManyZipFiles {
} }
} }
} finally { } finally {
// This finally block is due to bug 4171239. On windows, if the // This finally block is due to bug 4171239. On Windows, if the
// file is still open at the end of the VM, deleteOnExit won't // file is still open at the end of the VM, deleteOnExit won't
// take place. "new ZipFile(...)" opens the zip file, so we have // take place. "new ZipFile(...)" opens the zip file, so we have
// to explicity close those opened above. This finally block can // to explicitly close those opened above. This finally block can
// be removed when 4171239 is fixed. // be removed when 4171239 is fixed. See also 6357433, against which
// 4171239 was closed as a duplicate.
for (int i = 0; i < numFiles; i++) { for (int i = 0; i < numFiles; i++) {
if (zips[i] != null) { if (zips[i] != null) {
try { try {
......
...@@ -34,10 +34,13 @@ import java.util.*; ...@@ -34,10 +34,13 @@ import java.util.*;
public class ReadAfterClose { public class ReadAfterClose {
public static void main(String[] argv) throws Exception { public static void main(String[] argv) throws Exception {
ZipFile zf = new ZipFile(new File(System.getProperty("test.src","."),"crash.jar")); InputStream in;
try (ZipFile zf = new ZipFile(
new File(System.getProperty("test.src","."),"crash.jar"))) {
ZipEntry zent = zf.getEntry("Test.java"); ZipEntry zent = zf.getEntry("Test.java");
InputStream in = zf.getInputStream(zent); in = zf.getInputStream(zent);
zf.close(); }
// ensure zf is closed at this point
try { try {
in.read(); in.read();
} catch (IOException e) { } catch (IOException e) {
......
...@@ -27,6 +27,10 @@ ...@@ -27,6 +27,10 @@
*/ */
import java.io.*; import java.io.*;
import java.nio.file.Files;
import java.nio.file.Paths;
import java.nio.file.StandardCopyOption;
import java.nio.file.StandardOpenOption;
import java.util.zip.*; import java.util.zip.*;
public class ReadZip { public class ReadZip {
...@@ -38,9 +42,8 @@ public class ReadZip { ...@@ -38,9 +42,8 @@ public class ReadZip {
} }
public static void main(String args[]) throws Exception { public static void main(String args[]) throws Exception {
ZipFile zf = new ZipFile(new File(System.getProperty("test.src", "."), try (ZipFile zf = new ZipFile(new File(System.getProperty("test.src", "."),
"input.zip")); "input.zip"))) {
// Make sure we throw NPE on null objects // Make sure we throw NPE on null objects
try { unreached (zf.getEntry(null)); } try { unreached (zf.getEntry(null)); }
catch (NullPointerException e) {} catch (NullPointerException e) {}
...@@ -52,57 +55,49 @@ public class ReadZip { ...@@ -52,57 +55,49 @@ public class ReadZip {
if (ze == null) { if (ze == null) {
throw new Exception("cannot read from zip file"); throw new Exception("cannot read from zip file");
} }
zf.close(); }
// Make sure we can read the zip file that has some garbage // Make sure we can read the zip file that has some garbage
// bytes padded at the end. // bytes padded at the end.
FileInputStream fis = new FileInputStream( File newZip = new File(System.getProperty("test.dir", "."), "input2.zip");
new File(System.getProperty("test.src", "."), Files.copy(Paths.get(System.getProperty("test.src", ""), "input.zip"),
"input.zip")); newZip.toPath(), StandardCopyOption.REPLACE_EXISTING);
File newZip = new File(System.getProperty("test.dir", "."),
"input2.zip");
FileOutputStream fos = new FileOutputStream(newZip);
byte[] buf = new byte[1024];
int n = 0;
while ((n = fis.read(buf)) != -1) {
fos.write(buf, 0, n);
}
fis.close();
// pad some bytes // pad some bytes
fos.write(1); fos.write(3); fos.write(5); fos.write(7); try (OutputStream os = Files.newOutputStream(newZip.toPath(),
fos.close(); StandardOpenOption.APPEND)) {
try { os.write(1); os.write(3); os.write(5); os.write(7);
zf = new ZipFile(newZip); }
ze = zf.getEntry("ReadZip.java");
try (ZipFile zf = new ZipFile(newZip)) {
ZipEntry ze = zf.getEntry("ReadZip.java");
if (ze == null) { if (ze == null) {
throw new Exception("cannot read from zip file"); throw new Exception("cannot read from zip file");
} }
} finally { } finally {
zf.close();
newZip.delete(); newZip.delete();
} }
// Read zip file comment // Read zip file comment
try { try {
try (FileOutputStream fos = new FileOutputStream(newZip);
ZipOutputStream zos = new ZipOutputStream( ZipOutputStream zos = new ZipOutputStream(fos))
new FileOutputStream(newZip)); {
ze = new ZipEntry("ZipEntry"); ZipEntry ze = new ZipEntry("ZipEntry");
zos.putNextEntry(ze); zos.putNextEntry(ze);
zos.write(1); zos.write(2); zos.write(3); zos.write(4); zos.write(1); zos.write(2); zos.write(3); zos.write(4);
zos.closeEntry(); zos.closeEntry();
zos.setComment("This is the comment for testing"); zos.setComment("This is the comment for testing");
zos.close(); }
zf = new ZipFile(newZip); try (ZipFile zf = new ZipFile(newZip)) {
ze = zf.getEntry("ZipEntry"); ZipEntry ze = zf.getEntry("ZipEntry");
if (ze == null) if (ze == null)
throw new Exception("cannot read entry from zip file"); throw new Exception("cannot read entry from zip file");
if (!"This is the comment for testing".equals(zf.getComment())) if (!"This is the comment for testing".equals(zf.getComment()))
throw new Exception("cannot read comment from zip file"); throw new Exception("cannot read comment from zip file");
}
} finally { } finally {
zf.close();
newZip.delete(); newZip.delete();
} }
......
...@@ -38,14 +38,15 @@ public class ShortRead { ...@@ -38,14 +38,15 @@ public class ShortRead {
try { try {
final String entryName = "abc"; final String entryName = "abc";
final String data = "Data disponible"; final String data = "Data disponible";
final ZipOutputStream zos = try (FileOutputStream fos = new FileOutputStream(zFile);
new ZipOutputStream(new FileOutputStream(zFile)); ZipOutputStream zos = new ZipOutputStream(fos))
{
zos.putNextEntry(new ZipEntry(entryName)); zos.putNextEntry(new ZipEntry(entryName));
zos.write(data.getBytes("ASCII")); zos.write(data.getBytes("ASCII"));
zos.closeEntry(); zos.closeEntry();
zos.close(); }
final ZipFile zipFile = new ZipFile(zFile); try (ZipFile zipFile = new ZipFile(zFile)) {
final ZipEntry zentry = zipFile.getEntry(entryName); final ZipEntry zentry = zipFile.getEntry(entryName);
final InputStream inputStream = zipFile.getInputStream(zentry); final InputStream inputStream = zipFile.getInputStream(zentry);
System.out.printf("size=%d csize=%d available=%d%n", System.out.printf("size=%d csize=%d available=%d%n",
...@@ -57,8 +58,9 @@ public class ShortRead { ...@@ -57,8 +58,9 @@ public class ShortRead {
if (! new String(buf, "ASCII").equals(data) || if (! new String(buf, "ASCII").equals(data) ||
count != data.length()) count != data.length())
throw new Exception("short read?"); throw new Exception("short read?");
zipFile.close();
} }
finally { zFile.delete(); } } finally {
zFile.delete();
}
} }
} }
...@@ -322,20 +322,21 @@ public class zip { ...@@ -322,20 +322,21 @@ public class zip {
void create(OutputStream out) throws IOException void create(OutputStream out) throws IOException
{ {
ZipOutputStream zos = new ZipOutputStream(out, cs); try (ZipOutputStream zos = new ZipOutputStream(out, cs)) {
if (flag0) { if (flag0) {
zos.setMethod(ZipOutputStream.STORED); zos.setMethod(ZipOutputStream.STORED);
} }
for (File file: entries) { for (File file: entries) {
addFile(zos, file); addFile(zos, file);
} }
zos.close(); }
} }
boolean update(InputStream in, OutputStream out) throws IOException boolean update(InputStream in, OutputStream out) throws IOException
{ {
ZipInputStream zis = new ZipInputStream(in, cs); try (ZipInputStream zis = new ZipInputStream(in, cs);
ZipOutputStream zos = new ZipOutputStream(out, cs); ZipOutputStream zos = new ZipOutputStream(out, cs))
{
ZipEntry e = null; ZipEntry e = null;
byte[] buf = new byte[1024]; byte[] buf = new byte[1024];
int n = 0; int n = 0;
...@@ -371,8 +372,7 @@ public class zip { ...@@ -371,8 +372,7 @@ public class zip {
for (File f: entries) { for (File f: entries) {
addFile(zos, f); addFile(zos, f);
} }
zis.close(); }
zos.close();
return updateOk; return updateOk;
} }
...@@ -517,7 +517,7 @@ public class zip { ...@@ -517,7 +517,7 @@ public class zip {
} }
void extract(String fname, String files[]) throws IOException { 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(); Set<ZipEntry> dirs = newDirSet();
Enumeration<? extends ZipEntry> zes = zf.entries(); Enumeration<? extends ZipEntry> zes = zf.entries();
while (zes.hasMoreElements()) { while (zes.hasMoreElements()) {
...@@ -535,7 +535,7 @@ public class zip { ...@@ -535,7 +535,7 @@ public class zip {
} }
} }
} }
zf.close(); }
updateLastModifiedTime(dirs); updateLastModifiedTime(dirs);
} }
...@@ -607,12 +607,12 @@ public class zip { ...@@ -607,12 +607,12 @@ public class zip {
} }
void list(String fname, String files[]) throws IOException { 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(); Enumeration<? extends ZipEntry> zes = zf.entries();
while (zes.hasMoreElements()) { while (zes.hasMoreElements()) {
printEntry(zes.nextElement(), files); printEntry(zes.nextElement(), files);
} }
zf.close(); }
} }
void printEntry(ZipEntry e, String[] files) throws IOException { void printEntry(ZipEntry e, String[] files) throws IOException {
......
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册