创建zip文件时没有此类文件或目录异常

时间:2015-09-14 10:15:01

标签: java exception filenotfoundexception

我有一个工作的java应用程序,它创建了给定文件夹的zip文件。这在我的计算机上运行得很好(运行Ubuntu)代码是这样的:

public static void zipDir(String zipFileName, String dir) throws Exception {
        File dirObj = new File(dir);
        ZipOutputStream out = new ZipOutputStream(new FileOutputStream(zipFileName));
        addDir(dirObj, out,dir);
        out.close();
      }

public static void addDir(File dirObj, ZipOutputStream out,String startDir) throws IOException {
        File[] files = dirObj.listFiles();
        byte[] tmpBuf = new byte[1024];

        for (int i = 0; i < files.length; i++) {
          if (files[i].isDirectory()) {
            addDir(files[i], out,startDir);
            continue;
          }
          String filename=files[i].toString();
          filename=filename.substring(filename.indexOf(startDir)+startDir.length());

          FileInputStream in = new FileInputStream(files[i].getAbsolutePath());
          out.putNextEntry(new ZipEntry(filename));
          int len;
          while ((len = in.read(tmpBuf)) > 0) {
            out.write(tmpBuf, 0, len);
          }
          out.closeEntry();
          in.close();
        }
      }

当我在虚拟机(也运行ubuntu)上运行它时,我得到一个文件未找到异常。 目录在那里,我有写访问权限(我使用nano创建了一个txt文件)。 我已经读过一个SO问题,问题可能出在要删除的文件上,但是在异常时不会访问这些文件。这些是在执行抛出异常的行之后的addDir方法中读取的。

任何想法?

tracktrace

 at java.io.FileOutputStream.open0(Native Method)
        at java.io.FileOutputStream.open(FileOutputStream.java:270)
        at java.io.FileOutputStream.<init>(FileOutputStream.java:213)
        at java.io.FileOutputStream.<init>(FileOutputStream.java:101)
        at ckan.upload.GetUploadFiles.zipDir(GetUploadFiles.java:347)
        at ckan.upload.GetUploadFiles.main(GetUploadFiles.java:110)
        at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
        at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
        at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
        at java.lang.reflect.Method.invoke(Method.java:497)
        at org.eclipse.jdt.internal.jarinjarloader.JarRsrcLoader.main(JarRsrcLoader.java:58)

此:

File zipFile = new File(zipFileName); 
System.out.println(zipFile.exists()); 
System.out.println(zipFile.isFile()); 
System.out.println(zipFile.canRead()); 
System.out.println(zipFile.canWrite());

为所有4个

返回FALSE

0 个答案:

没有答案