所以,我在 scala 中有这段代码,我将其转换为托管代码。
val file_out = new FileOutputStream(new java.io.File(filePath, resultFile + ".tar.gz"));
val buffer_out = new BufferedOutputStream(file_out);
val gzip_out = new GzipCompressorOutputStream(buffer_out);
val tar_out = new TarArchiveOutputStream(gzip_out);
try {
addFileToTarGz(tar_out, filePath + "/" + resultFolder, "");
} finally {
tar_out.finish();
tar_out.close();
gzip_out.close();
buffer_out.close();
file_out.close();
}
第一次尝试是:
val file = new java.io.File(filePath, s"$resultFile.tar.gz")
managed(new FileOutputStream(file))
.acquireAndGet(stream => managed(new BufferedOutputStream(stream))
.acquireAndGet(buffer => managed(new GzipCompressorOutputStream(buffer))
.acquireAndGet(gzip => managed(new TarArchiveOutputStream(gzip))
.acquireAndGet(tar => {
try {
addFileToTarGz(tar, filePath + "/" + resultFolder, "")
} finally {
tar.finish()
}
}))))
但是,它看起来不太可读。有没有更好的方法让它变得managed
可读?