java - ZipOutputStream 提示 FileNotFoundException

标签 java zip solaris zipoutputstream

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(),仍然给了我同样的异常。

最佳答案

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

Maven:

<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

也许您发现它很有用。

关于java - ZipOutputStream 提示 FileNotFoundException,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15433929/

相关文章:

Solaris 的 Java 8 中缺少 javaws

java - 连接到 MS Access 的 Eclipse 的深不可测的编码行为

java - 我可以将 Arrays.fill 与二维数组一起使用吗?如果是这样,我该怎么做?

Java:sl4j 如何记录所有类?

支持 Deflate64 的 Java 压缩库

shell - 解析 zpool 中的磁盘

perl - Perl 脚本中的 Bash 命令

java - 当一个对象的使用只在一个地方出现同步块(synchronized block)时会发生什么?

java - 按路径将 zip 文件夹中的文件放入字符串数组中

firefox-addon - zip 糊涂了。插件似乎已损坏