Skip to content

Commit a9488ef

Browse files
committed
[MASSEMBLY-742] Fixed closeable on iterator in PlexusIoZipFileResourceCollection
Thanks to Dawid Weiss for reporting this bug and keeping an eye on me :)
1 parent 8317bb5 commit a9488ef

File tree

1 file changed

+41
-20
lines changed

1 file changed

+41
-20
lines changed
Lines changed: 41 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,13 @@
11
package org.codehaus.plexus.archiver.zip;
22

3+
import java.io.Closeable;
34
import java.io.File;
45
import java.io.IOException;
56
import java.util.Enumeration;
67
import java.util.Iterator;
78

89
import org.apache.commons.compress.archivers.zip.ZipArchiveEntry;
10+
import org.apache.commons.compress.archivers.zip.ZipFile;
911
import org.codehaus.plexus.components.io.resources.AbstractPlexusIoArchiveResourceCollection;
1012
import org.codehaus.plexus.components.io.resources.PlexusIoResource;
1113

@@ -26,26 +28,45 @@ protected Iterator<PlexusIoResource> getEntries()
2628
{
2729
throw new IOException( "The tar archive file has not been set." );
2830
}
29-
final org.apache.commons.compress.archivers.zip.ZipFile zipFile = new org.apache.commons.compress.archivers.zip.ZipFile( f );
30-
final Enumeration en = zipFile.getEntries();
31-
return new Iterator<PlexusIoResource>()
31+
final ZipFile zipFile = new ZipFile( f );
32+
return new CloseableIterator( zipFile );
33+
}
34+
35+
class CloseableIterator
36+
implements Iterator<PlexusIoResource>, Closeable
37+
{
38+
final Enumeration en;
39+
40+
private final ZipFile zipFile;
41+
42+
public CloseableIterator( ZipFile zipFile )
43+
{
44+
this.en = zipFile.getEntriesInPhysicalOrder();
45+
this.zipFile = zipFile;
46+
}
47+
48+
public boolean hasNext ( ) {
49+
return en.hasMoreElements();
50+
}
51+
52+
public PlexusIoResource next()
3253
{
33-
public boolean hasNext()
34-
{
35-
return en.hasMoreElements();
36-
}
37-
38-
public PlexusIoResource next()
39-
{
40-
final ZipArchiveEntry entry = (ZipArchiveEntry) en.nextElement();
41-
42-
return new ZipResource( zipFile, entry, getStreamTransformer() );
43-
}
44-
45-
public void remove()
46-
{
47-
throw new UnsupportedOperationException( "Removing isn't implemented." );
48-
}
49-
};
54+
final ZipArchiveEntry entry = (ZipArchiveEntry) en.nextElement();
55+
56+
return new ZipResource( zipFile, entry, getStreamTransformer() );
57+
}
58+
59+
public void remove()
60+
{
61+
throw new UnsupportedOperationException( "Removing isn't implemented." );
62+
}
63+
64+
public void close()
65+
throws IOException
66+
{
67+
zipFile.close();
68+
}
5069
}
70+
71+
;
5172
}

0 commit comments

Comments
 (0)