惯性聚合 高效追踪和阅读你感兴趣的博客、新闻、科技资讯
阅读原文 在惯性聚合中打开

推荐订阅源

P
Privacy International News Feed
I
Intezer
T
Tenable Blog
S
Schneier on Security
Project Zero
Project Zero
G
GRAHAM CLULEY
酷 壳 – CoolShell
酷 壳 – CoolShell
小众软件
小众软件
Know Your Adversary
Know Your Adversary
博客园 - 司徒正美
The Cloudflare Blog
Recent Commits to openclaw:main
Recent Commits to openclaw:main
freeCodeCamp Programming Tutorials: Python, JavaScript, Git & More
N
News and Events Feed by Topic
博客园 - 叶小钗
宝玉的分享
宝玉的分享
L
LINUX DO - 热门话题
aimingoo的专栏
aimingoo的专栏
S
Secure Thoughts
Forbes - Security
Forbes - Security
T
The Exploit Database - CXSecurity.com
D
Darknet – Hacking Tools, Hacker News & Cyber Security
OSCHINA 社区最新新闻
OSCHINA 社区最新新闻
博客园 - 【当耐特】
罗磊的独立博客
IT之家
IT之家
H
Hacker News: Front Page
I
InfoQ
云风的 BLOG
云风的 BLOG
S
Security Affairs
M
MIT News - Artificial intelligence
GbyAI
GbyAI
Jina AI
Jina AI
Help Net Security
Help Net Security
Engineering at Meta
Engineering at Meta
大猫的无限游戏
大猫的无限游戏
Webroot Blog
Webroot Blog
L
Lohrmann on Cybersecurity
A
About on SuperTechFans
Attack and Defense Labs
Attack and Defense Labs
The Register - Security
The Register - Security
V
V2EX
G
Google Developers Blog
D
DataBreaches.Net
Apple Machine Learning Research
Apple Machine Learning Research
C
Cybersecurity and Infrastructure Security Agency CISA
J
Java Code Geeks
W
WeLiveSecurity
Cloudbric
Cloudbric
T
Tor Project blog

博客园 - 马森

Java IO测试样例-字节流-字符流 java中的io系统详解 MongoDB资料汇总 字符编码笔记:ASCII,Unicode和UTF-8 jQuery和ExtJS的timeOut超时设置和event事件处理 Sybase字符串函数-数学函数-系统函数 inux 设置系统时间和硬件时间 Sybase采用固定表+存储过程实现分页 Wordpress XML-RPC协议说明 struts2 action 配置方法 &&struts2的配置文件 理解 SET CHAINED command not allowed within multi-statement transaction. sybase性能优化 - 马森 - 博客园 Java引用对象SoftReference WeakReference PhantomReference(二) [整理]centos下配置和安装 jstl字符串处理 Java,Tomcat,Mysql乱码总结 [原]output parameters have not yet been processed源自返回了多个数据集 [转]MS SQL Server数据库事务锁机制分析 tomcat支持ssl时Keystore was tampered with, or password was incorrect
[原]动态打jar包程序,可用于手机图片音乐游戏的动态打包
马森 · 2010-02-25 · via 博客园 - 马森

一般我们项目jar包,会使用java自带的jar程序,或者使用IDE如eclipse build.xml,或者ant打包。

有一些情况需要动态打包,如根据客户端的请求,把特有的信息或适配打包到jar中,下面是我写的读写的例子程序,没有添加异常处理和多线程,这个基本和业务相关。

代码如下:

读Jar


package 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(); 
    } 
  } 
}

写jar


package 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(); 
    } 
  } 
}