無料で使えるシステムトレードフレームワーク「Jiji」 をリリースしました!

・OANDA Trade APIを利用した、オープンソースのシステムトレードフレームワークです。
・自分だけの取引アルゴリズムで、誰でも、いますぐ、かんたんに、自動取引を開始できます。

Zipの展開

Zipアーカイブを展開する関数です。

使い方。

InputStream in = null;
try {
    in = new FileInputStream( new File("./test.zip"));
    unzip( new File("./ext"), in);
} finally {
    if ( in != null ) {
        in.close();
    }
}

実装。

/**
 * ファイル({@link InputStream}) を指定されたディレクトリに解凍する。
 * @param dir 解凍先ディレクトリ
 * @param is ファイルの{@link InputStream}
 * @throws IOException 入出力例外が発生した場合
 */
public static void unzip(File dir, InputStream is )
throws IOException {

    ZipInputStream zis = new ZipInputStream(is);
    ZipEntry entry = null;
    int count = 0;
    try {
        while ((entry = zis.getNextEntry()) != null) {
            count++;
            try {
                unzipEntry ( entry, dir, zis );
            } finally {
                zis.closeEntry();
            }
        }
    } finally {
        if (zis != null) {
            try {
                zis.close();
            } catch (IOException e) {}
        }
    }
    if ( count == 0 ) {
        throw new IOException("stream is not zip.");
    }
}
/**
 * エントリーを展開する。
 * @param entry Zip内のエントリー
 * @param dir ファイルの保存先
 * @param zis {@link ZipInputStream}
 * @throws IOException 入出力例外が発生した場合
 */
private static final void unzipEntry ( ZipEntry entry, File dir, ZipInputStream zis )
throws IOException {
    File f  = null;
    if (entry.isDirectory()) {
        // フォルダ
        f = new File(dir, entry.getName());
        if ( !f.exists() && !f.mkdirs() ) {
            throw new IOException( "create directory faild. directory="
                + f.getAbsolutePath() );
        }
    } else {
        // ファイル
        f = new File(dir, entry.getName());
        File parent = new File(f.getParent());
        if ( !parent.exists() && !new File(f.getParent()).mkdirs()) {
            throw new IOException( "create directory faild. directory="
                + parent.getAbsolutePath() );
        }
        FileOutputStream out = new FileOutputStream(f);
        byte[] buf = new byte[1024];
        int size = 0;
        try {
            while ((size = zis.read(buf)) != -1) {
                out.write(buf, 0, size);
            }
        } finally {
            if (out != null) {
                try {
                    out.close();
                } catch (IOException e) { }
            }
        }
    }
}