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

View file

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