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
.
正在从web下载一个zip文件。它包含文件夹和文件。使用ZipInputstream和ZipEntry来解压它们。Zipentry。getName将文件名命名为htm/css/aaa.htm。
So I am creating new File(zipentry.getName);
我正在创建一个新文件(zipentryl . getname);
But problem it is throwing an exception: File not found
. I got that it is creating subfolders htm
and css
.
但问题是抛出一个异常:文件未找到。我得到它正在创建子文件夹htm和css。
My question is: how to create a file including its sub directories, by passing above path?
我的问题是:如何通过传递上面的路径来创建包含子目录的文件?
5 个解决方案
#1
92
Use this:
用这个:
File targetFile = new File("foo/bar/phleem.css");
File parent = targetFile.getParentFile();
if (!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).
虽然您可以只做file.getParentFile().mkdirs(),但不检查结果,它被认为是检查操作返回值的最佳实践。因此,首先检查现有目录,然后检查是否成功创建(如果还不存在的话)。
Reference:
参考:
- File.getParentFile()
- File.getParentFile()
- File.exists()
- File.exists()
- File.mkdir()
- File.mkdir()
- File.mkdirs()
- File.mkdirs()
#2
11
You can use Google's 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/
https://code.google.com/p/guava-libraries/
#3
2
You need to create subdirectories if necessary, as you loop through the entries in the zip file.
如果需要,您需要创建子目录,当您循环遍历zip文件中的条目时。
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
}
}
#4
1
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 }
#5
0
This is how I do it
我就是这么做的
static void ensureFoldersExist(File folder) {
if (!folder.exists()) {
if (!folder.mkdirs()) {
ensureFoldersExist(folder.getParentFile());
}
}
}
#1
92
Use this:
用这个:
File targetFile = new File("foo/bar/phleem.css");
File parent = targetFile.getParentFile();
if (!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).
虽然您可以只做file.getParentFile().mkdirs(),但不检查结果,它被认为是检查操作返回值的最佳实践。因此,首先检查现有目录,然后检查是否成功创建(如果还不存在的话)。
Reference:
参考:
- File.getParentFile()
- File.getParentFile()
- File.exists()
- File.exists()
- File.mkdir()
- File.mkdir()
- File.mkdirs()
- File.mkdirs()
#2
11
You can use Google's 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/
https://code.google.com/p/guava-libraries/
#3
2
You need to create subdirectories if necessary, as you loop through the entries in the zip file.
如果需要,您需要创建子目录,当您循环遍历zip文件中的条目时。
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
}
}
#4
1
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 }
#5
0
This is how I do it
我就是这么做的
static void ensureFoldersExist(File folder) {
if (!folder.exists()) {
if (!folder.mkdirs()) {
ensureFoldersExist(folder.getParentFile());
}
}
}