Open fefc opened 5 years ago
I've looked at the decompressZip.java and found out that in the doUnZip function the code concerning directories doesn't work (line 55). I've changed the code to allow creation of directories so it doesn't bug anymore. Following code seems to work fine:
public boolean doUnZip(String actualTargetPath) throws IOException{
File target = new File(actualTargetPath);
if (!target.exists()) {
target.mkdirs();
}
ZipInputStream zipFl= new ZipInputStream(new FileInputStream(this.sourceEntry));
ZipEntry entry = zipFl.getNextEntry();
while (entry != null) {
String filePath = actualTargetPath + (actualTargetPath.endsWith("/") ? entry.getName() : File.separator + entry.getName());
if (entry.isDirectory()) {
//This part doesn't seem to be fired
File path = new File(filePath);
path.mkdir();
} else {
//That is why we check here if the target dir exists
File targetDir = new File(filePath.substring(0, filePath.lastIndexOf("/")));
if (!targetDir.exists()) {
targetDir.mkdirs();
}
extractFile(zipFl, filePath);
}
zipFl.closeEntry();
entry = zipFl.getNextEntry();
}
zipFl.close();
return true;
}
Hi @fefc,
I didn't fully review / test what you did changed, but please feel free to send a Pull Request, so your change could be fully included.
Thanks in advance.
Hi @fefc While in fact is better to use mkdir (I wanna merge that)
I still cant replicate what you mention about not being able to decompress to cache
, after apply the changes you made to lines 47 and 54 everything looks fine.
It will be ok for you if I merge and modify just to include lines 47 and 54 ?
Hi,
It's been a while since I had this issue so can't remember the test case. I'm using my patch at the moment and know it works, but I will try to use your code in on of my projects and try reproducing the bug (and check if line 47 and 54 are enough). Hopefully this week.
I'll keep you updated!
Hi @jjdltc I've been doing some tests.
It seems the issue is not related to cacheDirectory but to subfolders of zipped files.
I'm running an Ionic3 app that runs with cordova under Android. The phone is a Galaxy J6 with Android 9 (but pretty sure that doesn't matter much, I've done previous tests with multiple versions).
I am trying to extract a zip file called "General culture about australia.zip" which content is as follows:
folder1/database.json
folder1/subfolder1/file1.jpeg
folder1/subfolder1/file2.jpeg
folder1/subfolder2/otherfile1.jpeg
folder1/subfolder2/otherfile2.jpeg
....
where folder1 and subfolders are actually named with a long uuid like "0e36b9cc-a407-4350-85c0-91dca3ab25d3".
The target directory is cacheDirectoy/import which in my Android app looks like:
/data/user/0/eu.fefc.quizpad/cache/import
The actual sourceEntry is located in the cache and in my Android app looks like:
/data/user/0/eu.fefc.quizpad/cache/General culture about australia.zip
I've added a few log lines (in the non patched code, with line 47 and 54 updated): At the very begging of the doUnZip function:
Log.d("JJDLTC Test Log "," doUnZip with following targetPath " + actualTargetPath);
Log.d("JJDLTC Test Log "," doUnZip with following sourceEntry " + this.sourceEntry);
Just after while(entry != null)
Log.d("JJDLTC Test Log "," entry filePath is: " + filePath);
Log.d("JJDLTC Test Log "," entry is a directory: " + entry.isDirectory());
And I've try catched the extractFile() line.
try {
extractFile(zipFl, filePath);
} catch (IOException error) {
Log.d("JJDLTC Test Log "," error: ");
Log.d("JJDLTC Test Log ", error.getMessage());
}
Unziping the file I get:
doUnZip with following targetPath /data/user/0/eu.fefc.quizpad/cache/import
doUnZip with following sourceEntry /data/user/0/eu.fefc.quizpad/cache/General culture about australia.zip
entry filePath is: /data/user/0/eu.fefc.quizpad/cache/import/0e36b9cc-a407-4350-85c0-91dca3ab25d3/3b034c43-b828-4dc4-a359-263e8569078a/tmp_IMG-20190726-WA00002070623267859090281.jpg
entry is a directory: false
error:
/data/user/0/eu.fefc.quizpad/cache/import/0e36b9cc-a407-4350-85c0-91dca3ab25d3/3b034c43-b828-4dc4-a359-263e8569078a/tmp_IMG-20190726-WA00002070623267859090281.jpg (No such file or directory)
I suspect the following: None of my folders in the .zip file are empty, so there is no entry which contains "only" the folder. When trying to unzip a file located in a subfolder entry.isDirectory() return false which is expected, but when actually unzip that file, the subfolder in the target does not exist. This explains my old comment on line 56:
//This part doesn't seem to be fired
Hope you will be able to reproduce that behaviour on your side. Otherwise I'll try to do a demo project.
Hi @fefc,
Just merge #9, but still can't reproduce what you mention. I want to ask you: is there any chance that your *.zip
file contain any hidden files from the OS or the tool used for compress? for example, in newer versions of OSX, the native zip process includes a __MACOSX
folder (that is not visible for Mac users).
When I try to unzip to android cacheDirectory I get decompress Operation fail. If I try with dataDirectory it works fine.
JJzip.unzip(PathToFileInString, {target:cordova.file.cacheDirectory})