fixed unzip issue
This commit is contained in:
parent
b08267a085
commit
f53ed08540
2 changed files with 56 additions and 53 deletions
|
@ -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();
|
|
||||||
}
|
}
|
||||||
});
|
});
|
||||||
},
|
},
|
||||||
|
|
|
@ -173,36 +173,38 @@ 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 zipOutputStream = new java.util.zip.ZipOutputStream(outputStream)
|
const outputStream = ContentResolver.openOutputStream(destFile)
|
||||||
|
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
|
let buffer = Array.create('byte', 4096)
|
||||||
let buffer = Array.create('byte', 4096)
|
let file = sourceFiles.get(i)
|
||||||
let file = sourceFiles.get(i)
|
let entry = new java.util.zip.ZipEntry(
|
||||||
let entry = new java.util.zip.ZipEntry(
|
'EnRecipes/' +
|
||||||
'EnRecipes/' +
|
new java.io.File(src)
|
||||||
new java.io.File(src)
|
.toURI()
|
||||||
.toURI()
|
.relativize(file.toURI())
|
||||||
.relativize(file.toURI())
|
.getPath()
|
||||||
.getPath()
|
)
|
||||||
)
|
zipOutputStream.putNextEntry(entry)
|
||||||
zipOutputStream.putNextEntry(entry)
|
if (!file.isDirectory()) {
|
||||||
if (!file.isDirectory()) {
|
let inputStream = new java.io.FileInputStream(file)
|
||||||
let inputStream = new java.io.FileInputStream(file)
|
while ((len = inputStream.read(buffer)) != -1) {
|
||||||
while ((len = inputStream.read(buffer)) != -1) {
|
zipOutputStream.write(buffer, 0, len)
|
||||||
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) {
|
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()
|
|
||||||
while (entry != null) {
|
|
||||||
filepath = dest + '/' + entry.getName()
|
filepath = dest + '/' + entry.getName()
|
||||||
if (!entry.isDirectory()) {
|
let output = new java.io.File(filepath)
|
||||||
const output = new java.io.BufferedOutputStream(
|
if (entry.isDirectory()) {
|
||||||
new java.io.FileOutputStream(filepath)
|
output.mkdirs()
|
||||||
)
|
continue
|
||||||
while ((len = zipInputStream.read(buffer)) != -1) {
|
|
||||||
output.write(buffer, 0, len)
|
|
||||||
}
|
|
||||||
output.close()
|
|
||||||
} else {
|
|
||||||
let dir = new java.io.File(filepath)
|
|
||||||
dir.mkdirs()
|
|
||||||
}
|
}
|
||||||
|
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()
|
zipInputStream.closeEntry()
|
||||||
entry = zipInputStream.getNextEntry()
|
|
||||||
}
|
}
|
||||||
zipInputStream.close()
|
zipInputStream.close()
|
||||||
resolve(dest)
|
resolve(dest)
|
||||||
|
|
Loading…
Reference in a new issue