How to create a file -- including folders -- for a given path?

JavaAndroidFile

Java Problem Overview


Am downloading a zip file from web. It contain folders and files. Uncompressing them using ZipInputstream and ZipEntry. Zipentry.getName gives the name of file as htm/css/aaa.htm.

So I am creating new File(zipentry.getName);

But problem it is throwing an exception: File not found. I got that it is creating subfolders htm and css.

My question is: how to create a file including its sub directories, by passing above path?

Java Solutions


Solution 1 - Java

Use this:

File targetFile = new File("foo/bar/phleem.css");
File parent = targetFile.getParentFile();
if (parent != null && !parent.exists() && !parent.mkdirs()) {
    throw new IllegalStateException("Couldn't create dir: " + parent);
}

While you can just do file.getParentFile().mkdirs() without checking the result, it's considered a best practice to check for the return value of the operation. Hence the check for an existing directory first and then the check for successful creation (if it didn't exist yet).

Also, if the path doesn't include any parent directory, parent would be null. Check it for robustness.

Reference:

  • [File.getParentFile()][1]
  • [File.exists()][2]
  • [File.mkdir()][3]
  • [File.mkdirs()][4]

[1]: http://download.oracle.com/javase/6/docs/api/java/io/File.html#getParentFile%28%29 "Get a file's parent folder (even if neither file nor folder exist yet)" [2]: http://download.oracle.com/javase/6/docs/api/java/io/File.html#exists%28%29 "Check whether a File or folder exists" [3]: http://download.oracle.com/javase/6/docs/api/java/io/File.html#mkdir%28%29 "Create only this folder" [4]: http://download.oracle.com/javase/6/docs/api/java/io/File.html#mkdirs%28%29 "Create this folder and all required ancestors"

Solution 2 - Java

You can use Google's [tag:Guava] library to do it in a couple of lines with Files class:

Files.createParentDirs(file);
Files.touch(file);

https://code.google.com/p/guava-libraries/

Solution 3 - Java

You need to create subdirectories if necessary, as you loop through the entries in the zip file.

ZipFile zipFile = new ZipFile(myZipFile);
Enumeration e = zipFile.entries();
while(e.hasMoreElements()){
	ZipEntry entry = (ZipEntry)e.nextElement();
	File destinationFilePath = new File(entry.getName());
	destinationFilePath.getParentFile().mkdirs();
	if(!entry.isDirectory()){
	    //code to uncompress the file 
	}
}

Solution 4 - Java

Java NIO API Files.createDirectories

import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;

Path path = Paths.get("/folder1/folder2/folder3");
Files.createDirectories(path);

Solution 5 - Java

Looks at the file you use the .mkdirs() method on a File object: http://www.roseindia.net/java/beginners/java-create-directory.shtml

isDirectoryCreated = (new File("../path_for_Directory/Directory_Name")).mkdirs();
if (!isDirectoryCreated)
{
// Directory creation failed
}

Solution 6 - Java

This is how I do it

static void ensureFoldersExist(File folder) {
    if (!folder.exists()) {
        if (!folder.mkdirs()) {
            ensureFoldersExist(folder.getParentFile());
        }
    }
}

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionSrinivasView Question on Stackoverflow
Solution 1 - JavaSean Patrick FloydView Answer on Stackoverflow
Solution 2 - JavaAndrejsView Answer on Stackoverflow
Solution 3 - JavadogbaneView Answer on Stackoverflow
Solution 4 - JavaVadim ZverevView Answer on Stackoverflow
Solution 5 - Javaparag.raneView Answer on Stackoverflow
Solution 6 - JavaGubatronView Answer on Stackoverflow