fixed unzip issue

This commit is contained in:
vishnuraghavb 2021-04-18 18:58:07 +05:30
parent b08267a085
commit f53ed08540
2 changed files with 56 additions and 53 deletions

View file

@ -198,13 +198,17 @@ export default {
("0" + date.getMinutes()).slice(-2) + ("0" + date.getMinutes()).slice(-2) +
("0" + date.getSeconds()).slice(-2); ("0" + date.getSeconds()).slice(-2);
let filename = `EnRecipes_${formattedDate}.zip`; let filename = `${localize("EnRecipes")}_${formattedDate}.zip`;
let fromPath = path.join(knownFolders.documents().path, "EnRecipes"); let fromPath = path.join(knownFolders.documents().path, "EnRecipes");
utils.Zip.zip(fromPath, this.backupFolder, filename) utils.Zip.zip(fromPath, this.backupFolder, filename)
.then((res) => { .then((res) => {
if (res) this.showExportSummary(filename); if (res) this.showExportSummary(filename);
}) })
.catch((err) => console.log("Backup error: ", err)); .catch((err) => {
console.log("Backup error: ", err);
this.progress = null;
this.setBackupFolder(true);
});
}, },
exportFiles(option) { exportFiles(option) {
const folder = path.join(knownFolders.documents().path, "EnRecipes"); const folder = path.join(knownFolders.documents().path, "EnRecipes");
@ -258,10 +262,11 @@ export default {
}, },
showExportSummary(filename) { showExportSummary(filename) {
this.progress = null; this.progress = null;
let description = localize("buto", filename);
this.$showModal(ConfirmDialog, { this.$showModal(ConfirmDialog, {
props: { props: {
title: "expSuc", title: "expSuc",
description: `Backed up to ${filename}`, description,
okButtonText: "OK", okButtonText: "OK",
}, },
}); });
@ -270,9 +275,8 @@ export default {
// IMPORT HANDLERS // IMPORT HANDLERS
openZipFile() { openZipFile() {
utils.getBackupFile().then((uri) => { utils.getBackupFile().then((uri) => {
console.log(uri);
if (uri) { if (uri) {
let dest = path.join(knownFolders.temp().path, "tempUnZip"); let dest = knownFolders.temp().path;
utils.Zip.unzip(uri, dest) utils.Zip.unzip(uri, dest)
.then((res) => { .then((res) => {
if (res) this.validateZipContent(res, uri); if (res) this.validateZipContent(res, uri);
@ -325,14 +329,15 @@ export default {
}, },
]); ]);
} else { } else {
Folder.fromPath(extractedFolderPath).remove(); knownFolders.temp().clear();
this.progress = null; this.progress = null;
this.failedImport(localize("buInc")); this.failedImport(localize("buInc"));
} }
if (Folder.exists(ImagesFolderPath)) { if (Folder.exists(ImagesFolderPath)) {
const timer = setInterval(() => { const timer = setInterval(() => {
if (this.importSummary.found) { if (this.importSummary.found) {
this.importImages(uri, extractedFolderPath); knownFolders.temp().clear();
this.importImages(uri);
clearInterval(timer); clearInterval(timer);
} }
}, 100); }, 100);
@ -419,7 +424,7 @@ export default {
break; break;
} }
}, },
importImages(uri, extractedFolderPath) { importImages(uri) {
let destPath = knownFolders.documents().path; let destPath = knownFolders.documents().path;
Folder.fromPath(destPath); Folder.fromPath(destPath);
utils.Zip.unzip(uri, destPath).then((res) => { utils.Zip.unzip(uri, destPath).then((res) => {
@ -427,7 +432,6 @@ export default {
this.showImportSummary(); this.showImportSummary();
this.unlinkBrokenImages(); this.unlinkBrokenImages();
this.exportFiles("delete"); this.exportFiles("delete");
Folder.fromPath(extractedFolderPath).remove();
} }
}); });
}, },

View file

@ -173,12 +173,11 @@ export class Zip {
Application.android.context, Application.android.context,
parsedUri parsedUri
) )
.createFile('application/zip', filename) return new Promise((resolve, reject) => {
.getUri() if (uri.exists()) {
const outputStream = ContentResolver.openOutputStream(uri) let destFile = uri.createFile('application/zip', filename).getUri()
const outputStream = ContentResolver.openOutputStream(destFile)
const zipOutputStream = new java.util.zip.ZipOutputStream(outputStream) const zipOutputStream = new java.util.zip.ZipOutputStream(outputStream)
return new Promise((resolve) => {
const sourceFiles = Zip.getSubFiles(src, true) const sourceFiles = Zip.getSubFiles(src, true)
for (let i = 0; i < sourceFiles.size(); i++) { for (let i = 0; i < sourceFiles.size(); i++) {
let len let len
@ -202,7 +201,10 @@ export class Zip {
zipOutputStream.closeEntry() zipOutputStream.closeEntry()
} }
zipOutputStream.close() zipOutputStream.close()
resolve(uri) resolve(destFile)
} else {
reject('Destination file cannot be created: Path does not exist')
}
}) })
} }
static unzip(uri, dest) { static unzip(uri, dest) {
@ -210,26 +212,23 @@ export class Zip {
return new Promise((resolve) => { return new Promise((resolve) => {
const inputStream = ContentResolver.openInputStream(uri) const inputStream = ContentResolver.openInputStream(uri)
const zipInputStream = new java.util.zip.ZipInputStream(inputStream) const zipInputStream = new java.util.zip.ZipInputStream(inputStream)
let len, filepath let len, filepath, entry
let buffer = Array.create('byte', 4096) while ((entry = zipInputStream.getNextEntry()) != null) {
let entry = zipInputStream.getNextEntry()
filepath = dest + '/' + entry.getName() filepath = dest + '/' + entry.getName()
while (entry != null) { let output = new java.io.File(filepath)
filepath = dest + '/' + entry.getName() if (entry.isDirectory()) {
if (!entry.isDirectory()) { output.mkdirs()
const output = new java.io.BufferedOutputStream( continue
new java.io.FileOutputStream(filepath) }
if (!output.getParentFile().exists()) output.getParentFile().mkdirs()
const outputStream = new java.io.BufferedOutputStream(
new java.io.FileOutputStream(output)
) )
while ((len = zipInputStream.read(buffer)) != -1) { let buffer = Array.create('byte', 4096)
output.write(buffer, 0, len) while ((len = zipInputStream.read(buffer)) != -1)
} outputStream.write(buffer, 0, len)
output.close() outputStream.close()
} else {
let dir = new java.io.File(filepath)
dir.mkdirs()
}
zipInputStream.closeEntry() zipInputStream.closeEntry()
entry = zipInputStream.getNextEntry()
} }
zipInputStream.close() zipInputStream.close()
resolve(dest) resolve(dest)