diff --git a/README.md b/README.md index 314e0e5..ba149b9 100644 --- a/README.md +++ b/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((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); +} +``` + diff --git a/package.json b/package.json new file mode 100644 index 0000000..8afa5c7 --- /dev/null +++ b/package.json @@ -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" +} diff --git a/plugin.xml b/plugin.xml new file mode 100644 index 0000000..4b04482 --- /dev/null +++ b/plugin.xml @@ -0,0 +1,29 @@ + + + ZipCordovaPlugin + + + + + + + + + + + + + + + + + + + + + + + + + diff --git a/src/android/ZipCordovaPlugin.java b/src/android/ZipCordovaPlugin.java new file mode 100644 index 0000000..922d6b1 --- /dev/null +++ b/src/android/ZipCordovaPlugin.java @@ -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)); + } +} diff --git a/src/ios/ZipCordovaPlugin.m b/src/ios/ZipCordovaPlugin.m new file mode 100644 index 0000000..74eed52 --- /dev/null +++ b/src/ios/ZipCordovaPlugin.m @@ -0,0 +1,49 @@ +/********* zip-cordova-plugin.m Cordova Plugin Implementation *******/ + +#import +#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 *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 diff --git a/www/zip.js b/www/zip.js new file mode 100644 index 0000000..438b2ba --- /dev/null +++ b/www/zip.js @@ -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]); +};