【发布时间】:2019-12-10 13:11:24
【问题描述】:
是否可以从 zip 文件夹中解压单个文件并返回解压后的文件而不将数据存储在服务器上?
我有一个结构未知的 zip 文件,我想开发一项服务,该服务将按需提供给定文件的内容,而无需解压整个 zip 文件,也无需写入磁盘。 所以,如果我有这样的 zip 文件
zip_folder.zip
| folder1
| file1.txt
| file2.png
| folder 2
| file3.jpg
| file4.pdf
| ...
所以,我希望我的服务接收文件的名称和路径,以便我可以发送文件。
例如,fileName 可能是 folder1/file1.txt
def getFileContent(fileName: String): IBinaryContent = {
val content: IBinaryContent = getBinaryContent(...)
val zipInputStream: ZipInputStream = new ZipInputStream(content.getInputStream)
val outputStream: FileOutputStream = new FileOutputStream(fileName)
var zipEntry: ZipEntry = null
var founded: Boolean = false
while ({
zipEntry = zipInputStream.getNextEntry
Option(zipEntry).isDefined && !founded
}) {
if (zipEntry.getName.equals(fileName)) {
val buffer: Array[Byte] = Array.ofDim(9000) // FIXME how to get the dimension of the array
var length = 0
while ({
length = zipInputStream.read(buffer)
length != -1
}) {
outputStream.write(buffer, 0, length)
}
outputStream.close()
founded = true
}
}
zipInputStream.close()
outputStream /* how can I return the value? */
}
不把内容写入磁盘怎么办?
【问题讨论】:
标签: scala zip compression