Home  | My Account  |  Membership
WorldDeveloper.org
Forum Details
Topics
Posts
Categories
 
577 member(s)
718,905 hit(s)
14 categories
51 topics
26 messages
0 online
Forums: Code Post
Midlet Icon Extractor
hostj2me
43 topic(s)
23 post(s)

January 21, 2009 5:13 PM

HostJ2ME.com uses the code below to extract application icons from Midlet jars and display them in the HostJ2ME.com midlet directory.


package com.astrientlabs.hostj2me.util;

import java.io.ByteArrayOutputStream;
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.util.Properties;
import java.util.zip.ZipEntry;
import java.util.zip.ZipException;
import java.util.zip.ZipFile;

import com.astrientlabs.files.AstrientFile;
import com.astrientlabs.util.Strings;

public class IconExtractor
{
private String MANIFEST = "META-INF/MANIFEST.MF";

public byte[] extract(String path) throws ZipException, IOException
{
return extract(new File(path));
}

public byte[] extract(File jarFile) throws ZipException, IOException
{
ByteArrayOutputStream baos = new ByteArrayOutputStream(1024);
ZipFile zipFile = new ZipFile(jarFile);
try
{
/*for ( Enumeration? extends ZipEntry> e = zipFile.entries(); e.hasMoreElements(); )
{
System.out.println(e.nextElement().getName());
}*/

ZipEntry entry = zipFile.getEntry(MANIFEST);

InputStream is = zipFile.getInputStream(entry);
if ( is != null )
{
Properties props = new Properties();
props.load(is);

String iconFile = props.getProperty("MIDlet-Icon");
if ( Strings.isNull(iconFile) )
{
props.list(System.out);
String midlet1 = props.getProperty("MIDlet-1");
if ( !Strings.isNull(midlet1) )
{
String[] parts = midlet1.split(",");
if ( parts.length > 2 )
{
iconFile = parts[1].trim();
if ( iconFile.startsWith("/") )
{
iconFile = iconFile.substring(1);
}
}
}
}

if ( iconFile != null )
{
ZipEntry iconEntry = zipFile.getEntry(iconFile);

if ( iconEntry != null )
{
FileOutputStream fos = new FileOutputStream("/tmp/icon.png");
InputStream zis = zipFile.getInputStream(iconEntry);
int r = 0;
byte[] buffer = new byte[4*1024];
while( (r = zis.read(buffer)) != -1 )
{
baos.write(buffer,0,r);
fos.write(buffer,0,r);
}

fos.close();
zis.close();
}
}
}
}
finally
{
zipFile.close();
}

return baos.toByteArray();
}

public static void main(String[] args)
{

File jarFile = new File("/projects/workspace/cliqmobile/builds/cliqmobile.jar");
IconExtractor extractor = new IconExtractor();
try
{
byte[] data = extractor.extract(jarFile);
System.out.println(data.length + " " + AstrientFile.fileType(data));
}
catch (ZipException e)
{
// TODO Auto-generated catch block
e.printStackTrace();
}
catch (IOException e)
{
// TODO Auto-generated catch block
e.printStackTrace();
}

System.exit(0);
}
}


©2008 WorldDeveloper.org - PO Box 310575 Miami, FL 33231

WorldDeveloper.org is a managed social network for application developers around the world.