2013-01-18 20 views

回答

3

我修改維沙爾Rajpal代碼遵從科爾多瓦插件結構,如在Plugin Development GuideDeveloping a Plugin on Android記錄(所述ExtractZipFile插件的作者)。

Java代碼被放在src目錄下的組織/阿帕奇/科爾多瓦/插件/ 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 /配置。在插件的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); 
} 


與科爾多瓦成功測試2.4.0
乾杯!

+0

謝謝!這真的有助於 – Omega

+0

我在這一個問題,但現在我凸起了。感謝您展示路徑 –

+0

作爲參考,Nattawat Nonsung將其修改爲3.0.0 http://nattawat-nonsung.blogspot.hk/2013/11/android-plugin-to-extract-zip-file-in.html –

0

我也面臨同樣的問題,比我修改,這PhoneGap的插件解壓在2.6

Unzip plugin

相關問題