▼MIDP2.0メモ▼
圧縮データを解凍する


圧縮データを解凍するプログラムを作成する。



プログラム
InflateEx.java
import javax.microedition.lcdui.*;
import javax.microedition.midlet.*;

//圧縮データを解凍する(本体)
public class InflateEx extends MIDlet 
    implements Runnable {
    static InflateCanvas c;//キャンバス

    //コンストラクタ
    public InflateEx() {
        c=new InflateCanvas();
        (new Thread(this)).start();
    }
    
    //処理
    public void run() {
        Display.getDisplay(this).setCurrent(c);
        c.exe();
    }

    //アプリの開始
    public void startApp() {
    }

    //アプリの一時停止
    public void pauseApp() {
    }

    //アプリの終了
    public void destroyApp(boolean flag) {
    }
}


InflateCanvas.java
import com.jblend.io.*;
import javax.microedition.io.*;
import javax.microedition.lcdui.*;
import javax.microedition.midlet.*;
import java.io.*;

//圧縮データを解凍する(キャンバス)
class InflateCanvas extends Canvas {
    private final static String URL=//URL
        "http://www.saturn.dti.ne.jp/~npaka/sorami/test.jar";
    private Image image=null;//イメージ

    //処理
    void exe() {
        try {
            image=readImage(URL);
        } catch (Exception e) {
            e.printStackTrace();
        }
        repaint();
    }

    //描画
    public void paint(Graphics g) {
        g.setColor(255,255,255);
        g.fillRect(0,0,getWidth(),getHeight());
        if (image==null) {
            g.setColor(0,0,0);
            g.drawString("?",0,0,g.LEFT|g.TOP);
        } else {
            g.drawImage(image,0,0,g.LEFT|g.TOP);
        }
    }

    //イメージの読み込み
    private Image readImage(String url) throws Exception {
        byte[] w=readByte(url);
        w=extract(w);
        return Image.createImage(w,0,w.length);
    }

    //バイトデータの解凍
    private byte[] extract(byte[] data) throws Exception {
        int size;
        byte[] w=new byte[1024];
        InflateInputStream    in =null;
        ByteArrayOutputStream out=null;
        try {
            in =new InflateInputStream(
                new ByteArrayInputStream(data));
            out=new ByteArrayOutputStream();
            while (true) {
               size=in.read(w);
               if (size<=0) break;
               out.write(w,0,size);
           }
           out.close();
           in.close();
           return out.toByteArray();
       } catch (Exception e) {
           try {
               if (in !=null) in.close();
               if (out!=null) out.close();
           } catch (Exception e2) {
           }
           throw e;
       }
    }
    
    //バイトデータの読み込み
    private byte[] readByte(String url) throws Exception {
        byte[] w=new byte[10240];
        int rc,size;
        HttpConnection        c  =null;
        InputStream           in =null;
        ByteArrayOutputStream out=null;
        try {
            //ネットとの接続
            c  =(HttpConnection)Connector.open(url);
            in =c.openInputStream();
            out=new ByteArrayOutputStream();

            //レスポンスコードのチェック
            rc=c.getResponseCode();
            if (rc!=HttpConnection.HTTP_OK) {
                throw new Exception();
            }

            //データの読み込み
            while (true) {
                size=in.read(w);
                if (size<=0) break;
                out.write(w,0,size);
            }

            //ネットとの切断
            out.close();
            in.close();
            c.close();
            return out.toByteArray();
        } catch (Exception e) {
            //例外処理
            try {
                if (out!=null) out.close();
                if (in !=null) in.close();
                if (c  !=null) c.close();
            } catch (Exception e2) {
            }
            throw e;
        }
    }
}


S!アプリ(P6型/P7型)のJADファイルとMANIFESTファイル

InflateEx.jad
MIDlet-1: InflateEx, , InflateEx
MIDlet-Jar-Size: 3818
MIDlet-Jar-URL: InflateEx.jar
MIDlet-Name: InflateEx
MIDlet-Vendor: My Vendor
MIDlet-Version: 1.0
MicroEdition-Profile: MIDP-1.0
MicroEdition-Configuration: CLDC-1.0
MIDlet-Network: Y

MANIFEST.MF
MIDlet-1: InflateEx, , InflateEx
MIDlet-Name: InflateEx
MIDlet-Vendor: My Vendor
MIDlet-Version: 1.0
MicroEdition-Profile: MIDP-1.0
MicroEdition-Configuration: CLDC-1.0


Deflate圧縮ツール


DeflateCompressはS!アプリリソース用の圧縮ツール。使い方の書式は次の通り。
java DeflateCompress 圧縮対象ファイル 出力ファイル

使用例は次の通り。
java DeflateCompress sorami.png test.jar


【DeflateCompress.classのダウンロード】


DeflateCompress.java(JDK1.4)
import java.io.*;
import java.util.zip.*;
import java.util.ArrayList;
import java.util.List;

