本文介绍了ExtractZipFile对科尔多瓦的Andr​​oid 2.3.0的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想知道你是怎么得到ExtractZipFile插件至最新2.3科尔多瓦。我曾试图让插件工作,但没赢。

I would like to know how you get ExtractZipFile plugin to the latest cordova 2.3. I have tried to get the plugin working but did not win.

链接插件

希望有人能帮助我。

关于

推荐答案

我修改维沙尔Rajpal(该ExtractZipFile插件的作者)code遵守科尔多瓦插件结构,记录在的和开发在Android 的一个插件。

I modified Vishal Rajpal (the author of the ExtractZipFile plugin) code to comply with the cordova plugin structure, as documented in Plugin Development Guide and Developing a Plugin on Android.

的Java code 被放在src目录下的组织/阿帕奇/科尔多瓦/插件/ ExtractZipFilePlugin.java

Java code to be put in the src directory under org/apache/cordova/plugin/ExtractZipFilePlugin.java

/*
    Author: Vishal Rajpal
    Filename: ExtractZipFilePlugin.java
    Created Date: 21-02-2012
    Modified Date: 21-02-2013
    Modified to comply with Cordova by: Ran Friedlender
*/

package org.apache.cordova.plugin;

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 org.apache.cordova.api.CallbackContext;
import org.apache.cordova.api.CordovaPlugin;
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("unzip"))
        {
            String filename = args.getString(0);
            unzip(filename, callbackContext);
            return true;
        }

        return false;
    }

    private void unzip(String filename, CallbackContext callbackContext)
    {
        File file = new File(filename);
        String[] dirToSplit = filename.split(File.separator);
        String dirToInsert = "";

        for (int i = 0; i < dirToSplit.length - 1; i++)
        {
            dirToInsert += dirToSplit[i] + File.separator;
        }

        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), 8192);
                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)
        {
            callbackContext.error(PluginResult.Status.MALFORMED_URL_EXCEPTION.toString());
            return;
        }
        catch (IOException e1)
        {
            callbackContext.error(PluginResult.Status.IO_EXCEPTION.toString());
            return;
        }

        callbackContext.success(filename);
    }
}



插件声明被放在RES下插件/ XML / config.xml中


Plugin declaration to be put in res/xml/config.xml under plugins

<plugin name="ZipPlugin" value="org.apache.cordova.plugin.ExtractZipFilePlugin" />



JavaScript的code 要包含在你的项目 - 文件ZipPlugin.js


JavaScript code to be included in your project - file ZipPlugin.js

/*
    Author: Vishal Rajpal
    Filename: ZipPlugin.js
    Created Date: 21-02-2012
    Modified Date: 21-02-2013
    Modified to comply with Cordova by: Ran Friedlender
*/

var ExtractZipFilePlugin = function()
{
};

ExtractZipFilePlugin.prototype.extractFile = function(file, successCallback, errorCallback)
{
    cordova.exec(successCallback, errorCallback, "ZipPlugin", "unzip", [file]);
};



使用示例

var ZipClient = new ExtractZipFilePlugin();
ZipClient.extractFile("my_path/my.zip", win, fail);

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

function fail(error)
{
    alert(error);
}



与科尔多瓦2.4.0测试成功


干杯!


Tested successfully with cordova 2.4.0
Cheers!

这篇关于ExtractZipFile对科尔多瓦的Andr​​oid 2.3.0的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-30 08:31