java - 无法在 Ubuntu 中使用 Java 压缩空文件夹

标签 java scala ubuntu zip

该项目允许用户下载文件,下载的文件将是压缩文件。下载的压缩文件运行良好,只是空文件夹不会包含在压缩文件夹中。一位开发人员在他的 MacBook 上实现了以下代码,没有触发任何错误。但是,在ubuntu机器上执行源代码时,会出现错误。

代码:

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){ 用于压缩空文件夹,压缩的文件夹将排除空文件夹。

最佳答案

出现此问题的原因是您尝试在目录而不是文件上打开 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 默认压缩工具(存档管理)

关于java - 无法在 Ubuntu 中使用 Java 压缩空文件夹,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/46190169/

相关文章:

scala - TreeSet 映射后不遵守顺序

ubuntu - Ubuntu 上的 Xenomai : vxworks skin disabled?

java - Hibernate @UniqueConstraint 目的

运行Web服务客户端时出现java.lang.NoClassDefFoundError

java - 为什么 NetBeans Java 调试器永远无法到达此代码?

java - 当第一个实体是具有指定主键的实体之后的后续实体时,如何使用 JPA 获取列表

python - Hadoop Spark 1.4.1 - 对多个 CSV 文件进行排序并将排序后的结果保存在 1 个输出文件中

scala - Spark 数据集 : Example : Unable to generate an encoder issue

sorting - -g 选项打破保守的linux排序

php - setup-config.php 进入第 1 步然后返回 403 forbidden