1

I have a problem while unzipping a file, I get "open failed: ENOTDIR (Not a directory)" error.. After debugging I found out that the problem is in this function:
After I determine that the current ZipEntry is a Directory, I call this function with its path:

private void dirChecker(String dir) {
        File FiledirChecker = new File(PATH + dir); // PATH+dir = /mnt/sdcard/Pictures/Hafs

        if (!FiledirChecker.isDirectory())
            FiledirChecker.mkdirs();
    }

After this function is over, I browsed to Pictures directory, and Hafs isnt a Directory, instead its a 0 bytes file that is called "Hafs".

Why is this happening?

I should point out that for other directories (other than "Hafs"), mkdirs normally creates the folder! but only for this directory (Hafs) mkdirs doesnt make a directory but a file!

Dungeon Hunter
  • 19,827
  • 13
  • 59
  • 82
Omar
  • 7,835
  • 14
  • 62
  • 108
  • Have you checked [this post](http://stackoverflow.com/questions/4543559/file-mkdirs-method-not-working-in-android-java)? – assylias Aug 15 '12 at 11:45
  • @assylias Yes, I already have that permission. – Omar Aug 15 '12 at 11:47
  • Run under a debugger, or add print statements, and find out whether your belief that File("Hafs").mkdirs() is actually being called (it may not be.) Likewise, add checks to the file-unzipping part of the code to make sure it's not accidentally being unzipped as a file (it probably is). – Ernest Friedman-Hill Aug 15 '12 at 11:52
  • @ErnestFriedman-Hill already did that, and indeed File("/mnt/sdcard/Pictures/Hafs").mkdirs() gets called.. I dont know why only this directory is making the problem and not the 4 other directories! – Omar Aug 15 '12 at 11:58
  • 1
    But did you check to make sure a zero-size file isn't being created first? mkdirs() will fail silently -- it just returns boolean on failure -- and you're not checking the return value. – Ernest Friedman-Hill Aug 15 '12 at 12:01
  • @ErnestFriedman-Hill That was the cause. I fixed a small bug in another place and now its working.. You were the first to comment with the correct answer, post an answer if u want that I accept ur answer. – Omar Aug 15 '12 at 12:26
  • I think it should be if (FiledirChecker.isDirectory()) FiledirChecker.mkdirs(); – andy Aug 15 '12 at 12:49

2 Answers2

7

This is happening because elsewhere in the code, an empty file has already been created.

Or the file is a leftover from your last run when your code couldn't create directories.

Aaron Digulla
  • 321,842
  • 108
  • 597
  • 820
0

Unless I miss something your file is already created somewhere else.
According to what I can see in your code
1) you check if the file is NOT a directory
2) if 1) is true you attempt to create complete directory structure and a directory with the same name as a file that may already exist.

Germann Arlington
  • 3,315
  • 2
  • 17
  • 19
  • This is a standard idiom. a `File` object represents a path, which possibly does not exist. If it's not a directory, then create it as such. It is true that if the file exists, but is not a directory, then this will try (and fail) to create the directory. – Ernest Friedman-Hill Aug 15 '12 at 12:03
  • But, the correct approach would be to check `File.exists()`, NOT `File.isDirectory()` – Germann Arlington Aug 15 '12 at 12:17