读取,修改,操作,编辑,jar文件
文件选择 jarFileSelect()
private void jarFileSelect(MouseEvent evt) {
JFileChooser fileChooser = new JFileChooser(".");
fileChooser.setDialogTitle("editFrame");
fileChooser.setFileHidingEnabled(false);
fileChooser.setFileSelectionMode(JFileChooser.FILES_ONLY);
FileFilter filter = new FileNameExtensionFilter("jar文件", "jar");
fileChooser.addChoosableFileFilter(filter);
int choice = fileChooser.showOpenDialog(null);
if (choice != JFileChooser.APPROVE_OPTION) {
return;
}
contentTa.setText("");
File file = fileChooser.getSelectedFile();
jarFileTf.setText(file.getAbsolutePath());
}
有效性校验 valid()
private boolean valid() {
boolean ret = false;
if (!StringUtils.isEmpty(jarFileTf.getText()) && !StringUtils.isEmpty(fileTf.getText())) {
ret = true;
} else {
contentTa.setText("不能为空");
}
return ret;
}
根据stream获取byte getByte()
//从输入取字节
public byte[] getByte(java.io.InputStream s) {
byte[] buffer = new byte[0];
byte[] chunk = new byte[4096];
int count;
try {
while ((count = s.read(chunk)) >= 0) {
byte[] t = new byte[buffer.length + count];
System.arraycopy(buffer, 0, t, 0, buffer.length);
System.arraycopy(chunk, 0, t, buffer.length, count);
buffer = t;
}
s.close();
} catch (Exception e) {
}
return buffer;
}
打开jar文件 open()
private void open(MouseEvent evt) {
try {
if (!valid()) {
return;
}
String jarFileName = jarFileTf.getText();
String fileName = fileTf.getText();
File file = new File(jarFileName);
JarFile jarFile = new JarFile(file);
TreeMap tm = new TreeMap();
Enumeration es = jarFile.entries();
while (es.hasMoreElements()) {
JarEntry je = (JarEntry) es.nextElement();
byte[] bytes = getByte(jarFile.getInputStream(je));
tm.put(je.getName(), bytes);
if (je.getName().contains(fileName)) {
String str = new String(bytes);
contentTa.setText(str);
break;
}
}
} catch (Exception e) {
e.printStackTrace();
contentTa.setText("出错了!!!,文件格式不对");
}
JOptionPane.showMessageDialog(rootPane, "打开完成");
}
编辑 edit()
private void edit(MouseEvent evt) {
try {
if (!valid()) {
return;
}
String content = contentTa.getText();
if (content == null || content.equals("")) {
JOptionPane.showMessageDialog(rootPane, "不能为空");
return;
}
String jarFileName = jarFileTf.getText();
String fileName = fileTf.getText();
File file = new File(jarFileName);
JarFile jarFile = new JarFile(file);
TreeMap tm = new TreeMap();
Enumeration es = jarFile.entries();
while (es.hasMoreElements()) {
JarEntry je = (JarEntry) es.nextElement();
byte[] bytes = getByte(jarFile.getInputStream(je));
tm.put(je.getName(), bytes);
}
JarOutputStream out = new JarOutputStream(new FileOutputStream(file));
Set set = tm.entrySet();
Iterator it = set.iterator();
while (it.hasNext()) {
Map.Entry me = (Map.Entry) it.next();
String name = (String) me.getKey();
JarEntry jeNew = new JarEntry(name);
out.putNextEntry(jeNew);
byte[] b = (byte[]) me.getValue();
if (name.contains(fileName)) {
b = content.getBytes();
} else {
}
out.write(b, 0, b.length);
}
out.finish();
out.close();
JOptionPane.showMessageDialog(rootPane, "修改完成");
} catch (Exception e) {
e.printStackTrace();
contentTa.setText("出错了!!!,文件格式不对");
}
}