Le projet permet à l'utilisateur de télécharger un fichier dont le fichier téléchargé sera le fichier compressé. Le fichier compressé téléchargé fonctionne bien, sauf que le dossier vide ne sera pas inclus dans le dossier compressé. Un des développeurs implémenté avec le code ci-dessous avec son MacBook qui ne déclenche aucune erreur. Cependant, lorsque le code source est exécuté dans la machine ubuntu, une erreur survient.Impossible de compresser le dossier vide avec Java dans Ubuntu
Le code:
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()
}
}
Et l'erreur connecté:
! @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)
Le code exécuté bien sans le cas suivant if(fileList.length == 0){
pour passer comme un éclair dossier vide, que le dossier compressé exclura le dossier vide.
Essayez d'utiliser la classe FilenameUtils d'Apache Commons IO (https://commons.apache.org/proper/commons-io/javadocs/api-2.5/org /apache/commons/io/FilenameUtils.html) ou la classe Paths de java.nio (https://docs.oracle.com/javase/7/docs/api/java/nio/file/Paths.html). Il existe un tas de méthodes utiles. Toujours m'a aidé en cas de problème avec le développement multiplateforme. Bonne chance – Bohdan