读取ear,jar压缩包里面的文件。

今天写ComFrame属性配置工具,参照网上的资料,实现了题目的功能。
支持原创啊。

这里的流程是这样的。层层剥皮。

 

//------------------
读取jar文件里面的文件
//----------------
import java.io.*;
import java.util.jar.JarEntry;
import java.util.jar.JarFile;

public class ReadFileImpl implements IReadFile{

public String readFile() {
String result = "";
try {
String currentJarPath = ReadFileImpl.class.getProtectionDomain().getCodeSource().getLocation().getFile();
JarFile currentJar = new JarFile("conf.jar");
JarEntry dbEntry = currentJar.getJarEntry("text.properties");
InputStream in = currentJar.getInputStream(dbEntry);

//FileInputStream fis = new FileInputStream(currentJarPath+"/text.properties");
BufferedReader br = new BufferedReader(new InputStreamReader(in));
String readLine = null;
while((readLine = br.readLine())!= null){
result = result + readLine;
}
} catch (IOException e) {
// TODO 自动生成 catch 块
e.printStackTrace();
}
return result;
}

}

//----------------------
解压某个文件 把conf.jar从UWFE.ear里面解压出来
//---------------------
JarFile currentJar = new JarFile(path + "/UWFE.ear");
JarEntry dbEntry = currentJar.getJarEntry("conf.jar");
InputStream in = currentJar.getInputStream(dbEntry);
FileOutputStream fos = new FileOutputStream(path+"/conff.jar", true);
int i = 0;
while ((i = in.read()) != -1) {
fos.write(i);
}
fos.flush();
fos.close();

//------------------------
打包 把conf.jar打包到UWFE.ear里面。
//------------------------
private static boolean EAR(){
try {

File tempJar = new File(filepath+"/UWFE1.ear");
tempJar.createNewFile();
JarFile jar = new JarFile(filepath+"/UWFE.ear");
FileOutputStream f = new FileOutputStream(tempJar);
JarOutputStream newJar = new JarOutputStream( f);
byte buffer[] = new byte[100];
int bytesRead;

// Add back the original files
Enumeration entries = jar.entries();

while (entries.hasMoreElements()) {
// Prompt for copy
JarEntry entry = (JarEntry) entries.nextElement();
if(entry.toString().equals("conf.jar"))
continue;
InputStream is = jar.getInputStream(entry);
newJar.putNextEntry(entry);
while ((bytesRead = is.read(buffer)) != -1) {
newJar.write(buffer, 0, bytesRead);
}
newJar.flush();
is.close();
} // Add new file last
jar.close();
JarEntry entry = new JarEntry("conf.jar");
newJar.putNextEntry(entry);
FileInputStream fis = new FileInputStream(filepath+"/conf.jar");
while ((bytesRead = fis.read(buffer)) != -1) {
newJar.write(buffer, 0, bytesRead);
}
newJar.flush();
fis.close();
newJar.close();
f.close();
return true;
} catch (Exception ex){
ex.getMessage();
return false;
}

 

如果需要转载,请标明出处,尊重偶的原创版权啊。

http://www.chinacsharp.net 

http://blog.csdn.net/keyboardsun

作者keyboardsun

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值