Skip to content

Commit 7e9a652

Browse files
committed
Make ZipArchive closeable as in Scala 2
1 parent 4cf79dc commit 7e9a652

File tree

1 file changed

+17
-1
lines changed

1 file changed

+17
-1
lines changed

compiler/src/dotty/tools/io/ZipArchive.scala

Lines changed: 17 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -108,6 +108,8 @@ abstract class ZipArchive(override val jpath: JPath) extends AbstractFile with E
108108
if (entry.isDirectory) ensureDir(dirs, entry.getName)
109109
else ensureDir(dirs, dirName(entry.getName))
110110
}
111+
112+
def close(): Unit
111113
}
112114
/** ''Note: This library is considered experimental and should not be used unless you know what you are doing.'' */
113115
final class FileZipArchive(jpath: JPath) extends ZipArchive(jpath) {
@@ -176,6 +178,7 @@ final class FileZipArchive(jpath: JPath) extends ZipArchive(jpath) {
176178
}
177179
} finally {
178180
if (ZipArchive.closeZipFile) zipFile.close()
181+
else closeables ::= zipFile
179182
}
180183
(root, dirs)
181184
}
@@ -194,15 +197,23 @@ final class FileZipArchive(jpath: JPath) extends ZipArchive(jpath) {
194197
case x: FileZipArchive => jpath.toAbsolutePath == x.jpath.toAbsolutePath
195198
case _ => false
196199
}
200+
201+
private[this] var closeables: List[java.io.Closeable] = Nil
202+
override def close(): Unit = {
203+
closeables.foreach(_.close)
204+
}
197205
}
198206

199207
final class ManifestResources(val url: URL) extends ZipArchive(null) {
200208
def iterator(): Iterator[AbstractFile] = {
201209
val root = new DirEntry("/", null)
202210
val dirs = mutable.HashMap[String, DirEntry]("/" -> root)
203-
val manifest = new Manifest(input)
211+
val stream = input
212+
val manifest = new Manifest(stream)
204213
val iter = manifest.getEntries().keySet().iterator().asScala.filter(_.endsWith(".class")).map(new ZipEntry(_))
205214

215+
closeables ::= stream
216+
206217
for (zipEntry <- iter) {
207218
val dir = getDir(dirs, zipEntry)
208219
if (!zipEntry.isDirectory) {
@@ -251,4 +262,9 @@ final class ManifestResources(val url: URL) extends ZipArchive(null) {
251262
}
252263
}
253264
}
265+
266+
private[this] var closeables: List[java.io.Closeable] = Nil
267+
override def close(): Unit = {
268+
closeables.foreach(_.close())
269+
}
254270
}

0 commit comments

Comments
 (0)