//Deflate圧縮ツール
public final class DeflateCompress {
    //更新
    public void compress(String[] param) {
        int size;
        byte[] input,output,result;
        Deflater compresser;
        try {
            input=readFile(param[0]);
            output=new byte[input.length*2];
            compresser=new Deflater(); 
            compresser.setInput(input); 
            compresser.finish();
            size=compresser.deflate(output);
            result=new byte[size];
            System.arraycopy(output,0,result,0,size);
            writeFile(param[1],result);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    //ファイル読み込み
    private byte[] readFile(String name) throws Exception {
        int size;
        byte[] w=new byte[10240];
        ByteArrayOutputStream out=null;
        InputStream           in =null;
        try {
            out=new ByteArrayOutputStream();
            in =new FileInputStream(name);
            while (true) {
                size=in.read(w);
                if (size<=0) break;
                out.write(w,0,size);
            }
            in.close();
            out.close();
            return out.toByteArray();
        } catch(Exception e) {
            try {
                if (in !=null) in.close();
                if (out!=null) out.close();
            } catch (Exception e2) {
            }
            throw e;
        }
    }

    //ファイルの書き込み
    private void writeFile(String name,byte[] data) throws Exception {
        FileOutputStream out=null;
        try {
            out=new FileOutputStream(name);
            out.write(data);
            out.close();
        } catch(Exception e) {
            try{ 
                if (out!=null) out.close();
            } catch (Exception e2) {
            }
            throw e;
        }
    }

    //メイン
    public static void main(String args[]) {
        if (args.length<2) {
            System.out.println("java DeflateCompress 圧縮対象ファイル 出力ファイル");
            System.exit(-1);
        }
        DeflateCompress compress=new DeflateCompress();
        compress.compress(args);
    }
}


ファイル結合ツール


複数のファイルを結合してからDeflate圧縮するための、ファイル結合ツール。使い方の書式は次の通り。
java FileConnect 圧縮対象フォルダ 出力ファイル

使用例は次の通り。
java FileConnect res r
java DeflateCompress r test.jar


【FileConnect.classのダウンロード】


内容 サイズ
ファイル名のサイズ1 2バイト
ファイル名のバイナリ1 ファイル名のサイズ分
ファイルのサイズ1 2バイト
ファイルのバイナリ1 ファイルのサイズ分
ファイル名のサイズ2 2バイト
ファイル名のバイナリ2 ファイルのサイズ分
ファイルのサイズ2 2バイト
ファイル名のサイズ2 ファイルのサイズ分


FileConnect.java(JDK1.4)
import java.io.*;
import java.util.List;
import java.util.ArrayList;

//ファイル結合
public final class FileConnect {

    //結合
    public void connect(String[] param) {
        int i,size;
        byte[] w=new byte[102400];
        String label;
        File file;
        String[] name;
        InputStream  in =null;
        OutputStream out=null;
        
        try {
            //ファイル名
            name=new File(param[0]).list();
            System.out.println("ファイル数>"+name.length);

            //接続
            out=new FileOutputStream(param[1]);

            //出力
            for (i=0;i<name.length;i++) {
                label=makeLabel(name[i]);
    
                //名前サイズ
                out.write(label.getBytes().length/256);
                out.write(label.getBytes().length%256);
                
                //名前
                out.write(label.getBytes());
    
                //データ読み込み
                file=new File(param[0]+"/"+name[i]);
                in=new FileInputStream(file);
                size=in.read(w);
                in.close();
                
                //データサイズ
                out.write(size/256);
                out.write(size%256);
                
                //データ
                out.write(w,0,size);
                System.out.println(label+"="+size);
            }
            out.close();
            
            //出力サイズ
            System.out.println("出力サイズ>"+(new File(param[1])).length());
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
    
    //ラベルの生成
    private String makeLabel(String name) {
        int idx;

        //フォルダ削除
        idx=name.indexOf("/");
        if (idx>=0) name=name.substring(idx+1);
        return name;
    }

    //メイン
    public static void main(String[] args) {
        if (args.length<2) {
            System.out.println("java FileConnect 圧縮対象フォルダ 出力ファイル");
            System.exit(-1);
        }
        FileConnect connect=new FileConnect();
        connect.connect(args);
    }
}


Deflate圧縮したバイナリから指定したファイル名のファイルのバイナリを取得するプログラムは次の通り。
Deflate圧縮バイナリからのファイル取得例
    //ファイルの読み込み
    public byte[] readFile(byte[] data,String label) throws Exception {
        int i,size;
        byte[] w=null;
        String l;
        InflateInputStream in=null;
        try {
            //ファイルとの接続
            in=new InflateInputStream(new ByteArrayInputStream(data));

            //ファイルからの読み込み
            while (true) {
                //ラベル
                size=(in.read()<<8)+in.read();
                if (size<=0) throw new Exception();
                w=new byte[size];
                for (i=0;i<size;i++) in.read(w,i,1);
                l=new String(w);

                //バイナリ
                size=(in.read()<<8)+in.read();
                if (size<=0) throw new Exception();
                w=new byte[size];
                for (i=0;i<size;i++) in.read(w,i,1);
                if (l.equals(label)) break;
            }

            //ファイルとの切断
            in.close();
            return w;
        } catch (Exception e) {
            try {
                if (in!=null) in.close();
            } catch (Exception e2) {
            }
            throw e;
        }
    }



−戻る−