2017-09-13 71 views
0

該項目允許用戶下載下載的文件將成爲壓縮文件的文件。下載的壓縮文件運行良好,但空文件夾不會包含在壓縮文件夾中。其中一位開發人員用他的MacBook實現了下面的代碼,不會觸發任何錯誤。但是,當源代碼在ubuntu機器上執行時,會發生錯誤。無法在Ubuntu中使用Java壓縮空文件夾

代碼:

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) 

沒有下面的情況if(fileList.length == 0){爲荏苒空文件夾,它的壓縮文件夾將排除空文件夾執行良好的代碼。

+0

嘗試使用Apache Commons IO中的FilenameUtils類(https://commons.apache.org/proper/commons-io/javadocs/api-2.5/org /apache/commons/io/FilenameUtils.html)或java.nio的Paths類(https://docs.oracle.com/javase/7/docs/api/java/nio/file/Paths.html)。 有很多有用的方法。在跨平臺開發遇到麻煩時總是幫助我。祝你好運 – Bohdan

回答

1

問題發生是因爲您試圖在目錄而不是文件上打開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默認壓縮工具(歸檔管理)

+0

上面的代碼工作!但是,有一個缺陷,空文件夾成爲未知文件。任何想法? –

+0

我在zip.putNextEntry中添加了「/」(新的ZipEntry(subDirectory +「/」))。空文件夾再次消失 –

+0

很高興聽到:) – P3trur0