1

我想知道您如何将 ExtractZipFile 插件安装到最新的cordova 2.3。我试图让插件工作但没有获胜。

链接到插件 https://github.com/phonegap/phonegap-plugins/tree/master/Android/ExtractZipFile

希望可以有人帮帮我。

问候

4

2 回答 2

3

我修改了 Vishal Rajpal(ExtractZipFile 插件的作者)代码以符合 cordova 插件结构,如Plugin Development GuideDeveloping a Plugin on Android中所述。

Java 代码放在 org/apache/cordova/plugin/ExtractZipFilePlugin.java 下的 src 目录中

/*
    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 name="ZipPlugin" value="org.apache.cordova.plugin.ExtractZipFilePlugin" />


要包含在项目中的JavaScript 代码- 文件 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);
}


使用cordova 2.4.0成功测试
干杯!

于 2013-02-20T23:07:36.057 回答
0

我也面临与我修改相同的问题而且这个phonegap解压缩插件在2.6中工作正常

解压插件

于 2013-09-13T06:14:10.743 回答