Reputation: 101
I'm trying to decompress RAR archive on my Android device from the sd card but I got an error:
java.io.FileNotFoundException: /mnt/sdcard: open failed: EISDIR (Is a directory)
I chose a rar-file and try to decompress it in my sd-card. Error says that it's not directory but it is. I have no idea how I can fix it.
My code:
public static void unrar(File srcRarFile, String destPath, String password) throws IOException {
if (null == srcRarFile || !srcRarFile.exists()) {
throw new IOException(".");
}
if (!destPath.endsWith(SEPARATOR)) {
destPath += SEPARATOR;
}
Archive archive = null;
OutputStream unOut = null;
try {
archive = new Archive(srcRarFile, password, false);
FileHeader fileHeader = archive.nextFileHeader();
while(null != fileHeader) {
if (!fileHeader.isDirectory())
{
// 1 destDirName destFileName
String destFileName = "";
String destDirName = "";
destFileName = (destPath + fileHeader.getFileNameW()).replaceAll("/", "\\\\");
destDirName = destFileName.substring(0, destFileName.lastIndexOf("\\"));
// 2
File dir = new File(destDirName);
if (!dir.exists() || !dir.isDirectory()) {
dir.mkdirs();
}
//
// ERROR:
unOut = new FileOutputStream(dir);
archive.extractFile(fileHeader, unOut);
unOut.flush();
unOut.close();
}
fileHeader = archive.nextFileHeader();
}
archive.close();
} catch (RarException e) {
e.printStackTrace();
} finally {
IOUtils.closeQuietly(unOut);
}
}
Upvotes: 1
Views: 10761
Reputation: 5372
Clear thing. You first create the directory /mnt/sdcard
(variable dir
is equal to destDirName
in your code) and then you try to create a file with the same same. This will not work. Use a name like /mnt/sdcard/abc.rar
and it might work. Here is how you create the corresponding File object:
File file = new File(dir, "abc.rar");
Btw: creating a directory called /mnt/sdcard
will probably not work due to a lack of permissions. If it's an SD card, Android will do this job for you anyway. If it isn't an SD card, its not a good idea to create a directory with this name.
PS2: After further reviewing your code I see things which are not good style:
indexOf
/substring
to find the parent directory.File
and string
You can simply get rid of all this by using File.getParent()
/File.getParentFile()
Upvotes: 5
Reputation: 3759
Check if you have set permissions in your manifest file.
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
Upvotes: 0