[syntax=java5] public static boolean decompress(File zipFile, File targetDirectory) {
boolean result = true;
ZipInputStream zis = null;
try {
zis = new ZipInputStream(
new BufferedInputStream(new FileInputStream(zipFile)));
} catch (FileNotFoundException e) {
e.printStackTrace();
return false;
}
try {
ZipEntry ze;
int count;
byte[] buffer = new byte[8192];
while ((ze = zis.getNextEntry()) != null) {
Log.d("Zip", "Decompressing: " + ze.getName());
File file = new File(targetDirectory, ze.getName());
File dir = ze.isDirectory() ? file : file.getParentFile();
if (!dir.isDirectory() && !dir.mkdirs())
throw new FileNotFoundException("Failed to ensure directory: " +
dir.getAbsolutePath());
if (ze.isDirectory())
continue;
FileOutputStream fout = new FileOutputStream(file);
try {
long totalBytes = 0;
while ((count = zis.read(buffer)) != -1) {
totalBytes += count;
fout.write(buffer, 0, count);
}
zis.closeEntry();
Log.d("Zip", "Total bytes: " + totalBytes);
Log.d("Zip", "Completed: " + ze.getName());
if (!file.exists())
Log.d("Zip", "Ops!!! File not exists!!!");
} catch (Exception e) {
e.printStackTrace();
result = false;
} finally {
fout.close();
}
}
} catch (Exception e) {
e.printStackTrace();
result = false;
} finally {
try {
zis.close();
} catch (IOException e) {
e.printStackTrace();
}
}
return result;
}[/syntax]
Дело в том, что он извлекает не все файлы. Если смотреть по логам, иногда я вижу следующее:
Получается, файл "common.db" находится в архиве, его распаковка прошла успешно, было с читано 212992 байт. Но сам файл при этом не существует! Как такое вообще может быть? Ошибка происходит не всегда. Иногда все распаковывается. Ошибка в основном при распаковке "common.db".Decompressing: backup_info.xml
Total bytes: 247
Completed: backup_info.xml
Decompressing: common.db
Total bytes: 212992
Completed: common.db
Ops!!! File not exists!!!
Decompressing: user_def_user.db
Total bytes: 102400
Completed: user_def_user.db
Не пойму в чем дело. Неужто процедура с ошибкой?