无法在Ubuntu中使用Java压缩空文件夹
该项目允许用户下载一个文件,下载的文件将是压缩文件。下载的压缩文件运行良好,只是压缩文件夹中不会包含空文件夹。其中一位开发人员使用MacBook实现了下面的代码,这不会触发任何错误。然而,当源代码在ubuntu机器中执行时,就会发生错误 守则:无法在Ubuntu中使用Java压缩空文件夹,java,scala,ubuntu,zip,Java,Scala,Ubuntu,Zip,该项目允许用户下载一个文件,下载的文件将是压缩文件。下载的压缩文件运行良好,只是压缩文件夹中不会包含空文件夹。其中一位开发人员使用MacBook实现了下面的代码,这不会触发任何错误。然而,当源代码在ubuntu机器中执行时,就会发生错误 守则: def zipFolder(folderNamePath: String, subDirectory: String,zip: ZipOutputStream): Unit = { val file = new File(folderNameP
def zipFolder(folderNamePath: String, subDirectory: String,zip: ZipOutputStream): Unit = {
val file = new File(folderNamePath)
val readBuffer = new Array[Byte](Buffer)
val fileList = file.list()
var i = 0
for( i <- 0 until fileList.length ){
val path = folderNamePath + "/" + fileList(i)
val currFile = new File(path)
if(currFile.isDirectory){
val filePath = currFile.getPath
zipFolder(filePath, subDirectory + '/' + fileList(i) ,zip)
}else{
val anEntry = new ZipEntry(subDirectory + '/' + fileList(i))
val bis = new BufferedInputStream(new FileInputStream(path), Buffer)
zip.putNextEntry(anEntry)
var bytesIn: Int = -1
while({
bytesIn = bis.read(readBuffer, 0, Buffer)
bytesIn != -1
}){
zip.write(readBuffer, 0, bytesIn);
}
bis.close()
}
}
//THE CODE BELOW IS TO ZIP EMPTY DIRECTORY
if(fileList.length == 0){
val path = folderNamePath
val anEntry = new ZipEntry(subDirectory)
val bis = new BufferedInputStream(new FileInputStream(path + "/"), Buffer)
zip.putNextEntry(anEntry)
bis.close()
}
}
并记录错误:
! @75akh0a6a - Internal server error, for (POST)
[/storage_ws/download_directory_file] ->
play.api.http.HttpErrorHandlerExceptions$$anon$1: Execution
exception[[FileNotFoundException: /home/jarvis/project/storage-api/media/jarvis/test/Test/Testing (Is a directory)]]
at play.api.http.HttpErrorHandlerExceptions$.throwableToUsefulException(HttpErrorHandler.scala:280)
at play.api.http.DefaultHttpErrorHandler.onServerError(HttpErrorHandler.scala:206)
at play.api.GlobalSettings$class.onError(GlobalSettings.scala:160)
at play.api.DefaultGlobal$.onError(GlobalSettings.scala:188)
at play.api.http.GlobalSettingsHttpErrorHandler.onServerError(HttpErrorHandler.scala:98)
at play.filters.cors.AbstractCORSPolicy$$anonfun$2.applyOrElse(AbstractCORSPolicy.scala:146)
at play.filters.cors.AbstractCORSPolicy$$anonfun$2.applyOrElse(AbstractCORSPolicy.scala:145)
at scala.concurrent.Future$$anonfun$recoverWith$1.apply(Future.scala:346)
at scala.concurrent.Future$$anonfun$recoverWith$1.apply(Future.scala:345)
at scala.concurrent.impl.CallbackRunnable.run(Promise.scala:32)
Caused by: java.io.FileNotFoundException: /home/jarvis/project/storage-api/media/user/jarvis/Test/Testing (Is a directory)
at java.io.FileInputStream.open0(Native Method)
at java.io.FileInputStream.open(FileInputStream.java:195)
at java.io.FileInputStream.<init>(FileInputStream.java:138)
at java.io.FileInputStream.<init>(FileInputStream.java:93)
at tools.ZipUtils$.zipFolder(ZipUtils.scala:122)
at tools.ZipUtils$$anonfun$zipFolder$1.apply$mcVI$sp(ZipUtils.scala:101)
at scala.collection.immutable.Range.foreach$mVc$sp(Range.scala:160)
at tools.ZipUtils$.zipFolder(ZipUtils.scala:96)
at tools.IO$.handleDownloadDirectoryFile(IO.scala:176)
at controllers.FileHandleController$$anonfun$downloadDirectoryFile$1$$anonfun$apply$50.apply(FileHandleController.scala:586)
代码执行得很好,没有以下情况:iffileList.length==0{压缩空文件夹,压缩后的文件夹将排除空文件夹。出现问题的原因是您试图在目录而不是文件上打开FileInputStream 实际上,您可以在此处看到异常消息:
Caused by: java.io.FileNotFoundException: /home/jarvis/project/storage-api/media/user/jarvis/Test/Testing (Is a directory)
at java.io.FileInputStream.open0(Native Method)
这是一个稍微修改过的方法版本,应该可以解决这个问题。请注意,我用File.separator替换了所有出现的“/”项
def zipFolder(folderNamePath: String, subDirectory: String,zip: ZipOutputStream): Unit = {
val file = new File(folderNamePath)
val readBuffer = new Array[Byte](Buffer)
val fileList = file.list()
var i = 0
for( i <- 0 until fileList.length ){
val currFile = new File(folderNamePath, fileList(i))
if(currFile.isDirectory){
val filePath = currFile.getPath
zipFolder(filePath, subDirectory + File.separator + fileList(i) ,zip)
}else{
val path = folderNamePath + File.separator + fileList(i)
val anEntry = new ZipEntry(subDirectory + File.separator + fileList(i))
val bis = new BufferedInputStream(new FileInputStream(path), Buffer)
zip.putNextEntry(anEntry)
var bytesIn: Int = -1
while({
bytesIn = bis.read(readBuffer, 0, Buffer)
bytesIn != -1
}){
zip.write(readBuffer, 0, bytesIn);
}
bis.close()
}
}
//THE CODE BELOW IS TO ZIP EMPTY DIRECTORY
if(fileList.length == 0){
zip.putNextEntry(new ZipEntry(subDirectory + File.separator))
zip.closeEntry()
}
}
空文件夹只有在解压缩文件时才可见。至少,对于Ubuntu默认的zip toolArchive Manage来说,尝试使用Apache Commons IO中的FilenameUtils类或java.nio中的Paths类。有很多有用的方法。在跨平台开发中遇到困难时总是能帮到我。很幸运上面的代码ks!但是有一个缺陷,空文件夹变成了未知文件。你知道吗?我在zip.putnextryNew ZipEntrysubDirectory+/中添加了/。空文件夹又消失了。听说:压缩后如何解决空文件夹变成未知文件的问题?在结尾添加/没有帮助=尝试在zip.putNext之后使用:zip.closeEntry进入呼叫。请参阅上面的编辑。