0

new_file_name is something like 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());
        }

I get this exception Exception writing to 2013-03-15-09:28:20_65.zip: java.io.FileNotFoundException: (No such file or directory)

The directory has full permissions. I see a file that gets created in the folder too, I also tried giving the getAbsolutePath(), still gave me the same exception.

roymustang86
  • 8,054
  • 22
  • 70
  • 101
  • 1
    Does your file system allow `:` to be part of the name of a file? – Bruno Reis Mar 15 '13 at 13:43
  • 1
    Rather than just logging the exception message, log the entire stack trace. And update your question with that trace. Your current message doesn't give much actual information. Worse, it prints out your desired filename on any error, which is just confusing you. – parsifal Mar 15 '13 at 14:11
  • Once you do this, I'm willing to bet that you'll find it's the `new FileInputStream()` that's throwing the exception. – parsifal Mar 15 '13 at 14:14
  • You are right, I am kinda stupid. The whole stacktrace pointed to me that there is an empty string in the fileList – roymustang86 Mar 15 '13 at 14:37
  • @roymustang86 - I don't know if you can delete this question now that it has an upvoted answer, but if you can please do (or flag for moderator attention). It's unlikely to help anyone else. – parsifal Mar 15 '13 at 15:03

2 Answers2

1

I've written some utility methods to copy directories to/ from a Zip file using the NIO.2 File API (the library is Open Source):

Maven:

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

Tutorial:

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

API: CopyFileVisitor.copy

Maybe you find it useful.

Puce
  • 37,247
  • 13
  • 80
  • 152
0

I think you're not allowed to use ':' in file name, if you use "2013-03-15-08_59_10_65.zip" it should be fine.

Ok... I somehow manage to find the same error when a file in fileList does not exist!

cwhsu
  • 1,493
  • 24
  • 31
  • Sorry, but that didn't work. I still get `Exception writing to 2013-03-15-09_45_13_65.zip: java.io.FileNotFoundException: (No such file or directory)` – roymustang86 Mar 15 '13 at 13:56