Cordova-iOS上的Zip文件和文件夹 [英] Cordova - Zip files and folders on iOS
问题描述
我正在使用Cordova在iOS上创建移动应用程序.我想将多个文件上传到服务器,而不是一个接一个地发送.我想将这些文件压缩为一个".zip"文件,然后将其上传到服务器.
I am using Cordova to create a mobile application on iOS. I want to upload multiple files to server but instead of sending them one after another. I want to compress these files into one '.zip' file and upload it to server.
我搜索了Cordova插件,但是我发现只有这些插件无法解决我的问题:
I searched for a Cordova plugin, but I found only these plugins that doesn't solve my problem:
- Icenium/cordova-plugin-zip 可以在iOS上压缩和解压缩文件,但是没有关于如何使用它的文档或示例.
- jjdltc/jjdltc-cordova-plugin-zip 可以压缩和解压缩文件仅在Android平台上.
- Icenium/cordova-plugin-zip to zip and unzip files on iOS but there is no documentation or example about how using it.
- jjdltc/jjdltc-cordova-plugin-zip to zip and unzip files on Android platform only.
请帮我找到一个可在iOS上压缩文件和文件夹的插件,或者给我一个有关使用第一个插件的示例.
Please, help me to find a plugin that zip files and folders on iOS, or give me an example about using the first plugin.
推荐答案
We have managed to unzip, modify and zip back files using the JSZip javascript API and the cordova file plugin. It should also work to create a zip from scratch. There isn't a real need to use native code for the zipping action (although it is most likely faster), only to read the files to zip and write the zip file. Hence, it is fine to use a javascript API that wasn't designed for cordova in particular.
过程 如果您想尝试JSZip +文件插件方法,这里是我们如何使用它的快速概述:
Process If you want to try the JSZip + file plugin method, here is a quick outline of how we worked with it :
- 我们使用文件插件以二进制形式读取文件.如果您很幸运只有文本文件,那么您也可以将其读取为文本,但是这种方式的灵活性较差.请注意,要读取文件,您需要从其
FileEntry
获取File
对象,这需要使用DirectoryEntry
在文件系统中进行导航.如果您不熟悉文件插件,请查看其文档以执行此步骤. - 我们创建一个JSZip对象.
- 根据需要操纵JSZip对象.您可以在zip中创建文件夹,添加文件,删除其中一些,修改某些内容.如您所见.他们的文档提供了简单而良好的示例.
- 使用JSZip
JSZip#generate()
方法生成zip二进制内容,并指定类型.如果要使用它创建一个实际文件,我们注意到可以使用文件插件的write方法(创建文件后)写入字符串和arraybuffer,而无需在iOS上将其转换的代码,但不能使用uint8array(我们没有尝试过)其他世代类型). - 根据需要处理二进制文件.就像您阅读了实际zip的二进制文件一样.
- We use the file plugin to read files as binary. If you are lucky enough to have only text files, you could read as text too, but it's less flexible that way. Note that to read the files, you will need to obtain the
File
object from theirFileEntry
which will require navigating in the file system usingDirectoryEntry
s. If you aren't familiar with the file plugin, take a look at its documentation to do this step. - We create a JSZip object.
- Manipulate the JSZip object as you want it. You can create folders within the zip, add files, remove some, modify some content. As you see fit. Their documentation gives simple and good examples.
- Generate the zip binary content using the JSZip
JSZip#generate()
method, specifying the type. If you want to create an actual file with it, we noticed that string and arraybuffer could be written with the file plugin's write method (after creating the file) without code to convert it on iOS, but not uint8array (and we didn't try the other generation types). - Treat the binary as you wish. It is the same as if you had read the binary of an actual zip.
PS:该文件插件在cordova.apache.org上有一些过时的文档.尽管其中的示例可能有用,但是请注意其中一些不再有效.例如,resolveLocalFileSystemURI()
现在是resolveLocalFileSystemURL()
.
PS: The file plugin has some outdated documentation on cordova.apache.org. While the examples in it can be useful, be aware that some of those are not valid anymore. For instance, resolveLocalFileSystemURI()
is now resolveLocalFileSystemURL()
.
这篇关于Cordova-iOS上的Zip文件和文件夹的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!