Permalink
Browse files

[SHRINKWRAP-511] ArchiveFactory fails to close ZipFile and retains a …

…Windows file lock
  • Loading branch information...
1 parent c10d668 commit eccccd10005dbc7e63d780e5f40b437963b92d64 @AndyGee AndyGee committed with ALRubinger Sep 22, 2016
@@ -183,20 +183,15 @@
+ archiveFile.getAbsolutePath());
}
- // Construct ZipFile
- final ZipFile zipFile;
try {
- zipFile = new ZipFile(archiveFile);
+ // Import
+ return ShrinkWrap.create(type, archiveFile.getName()).as(ZipImporter.class).importFrom(new ZipFile(archiveFile)).as(type);
} catch (final ZipException ze) {
throw new IllegalArgumentException("Does not appear to be a valid ZIP file: "
+ archiveFile.getAbsolutePath());
} catch (final IOException ioe) {
throw new RuntimeException("I/O Error in importing new archive from ZIP: " + archiveFile.getAbsolutePath(),
ioe);
}
-
- // Import
- return ShrinkWrap.create(type, archiveFile.getName()).as(ZipImporter.class).importFrom(zipFile).as(type);
-
}
}
@@ -16,6 +16,8 @@
*/
package org.jboss.shrinkwrap.impl.base.asset;
+import java.io.File;
+import java.io.IOException;
import java.io.InputStream;
import java.util.zip.ZipEntry;
import java.util.zip.ZipFile;
@@ -31,10 +33,10 @@
* @author <a href="mailto:aslak@conduct.no">Aslak Knutsen</a>
*/
public class ZipFileEntryAsset implements Asset {
- private final ZipFile file;
+ private final File file;
private final ZipEntry entry;
- public ZipFileEntryAsset(ZipFile file, ZipEntry entry) {
+ public ZipFileEntryAsset(final File file, final ZipEntry entry) {
Validate.notNull(file, "File must be specified");
Validate.notNull(entry, "Entry must be specified");
@@ -51,9 +53,39 @@ public ZipFileEntryAsset(ZipFile file, ZipEntry entry) {
// TODO: create AssetStreamException ?
public InputStream openStream() {
try {
- return file.getInputStream(entry);
- } catch (Exception e) {
+ final ZipFile file = new ZipFile(this.file);
+ return new InputStreamWrapper(file, file.getInputStream(entry));
+ } catch (final Exception e) {
throw new RuntimeException("Could not open zip file stream", e);
}
}
+
+ private static class InputStreamWrapper extends InputStream {
+
+ private final ZipFile file;
+ private final InputStream is;
+
+ private InputStreamWrapper(final ZipFile file, final InputStream is) {
+ this.file = file;
+ this.is = is;
+ }
+
+ @Override
+ public int read() throws IOException {
+ return this.is.read();
+ }
+
+ @Override
+ public void close() throws IOException {
+ try {
+ try {
+ this.is.close();
+ } finally {
+ this.file.close();
+ }
+ } finally {
+ super.close();
+ }
+ }
+ }
}
@@ -214,10 +214,16 @@ private ZipImporter importFrom(final ZipFile file, Filter<ArchivePath> filter) t
continue;
}
- archive.add(new ZipFileEntryAsset(file, entry), new BasicPath(entryName));
+ archive.add(new ZipFileEntryAsset(new File(file.getName()), entry), new BasicPath(entryName));
}
} catch (Exception e) {
throw new ArchiveImportException("Could not import file", e);
+ } finally {
+ try {
+ file.close();
+ } catch (IOException e) {
+ //no-op
+ }
}
return this;
}

0 comments on commit eccccd1

Please sign in to comment.