[原]动态打jar包程序,可用于手机图片音乐游戏的动态打包
生活随笔
收集整理的這篇文章主要介紹了
[原]动态打jar包程序,可用于手机图片音乐游戏的动态打包
小編覺得挺不錯的,現在分享給大家,幫大家做個參考.
一般我們項目jar包,會使用java自帶的jar程序,或者使用IDE如eclipse build.xml,或者ant打包。
有一些情況需要動態打包,如根據客戶端的請求,把特有的信息或適配打包到jar中,下面是我寫的讀寫的例子程序,沒有添加異常處理和多線程,這個基本和業務相關。
代碼如下:
讀Jarpackage?test;?
import?java.io.FileInputStream;
import?java.util.Iterator;
import?java.util.jar.Attributes;
import?java.util.jar.JarInputStream;
import?java.util.jar.Manifest;
import?java.util.zip.ZipEntry;
/**
?*?讀取測試程序
?*?@author?pony
?*?@blog?http://pony.cnblogs.com
?*?@date?2010-02-24
?*?存在問題:entry文件名若使用unicode會出現亂碼
?*/
public?class?JarReadTest?{?
????/**
?????*?寫文件到jar包中
?????*?例子中,讀取一個文件,并將這個文件存儲到jar包中的文件中
?????*????????同時新建一個新的文件
?????*?@param?inputFileName
?????*?@param?outputFileName
?????*?@throws?Exception
?????*/
??public?static?void?Readjar(String?inputFileName)?throws?Exception?{?
????
????JarInputStream?in?=?new?JarInputStream(new?FileInputStream(inputFileName))?;
????Manifest?manifest?=?in.getManifest();
????Attributes?atts=manifest.getMainAttributes();
????//輸入所有的manifest信息
????Iterator?ite=atts.keySet().iterator();
????while(ite.hasNext())
????{
????????Object?key=ite.next();
????????System.out.println(key+":"+atts.getValue(key.toString()));
????}
????ZipEntry?entry=null;
????while((entry=in.getNextEntry())!=null)
????{
????????//輸入每個文件的名稱
????????System.out.println(entry.getName());
????????byte[]?buffer?=?new?byte[1024];
????????int?read;
????????while((read=in.read(buffer))!=-1)
????????{//輸出文件內容
????????????System.out.println(new?String(buffer));
????????}
????????in.closeEntry();
????}
????in.close();
??}?
??public?static?void?main(String[]?args)?{?
????try?{?
??????Readjar("c:\\打包.jar");?
????}?catch?(Exception?e)?{?
??????//?TODO?Auto-generated?catch?block?
??????e.printStackTrace();?
????}?
??}?
}
?
寫jarpackage?test;?
import?java.io.File;
import?java.io.FileInputStream;
import?java.io.FileOutputStream;
import?java.util.jar.JarEntry;
import?java.util.jar.JarOutputStream;
import?java.util.jar.Manifest;
/**
?*?將目標文件打包的測試程序
?*?@author?pony
?*?@blog?http://pony.cnblogs.com
?*?@date?2010-02-24
?*?存在問題:entry文件名若使用unicode會出現亂碼
?*/
public?class?JarWriteTest?{?
????/**
?????*?寫文件到jar包中
?????*?例子中,讀取一個文件,并將這個文件存儲到jar包中的文件中
?????*????????同時新建一個新的文件
?????*?@param?inputFileName
?????*?@param?outputFileName
?????*?@throws?Exception
?????*/
??public?static?void?Writejar(String?inputFileName,?String?outputFileName)?throws?Exception?{?
????//Mainifest是jar包特有的說明文件,不能通過手動編寫實現
????//它可以幫助你實現META-INF的目錄保存了一個叫MANIFEST.MF的文件,記錄版本,入口程序等信息
????Manifest?manifest?=?new?Manifest();
????manifest.getMainAttributes().putValue("Manifest-Version",?"1.0");
????manifest.getMainAttributes().putValue("author",?"pony");
????manifest.getMainAttributes().putValue("blog",?"http://pony.cnblogs.com");
????//JarOutputStream和JarInputStream是jar包生成時特有封裝stream
????File?outfile=new?File(outputFileName);
????JarOutputStream?out?=?new?JarOutputStream(new?FileOutputStream(outfile),manifest)?;
????File?f?=?new?File(inputFileName);?
?
????/************************將輸入文件讀取寫入jar?outputstream中**********************************/
????//JarEntry?是jar包目錄類
????JarEntry????entry=new?JarEntry(new?String("file1".getBytes(),"utf-8"));
????//將目錄加入到out中
????out.putNextEntry(entry);?
????FileInputStream?in?=?new?FileInputStream(f);?
????byte[]?buffer?=?new?byte[1024];?
????int?n?=?in.read(buffer);?
????while?(n?!=?-1)?{?
????????out.write(buffer,?0,?n);?
????????n?=?in.read(buffer);?
????}?
????in.close();?
????out.closeEntry();//關閉目錄
??????
??????
????//再做一個文件
????JarEntry?entry2=new?JarEntry("文件2");
????out.putNextEntry(entry2);?
????out.write("你好啊!".getBytes());
????out.closeEntry();
????out.flush();
????//注意關閉輸出文件流
????out.close();?
??}?
??public?static?void?main(String[]?args)?{?
????try?{?
??????Writejar("c:\\1.txt",?"c:\\打包.jar");?
????}?catch?(Exception?e)?{?
??????//?TODO?Auto-generated?catch?block?
??????e.printStackTrace();?
????}?
??}?
}
總結
以上是生活随笔為你收集整理的[原]动态打jar包程序,可用于手机图片音乐游戏的动态打包的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 【转载】请问Silverlight 获取
- 下一篇: CCNP-EIGRP不等价负载均衡