完成插件开发
This commit is contained in:
39
README.md
39
README.md
@@ -1,2 +1,41 @@
|
||||
# zip-cordova-plugin
|
||||
|
||||
## 说明
|
||||
|
||||
用于将指定的文件列表压缩为一个zip文件
|
||||
|
||||
## 适用平台
|
||||
|
||||
+ android
|
||||
+ ios
|
||||
|
||||
##安装方法
|
||||
|
||||
```shell
|
||||
#cordova安装
|
||||
cordova plugin add git+http://m.shuto.cn:8680/public/zip-cordova-plugin.git
|
||||
|
||||
#ionc安装
|
||||
ionic cordova plugin add git+http://m.shuto.cn:8680/public/zip-cordova-plugin.git
|
||||
```
|
||||
|
||||
## 调用方法
|
||||
|
||||
```typescript
|
||||
declare var zip;
|
||||
|
||||
try{
|
||||
await new Promise<void>((resolve, reject) =>{
|
||||
zip.zip(`/zip/file/path`,['item','path','array'], () => {
|
||||
console.log('zip archive created');
|
||||
resolve();
|
||||
}, (error) => {
|
||||
console.error('zip created error',error);
|
||||
reject(error);
|
||||
});
|
||||
});
|
||||
}catch (e){
|
||||
console.error(error);
|
||||
}
|
||||
```
|
||||
|
||||
|
||||
20
package.json
Normal file
20
package.json
Normal file
@@ -0,0 +1,20 @@
|
||||
{
|
||||
"name": "zip-cordova-plugin",
|
||||
"version": "1.0.0",
|
||||
"description": "cordova zip plugin",
|
||||
"main": "zip.js",
|
||||
"scripts": {
|
||||
"test": "echo \"Error: no test specified\" && exit 1"
|
||||
},
|
||||
"repository": {
|
||||
"type": "git",
|
||||
"url": "git+http://m.shuto.cn:8086/public/zip-cordova-plugin"
|
||||
},
|
||||
"keywords": [
|
||||
"zip",
|
||||
"cordova",
|
||||
"ionic"
|
||||
],
|
||||
"author": "fandd",
|
||||
"license": "MIT"
|
||||
}
|
||||
29
plugin.xml
Normal file
29
plugin.xml
Normal file
@@ -0,0 +1,29 @@
|
||||
<?xml version='1.0' encoding='utf-8'?>
|
||||
<plugin id="zip-cordova-plugin" version="1.0.0" xmlns="http://apache.org/cordova/ns/plugins/1.0"
|
||||
xmlns:android="http://schemas.android.com/apk/res/android">
|
||||
<name>ZipCordovaPlugin</name>
|
||||
<js-module name="zip" src="www/zip.js">
|
||||
<clobbers target="zip"/>
|
||||
</js-module>
|
||||
<dependency id="cordova-plugin-cocoapod-support"/>
|
||||
<platform name="ios">
|
||||
<config-file parent="/*" target="config.xml">
|
||||
<feature name="ZipCordovaPlugin">
|
||||
<param name="ios-package" value="ZipCordovaPlugin"/>
|
||||
</feature>
|
||||
</config-file>
|
||||
<pods-config ios-min-version="8.0"></pods-config>
|
||||
<pod name="objective-zip" git="http://m.shuto.cn:8680/public/Objective-Zip.git" branch="master" />
|
||||
|
||||
<source-file src="src/ios/ZipCordovaPlugin.m"/>
|
||||
</platform>
|
||||
<platform name="android">
|
||||
<config-file parent="/*" target="res/xml/config.xml">
|
||||
<feature name="ZipCordovaPlugin">
|
||||
<param name="android-package" value="com.shuto.plugin.zip.ZipCordovaPlugin"/>
|
||||
</feature>
|
||||
</config-file>
|
||||
<config-file parent="/*" target="AndroidManifest.xml"></config-file>
|
||||
<source-file src="src/android/ZipCordovaPlugin.java" target-dir="src/com/shuto/plugin/zip"/>
|
||||
</platform>
|
||||
</plugin>
|
||||
64
src/android/ZipCordovaPlugin.java
Normal file
64
src/android/ZipCordovaPlugin.java
Normal file
@@ -0,0 +1,64 @@
|
||||
package com.shuto.plugin.zip;
|
||||
|
||||
import org.apache.cordova.CordovaPlugin;
|
||||
import org.apache.cordova.CallbackContext;
|
||||
|
||||
import org.json.JSONArray;
|
||||
import org.json.JSONException;
|
||||
|
||||
import java.io.BufferedInputStream;
|
||||
import java.io.File;
|
||||
import java.io.FileInputStream;
|
||||
import java.io.FileOutputStream;
|
||||
import java.io.IOException;
|
||||
import java.net.URI;
|
||||
import java.util.zip.ZipEntry;
|
||||
import java.util.zip.ZipOutputStream;
|
||||
|
||||
public class ZipCordovaPlugin extends CordovaPlugin {
|
||||
|
||||
@Override
|
||||
public boolean execute(String action, JSONArray args, CallbackContext callbackContext) throws JSONException {
|
||||
if (action.equals("zip")) {
|
||||
String path = args.getString(0);
|
||||
JSONArray files = args.getJSONArray(1);
|
||||
this.zip(path,files, callbackContext);
|
||||
return true;
|
||||
}
|
||||
return false;
|
||||
}
|
||||
|
||||
private void zip(String path,JSONArray files, CallbackContext callbackContext) {
|
||||
File zipFile = this.uri2File(path);
|
||||
try (
|
||||
FileOutputStream out = new FileOutputStream(zipFile);
|
||||
ZipOutputStream zos = new ZipOutputStream(out);
|
||||
){
|
||||
for (int i = 0; i < files.length(); i++) {
|
||||
File itemFile = this.uri2File(files.getString(i));
|
||||
try(
|
||||
FileInputStream in = new FileInputStream(itemFile);
|
||||
BufferedInputStream bi = new BufferedInputStream(in);
|
||||
){
|
||||
ZipEntry entry = new ZipEntry(itemFile.getName());
|
||||
zos.putNextEntry(entry);
|
||||
byte buffer[] = new byte[2048];
|
||||
int len;
|
||||
while ((len = bi.read(buffer, 0, 2048)) != -1) {
|
||||
zos.write(buffer, 0, len);
|
||||
}
|
||||
}catch (IOException e){
|
||||
throw e;
|
||||
}
|
||||
}
|
||||
callbackContext.success(zipFile.getAbsolutePath());
|
||||
} catch (IOException | JSONException e) {
|
||||
e.printStackTrace();
|
||||
callbackContext.error(e.getMessage());
|
||||
}
|
||||
}
|
||||
|
||||
private File uri2File(String url){
|
||||
return new File(URI.create(url));
|
||||
}
|
||||
}
|
||||
49
src/ios/ZipCordovaPlugin.m
Normal file
49
src/ios/ZipCordovaPlugin.m
Normal file
@@ -0,0 +1,49 @@
|
||||
/********* zip-cordova-plugin.m Cordova Plugin Implementation *******/
|
||||
|
||||
#import <Cordova/CDV.h>
|
||||
#import "Objective-Zip.h"
|
||||
#import "OZZipWriteStream.h"
|
||||
#import "OZZipCompressionLevel.h"
|
||||
|
||||
@interface ZipCordovaPlugin : CDVPlugin {
|
||||
// Member variables go here.
|
||||
}
|
||||
|
||||
- (void)zip:(CDVInvokedUrlCommand*)command;
|
||||
@end
|
||||
|
||||
@implementation ZipCordovaPlugin
|
||||
|
||||
- (void)zip:(CDVInvokedUrlCommand*)command
|
||||
{
|
||||
NSLog(@"ZIP PLUGIN: zip");
|
||||
CDVPluginResult* pluginResult = nil;
|
||||
NSString* path = [command.arguments objectAtIndex:0];
|
||||
path = [self url2path:path];
|
||||
NSLog(@"ZIP PLUGIN: path:%@",path);
|
||||
NSArray<NSString *> *files = [command.arguments objectAtIndex:1];
|
||||
OZZipFile *zipFile= [[OZZipFile alloc] initWithFileName:path
|
||||
mode:OZZipFileModeCreate];
|
||||
@try{
|
||||
for (NSString* file in files){
|
||||
OZZipWriteStream *stream= [zipFile writeFileInZipWithName:[file substringFromIndex: ([file length] -5)]
|
||||
compressionLevel:OZZipCompressionLevelBest];
|
||||
NSData* data = [NSData dataWithContentsOfFile: [self url2path:file]];
|
||||
[stream writeData:data];
|
||||
[stream finishedWriting];
|
||||
}
|
||||
pluginResult = [CDVPluginResult resultWithStatus:CDVCommandStatus_OK messageAsString:path];
|
||||
}
|
||||
@catch(NSException *exception){
|
||||
pluginResult = [CDVPluginResult resultWithStatus:CDVCommandStatus_ERROR messageAsString:exception];
|
||||
}
|
||||
[zipFile close];
|
||||
[self.commandDelegate sendPluginResult:pluginResult callbackId:command.callbackId];
|
||||
}
|
||||
|
||||
-(NSString*) url2path:(NSString*)url {
|
||||
NSRange range = [url rangeOfString:@"file://"];
|
||||
return [url stringByReplacingCharactersInRange:range withString:@""];
|
||||
}
|
||||
|
||||
@end
|
||||
17
www/zip.js
Normal file
17
www/zip.js
Normal file
@@ -0,0 +1,17 @@
|
||||
var exec = require('cordova/exec');
|
||||
|
||||
exports.zip = function (path,files, success, error) {
|
||||
console.log("zip plugin:",path,files,success,error);
|
||||
if(!path){
|
||||
console.log("zip plugin: path must be provided");
|
||||
error(new Error('path must be provided'));
|
||||
return;
|
||||
}
|
||||
if(!files || !Array.isArray(files) || files.length <1){
|
||||
console.log("zip plugin: at least one file must be provided");
|
||||
error(new Error('at least one file must be provided'));
|
||||
return;
|
||||
}
|
||||
console.log("zip plugin: native code");
|
||||
exec(success, error, 'ZipCordovaPlugin', 'zip', [path,files]);
|
||||
};
|
||||
Reference in New Issue
Block a user