Hello Folks,
Its about after 2 months I thought of writing a post. So here is it. I was thinking to extract a .zip file from PhoneGap on android and I ended up implementing a plugin for it.
Include the below .java file with the same package mentioned.
/*
Author: Vishal Rajpal
Filename: ExtractZipFilePlugin.java
Date: 21-02-2012
*/
package com.phonegap.plugin.ExtractZipFile;
import java.io.BufferedInputStream;
import java.io.BufferedOutputStream;
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
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 com.phonegap.api.Plugin;
import com.phonegap.api.PluginResult;
public class ExtractZipFilePlugin extends Plugin {
@Override
public PluginResult execute(String arg0, JSONArray args, String arg2) {
PluginResult.Status status = PluginResult.Status.OK;
JSONArray result = new JSONArray();
try {
String filename = args.getString(0);
File file = new File(filename);
String[] dirToSplit=filename.split("/");
String dirToInsert="";
for(int i=0;i<dirToSplit.length-1;i++)
{
dirToInsert+=dirToSplit[i]+"/";
}
BufferedOutputStream dest = null;
BufferedInputStream is = null;
ZipEntry entry;
ZipFile zipfile;
try {
zipfile = new ZipFile(file);
Enumeration 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 = dirToInsert + 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) {
// TODO Auto-generated catch block
return new PluginResult(PluginResult.Status.MALFORMED_URL_EXCEPTION);
} catch (IOException e1) {
// TODO Auto-generated catch block
return new PluginResult(PluginResult.Status.IO_EXCEPTION);
}
} catch (JSONException e) {
// TODO Auto-generated catch block
return new PluginResult(PluginResult.Status.JSON_EXCEPTION);
}
return new PluginResult(status);
}
}
Include the below .js file in your phonegap project.
/*
Author: Vishal Rajpal
Filename: ZipPlugin.js
Date: 21-02-2012
*/
var ExtractZipFilePlugin=function(){
};
PhoneGap.addConstructor(function()
{
PhoneGap.addPlugin('ExtractZipFilePlugin', new ZipPlugin());
});
ExtractZipFilePlugin.prototype.extractFile = function(file, successCallback, errorCallback)
{
alert(file);
return PhoneGap.exec(successCallback, errorCallback, "ZipPlugin", "extract", [file]);
};
Lastly include this plugin in the plugins.xml of your project.
<plugin name="ZipPlugin" value="com.phonegap.plugin.ExtractZipFile.ExtractZipFilePlugin" />
Usage i.e. .html file
<!--
Author: Vishal Rajpal
Filename: index.html
Date: 21-02-2012
-->
<html>
<head>
<script type="text/javascript" src="phonegap-1.3.0.js"></script>
<script type="text/javascript" src="ZipPlugin.js"></script>
<script type="text/javascript">
document.addEventListener("deviceready",onDeviceReady);
function onDeviceReady()
{
}
function extractFile(fileName)
{
alert(fileName);
var ZipClient = new ExtractZipFilePlugin();
ZipClient.extractFile('sdcard/'+fileName,win,fail,'ExtractZipFilePlugin');
}
function win(status)
{
alert('Success'+status);
}
function fail(error)
{
alert(error);
}
</script>
</head>
<body>
<input type="button" value="Extract Zip File" onClick="extractFile('SampleDir/AnotherDir/SampleFile.zip');"/>
</body>
</html>
The default location for the file has to be sdcard then it can be as many directories or no directory i.e.
extractFile('SampleFile.zip');
The plugin will work both ways.
Thanks. Any suggestions or comments will be helpful.