Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Modify android ExtractZipFile-plugin to keep consistent with its ios verison #113

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
22 changes: 11 additions & 11 deletions Android/ExtractZipFile/README
Original file line number Diff line number Diff line change
Expand Up @@ -12,26 +12,26 @@ Adding the Plugin to your project
Create a folder called 'com/phonegap/plugin/ExtractZipFile' within your project's src folder and copy ExtractZipFilePlugin.java file into that new folder.

Add a plugin line to res/xml/plugins.xml
<plugin name="ZipPlugin" value="com.phonegap.plugin.ExtractZipFile.ExtractZipFilePlugin" />
<plugin name="ExtractZipFilePlugin" value="org.apache.cordova.plugin.ExtractZipFile.ExtractZipFilePlugin" />

Using the plugin
================
function extractFile(fileName)
function extractFile(fileName, destination)
{
var ZipClient = new ExtractZipFilePlugin();
ZipClient.extractFile('sdcard/'+fileName,win,fail,'ExtractZipFilePlugin');
ZipClient.extractFile('sdcard/'+fileName, 'sdcard/'+destination,win,fail);
}

function win(status)
{
alert('Success'+status);
}

function fail(error)
function win(destination)
{
alert(error);
alert('Success unzip to: ' + destination);
}

function fail(errormsg)
{
alert('Fail to unzip: ' + errormsg);
}

Function Call
=============
<input type="button" value="Extract Zip File" onClick="extractFile('ZipFolder/ZipFile.zip');"/>
<input type="button" value="Extract Zip File" onClick="extractFile('FileName.zip', 'destinationFolder');"/>

This file was deleted.

Original file line number Diff line number Diff line change
@@ -0,0 +1,108 @@
/*
Author: Vishal Rajpal
Filename: ExtractZipFilePlugin.java
Created Date: 21-02-2012
Modified Date: 31-01-2013
*/

package org.apache.cordova.plugin.ExtractZipFile;

import java.io.BufferedInputStream;
import java.io.BufferedOutputStream;
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.UnsupportedEncodingException;
import java.net.URLDecoder;
import java.net.URLEncoder;
import java.util.Enumeration;
import java.util.zip.ZipEntry;
import java.util.zip.ZipException;
import java.util.zip.ZipFile;

import org.json.JSONArray;
import org.json.JSONException;

import org.apache.cordova.api.CordovaPlugin;
import org.apache.cordova.api.CallbackContext;
import org.apache.cordova.api.PluginResult;

public class ExtractZipFilePlugin extends CordovaPlugin {

@Override
public boolean execute(String action, JSONArray args, CallbackContext callbackContext) throws JSONException {
if (action.equals("extract")){
try {
String filename = URLDecoder.decode(args.getString(0), "UTF-8");
if (filename.startsWith("file://")){
filename = filename.substring(7, filename.length());
}else if (filename.startsWith("/")){
filename = filename.substring(1, filename.length());
}
String destination = URLDecoder.decode(args.getString(1), "UTF-8");
if (destination.startsWith("file://")){
destination = destination.substring(7, destination.length());
}else if (destination.startsWith("/")){
destination = destination.substring(1, destination.length());
}
PluginResult res = this.unzip(filename, destination);
callbackContext.sendPluginResult(res);
} catch (UnsupportedEncodingException e) {}
return true;
}else {
return false;
}
}

public PluginResult unzip(String filename, String destination){
File file = new File(filename);
if (!destination.endsWith("/")){
destination = destination + "/";
}
BufferedOutputStream dest = null;
BufferedInputStream is = null;
ZipEntry entry;
ZipFile zipfile;
try {
zipfile = new ZipFile(file);
Enumeration<? extends ZipEntry> e = zipfile.entries();
while (e.hasMoreElements())
{
entry = (ZipEntry) e.nextElement();
is = new BufferedInputStream(zipfile.getInputStream(entry));
int count;
byte data[] = new byte[102222];
String fileName = destination + entry.getName();
File outFile = new File(fileName);
if (entry.isDirectory())
{
outFile.mkdirs();
}
else
{
FileOutputStream fos = new FileOutputStream(outFile);
dest = new BufferedOutputStream(fos, 102222);
while ((count = is.read(data, 0, 102222)) != -1)
{
dest.write(data, 0, count);
}
dest.flush();
dest.close();
is.close();
}
}
} catch (ZipException e1) {
return new PluginResult(PluginResult.Status.ERROR, "Invalid zip file");
} catch (IOException e1) {
return new PluginResult(PluginResult.Status.ERROR, "I/O error");
}
String encoding;
try {
encoding = URLEncoder.encode(destination, "UTF-8");
} catch (UnsupportedEncodingException e) {
encoding = "";
}
return new PluginResult(PluginResult.Status.OK, encoding);
}

}
16 changes: 8 additions & 8 deletions Android/ExtractZipFile/www/ZipPlugin.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,18 +2,18 @@
Author: Vishal Rajpal
Filename: ZipPlugin.js
Created Date: 21-02-2012
Modified Date: 04-04-2012
Modified Date: 30-01-2013
*/

var ExtractZipFilePlugin=function(){
};
var ExtractZipFilePlugin = function(){
}

PhoneGap.addConstructor(function()
{
PhoneGap.addPlugin('ExtractZipFilePlugin', new ExtractZipFilePlugin());
cordova.addConstructor(function(){
if(!window.plugins) window.plugins = {};
window.plugins.extractZipFile = new ExtractZipFilePlugin();
});

ExtractZipFilePlugin.prototype.extractFile = function(file, successCallback, errorCallback)
ExtractZipFilePlugin.prototype.extractFile = function(file, destination, successCallback, errorCallback)
{
return PhoneGap.exec(successCallback, errorCallback, "ZipPlugin", "extract", [file]);
return cordova.exec(successCallback, errorCallback, "ExtractZipFilePlugin", "extract", [file, destination]);
};
Loading