| |
"Calling JAVA From C"
Vol. 9, Issue 8, p. 38
Listing 1: Java code to list contents of a zip file
1 import java.util.zip.*;
2 import java.util.Enumeration;
3
4 ZipFile zf = new ZipFile(zipFileName);
5 Enumeration entries = zf.entries();
6 while(entries.hasMoreElements())
7 {
8 String entry = ((ZipEntry)(entries.nextElement())).getName();
9 }
Listing 2: Excerpt of C code to list contents of zip file
1 int zipEntry(char *csEntryName)
2 {
3 jboolean isException;
4 jstring jsEntryName;
5 jobject oZipEntry = (*jni)->CallObjectMethod(jni, oZipEntries,
6 midEnumNextElement);
7 isException = checkException();
8 if (isException || oZipEntry == NULL) return 0;
9
10 jsEntryName = (*jni)->CallObjectMethod(jni, oZipEntry, midZipGetName);
11 isException = checkException();
12 if (isException || jsEntryName == NULL) return 0;
13 else
14 {
15 const char *tempData =(*jni)->GetStringUTFChars(jni, jsEntryName, 0);
16 isException = checkException();
17 if (tempData == NULL || isException) return 0;
18
19 // copy to caller's buffer and release the UTF
20 strcpy(csEntryName, (char*)tempData);
21 (*jni)->ReleaseStringUTFChars(jni, jsEntryName, tempData);
22 isException = checkException();
23 return (!isException);
24 }
25 }
Listing 3: CJProxy
1 package cj;
2
3 public interface CJProxy
4 {
5 public Object execute(Object args) throws Exception;
6 }
Listing 4: Java zip proxy
1 package cj.example;
2
3 import cj.CJProxy;
4 import java.io.*;
5 import java.util.zip.*;
6 import java.util.Enumeration;
7
8 /**
9 * CJZipList is a CJProxy that lists the entries in a zip file.
10 */
11 public class CJZipList implements CJProxy
12 {
13 /**
14 * Proxy execute takes a string with the name of the zip file.
15 * It returns a pipe-delimited string with the list of entries
16 * in the zip file.
17 */
18 public Object execute(Object args) throws Exception
19 {
20 if (!(args instanceof String))
21 {
22 throw new RuntimeException("Invalid type for execute");
23 }
24
25 StringBuffer retbuf = new StringBuffer("");
26 ZipFile zf = new ZipFile((String)args);
27 Enumeration entries = zf.entries();
28 while(entries.hasMoreElements())
29 {
30 String entry = ((ZipEntry)(entries.nextElement())).getName();
31 retbuf.append(entry);
32 retbuf.append("|");
33 }
34
35 return retbuf.toString();
36 }
37 }
|
|