【问题标题】:ZipOutputStream complains of FileNotFoundException [closed]ZipOutputStream 抱怨 FileNotFoundException [关闭]
【发布时间】:2013-03-15 13:39:46
【问题描述】:

new_file_name 类似于 2013-03-15-08:59:10_65.zip

 fileZip = new ZipOutputStream(new FileOutputStream(new File(new_file_name)));
    byte[] buffer = new byte[1024];
    try{
        for(String fileName:fileList)
        {
            FileInputStream in = null;
            try{
                File file = new File(fileName);
                ZipEntry ze = new ZipEntry(fileName);
                fileZip.putNextEntry(ze);
                in = new FileInputStream(file);
                int len = 0;
                while((len = in.read(buffer)) > 0) {
                    fileZip.write(buffer, 0, len);
                }
                fileZip.closeEntry();
                in.close();
            } catch (Exception e) {
                log(0, "Exception writing "+fileName+" to "+new_file_name+": "+e.toString());
        }

我收到此异常Exception writing to 2013-03-15-09:28:20_65.zip: java.io.FileNotFoundException: (No such file or directory)

该目录具有完全权限。我也看到在文件夹中创建了一个文件,我也尝试给出 getAbsolutePath(),仍然给了我同样的异常。

【问题讨论】:

  • 您的文件系统是否允许: 成为文件名的一部分?
  • 记录整个堆栈跟踪,而不是只记录异常消息。并使用该跟踪更新您的问题。您当前的消息没有提供太多实际信息。更糟糕的是,它会在出现任何错误时打印出您想要的文件名,这只会让您感到困惑。
  • 一旦你这样做了,我敢打赌你会发现是new FileInputStream()引发了异常。
  • 你说得对,我有点傻。整个堆栈跟踪指向我 fileList 中有一个空字符串
  • @roymustang86 - 我不知道你是否可以删除这个问题,因为它有一个赞成的答案,但如果你可以请这样做(或标记版主注意)。它不太可能帮助其他任何人。

标签: java zip solaris zipoutputstream


【解决方案1】:

我已经编写了一些实用方法来使用 NIO.2 文件 API(该库是开源的)将目录复制到/从 Zip 文件中:

马文:

<dependency>  
    <groupId>org.softsmithy.lib</groupId>  
    <artifactId>softsmithy-lib-core</artifactId>  
    <version>0.3</version>  
</dependency>  

教程:

http://softsmithy.sourceforge.net/lib/current/docs/tutorial/nio-file/index.html#AddZipResourceSample

API:CopyFileVisitor.copy

也许你觉得它有用。

【讨论】:

    【解决方案2】:

    我认为你不允许在文件名中使用':',如果你使用“2013-03-15-08_59_10_65.zip”应该没问题。

    好的...当 fileList 中的文件不存在时,我设法找到了同样的错误!

    【讨论】:

    • 抱歉,这不起作用。我仍然收到Exception writing to 2013-03-15-09_45_13_65.zip: java.io.FileNotFoundException: (No such file or directory)
    猜你喜欢
    • 2020-06-13
    • 1970-01-01
    • 2012-03-19
    • 1970-01-01
    • 2019-06-16
    • 2022-01-10
    • 2012-01-28
    • 2011-11-14
    • 2014-06-27
    相关资源
    最近更新 更多