[英]Ionic 6 - Capacitor Filesystem - copy SQLite files from ZIP
Hello everyone and thanks for taking time to read and answer.大家好,感谢您花时间阅读和回答。
I am using Ionic 6 with Angular 14 and capacitor .我正在使用Ionic 6和Angular 14和电容器。 I need to copy the content of an ZIP file uploaded by the user (it contains .sqlite files ), and store it inside the Documents folder.
我需要复制用户上传的 ZIP 文件的内容(它包含.sqlite 文件),并将其存储在 Documents 文件夹中。 For that, I'm using JSZip library.
为此,我正在使用JSZip库。
public async unzipFolder(file: File): Promise<string[]> {
const zip: JSZip = await JSZip.loadAsync(file);
const promises: Promise<string>[] = [];
zip.forEach((relativePath: string, zipFile: JSZipObject) =>
promises.push(this.unzipFile(zipFile));
);
return await Promise.all(promises);
}
public async unzipFile(compressedFile: JSZipObject): Promise<string> {
return await compressedFile.async('string').then((data: string) => {
this.fileOpsService.write(compressedFile.name, data)
});
}
Everything works fine here.这里一切正常。 Problems comes when I try to store it in Documents folder using Filesystem plugin (capacitor) .
当我尝试使用Filesystem plugin (capacitor)将其存储在 Documents 文件夹中时出现问题。
public async write(
path: string,
data: string,
directory: Directory = Directory.Documents
): Promise<WriteFileResult> {
return await Filesystem.writeFile({
path: path,
directory: directory,
data: data,
recursive: false,
});
}
XCode's terminal throws this error: XCode 的终端抛出这个错误:
{"errorMessage":"Unable to save file"}
I tried File plugin (awasome-cordova-file) but it does not work.我尝试了文件插件(awasome-cordova-file),但它不起作用。
public async write(
path: string,
data: ArrayBuffer,
directory: Directory = Directory.Documents
): Promise<any> {
return await File.writeFile(path, '', data, {replace: true})
}
It throws the next error:它引发下一个错误:
[DocumentManager] Failed to associate thumbnails for picked URL file:///private/var/mobile/Containers/Shared/AppGroup/XXX/File%20Provider%20Storage/TEST_BATCH2.zip with the Inbox copy file:///private/var/mobile/Containers/Data/Application/XXX/tmp/io.ionic.starter.ivan-Inbox/TEST_BATCH2.zip: Error Domain=QLThumbnailErrorDomain Code=102 "(null)" UserInfo={NSUnderlyingError=0x282532cd0 {Error Domain=GSLibraryErrorDomain Code=3 "Generation not found" UserInfo={NSDescription=Generation not found}}}
Ok.好的。 After a few days, I finally found a solution.
几天后,我终于找到了解决方案。 Here is it, if it can help anyone...
这是它,如果它可以帮助任何人......
I changed the way of read from ZIP file by replacing 'string' with 'blob' as you can see in the following snippet:我通过将 'string' 替换为 'blob' 更改了从 ZIP 文件中读取的方式,如您在以下代码段中所见:
public async unzipFile(compressedFile: JSZipObject): Promise<void> {
return await compressedFile.async('blob').then(async (data: Blob) => {
const filename: string = compressedFile.name
.replace('.sqlite', 'SQLite.db');
const base64 = await this.convertBlobToBase64(data);
this.fileOpsService.write(filename, base64);
});
}
I create a convertBlobToBase64 helper function that looks like this:我创建了一个如下所示的 convertBlobToBase64 辅助函数:
private convertBlobToBase64 = (blob: Blob) => new Promise((resolve, reject) => {
const reader: FileReader = new FileReader();
reader.onerror = reject;
reader.onload = () => {
resolve(reader.result);
}
reader.readAsDataURL(blob);
})
And it works!它有效! Note that I replaced the filename because I need it in that format to read it with another plugin =)
请注意,我替换了文件名,因为我需要以这种格式使用另一个插件来读取它=)
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.