java修改jar包文件内容的一个实例 (四)

2014-11-24 07:39:58 · 作者: · 浏览: 3
ite(i);
}
baos.close();
} catch (IOException e) {
e.printStackTrace();
}
byte[] bytes = baos.toByteArray();
return bytes;
}

/** getters && setters~ */
public JTextArea getJta() {
return jta;
}
public void setJta(JTextArea jta) {
this.jta = jta;
}
public File getJarFile() {
return original;
}
public void setJarFile(File jarFile) {
this.original = jarFile;
}

/**
* 主方法~
* @param args
*/
public static void main(String[] args) {
new JarCfgEditor();
}
}


/**
* @author Bruce Yang
* 拖拽监听~
*/
class MyDropTargetListener extends DropTargetAdapter {
private JarCfgEditor jce;
public MyDropTargetListener(JarCfgEditor jce) {
this.jce = jce;
}
@Override
@SuppressWarnings("unchecked")
public void drop(DropTargetDropEvent event) {
if (event.isDataFlavorSupported(DataFlavor.javaFileListFlavor)) {
event.acceptDrop(DnDConstants.ACTION_COPY_OR_MOVE);

DataFlavor df = DataFlavor.javaFileListFlavor;
List list = null;
try {
list = (List)(event.getTransferable().getTransferData(df));
} catch (Exception e) {
e.printStackTrace();
}
Iterator iterator = list.iterator();
while (iterator.hasNext()) {
File file = iterator.next();
if(file.exists() && file.isFile()) {
String filePath = file.getAbsolutePath();
if(filePath == null || filePath.equals("")) {
System.out.println("文件名为 null 或为 \"\"~");
break;
}
if(!filePath.contains("GetVerticesMVC") || !filePath.endsWith(".jar")) {
String str = "此工具专门为 GetVerticesMVC 设计,不通用!! 请注意!!";
JOptionPane.showMessageDialog(null, str);
break;
}
System.out.println("jarFilePath=" + filePath);
jce.setJarFile(file);
JarFile jarFile = null;
try {
jarFile = new JarFile(filePath);
ZipEntry entry = jarFile.getEntry(jce.configPath);
if(entry == null) {
System.out.println(jce.configPath+"路径所代表的文件不存在!读取失败~");
// 安全起见,将 jarFile 置为 null,这样在关闭窗口的时候将不会执行收尾操作~
jce.setJarFile(null);
break;
}

//获取到inputstream了 就相当简单了
InputStream is = jarFile.getInputStream(entry);
byte[] bytes = JarCfgEditor.inputStream2byteArray(is);
String cfgStr = new String(bytes);
jce.getJta().setText(cfgStr);
} catch (IOException e) {
e.printStackTrace();
}
}
// 一次只能处理一个,要避免处理多个的情况,因此 break 跳出~
b