2005/11/03 | 用java打war包的程序
类别(JAVA相关) | 评论(0) | 阅读(1087) | 发表于 14:55
主 题:用java打war包的程序
作 者:石头心)(JSP学习中(angues1980)
时 间:2005-08-11 14:20:45

修改了别人的程序做了个swing界面,回去打个jar包就可以当个小工具用了 ^_^

package com.angus.util;
import javax.swing.*;
import javax.swing.border.EmptyBorder;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.*;
import java.util.jar.*;
public class MakeWAR extends JFrame implements ActionListener {
public static void main(String[] args) {
new MakeWAR();
}
public MakeWAR() {
super("WAR Maker");
warTextField = new JTextField(20);
warFileField = new JTextField(20);
JPanel mainPanel = new JPanel();
mainPanel.setBorder(new EmptyBorder(3, 3, 3, 3));
mainPanel.setLayout(new BoxLayout(mainPanel, 1));
JPanel dirPanel = new JPanel();
JPanel targetPanel = new JPanel();
dirPanel.setBorder(BorderFactory
.createTitledBorder("Enter a Directory to Make Into a WAR"));
targetPanel.setBorder(BorderFactory
.createTitledBorder("Enter a WAR File Name"));
dirPanel.add(warTextField);
targetPanel.add(warFileField);
JPanel panel3 = new JPanel();
panel3.setBorder(new EmptyBorder(5, 5, 5, 5));
panel3.setLayout(new BorderLayout());
JButton startButton = new JButton("Create WAR");
startButton.addActionListener(this);
panel3.add(startButton, "East");
mainPanel.add(dirPanel);
mainPanel.add(targetPanel);
mainPanel.add(panel3);
getContentPane().add(mainPanel);
pack();
Dimension screen = Toolkit.getDefaultToolkit().getScreenSize();
Dimension size = getSize();
setLocation((screen.width - size.width) / 2,
(screen.height - size.height) / 2);
this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setVisible(true);
}
public static void makeZip(File directory, File zipFile)
throws IOException, FileNotFoundException {
jos = new JarOutputStream(new FileOutputStream(zipFile), new Manifest());
String fileNames[] = directory.list();
if (fileNames != null) {
for (int i = 0; i < fileNames.length; i++)
recurseFiles(new File(directory, fileNames[i]), "");
}
jos.close();
}
private static void recurseFiles(File file, String pathName)
throws IOException, FileNotFoundException {
if (file.isDirectory()) {
pathName = pathName + file.getName() + "/";
jos.putNextEntry(new JarEntry(pathName));
String fileNames[] = file.list();
if (fileNames != null) {
for (int i = 0; i < fileNames.length; i++)
recurseFiles(new File(file, fileNames[i]), pathName);
}
} else {
JarEntry jarEntry = new JarEntry(pathName + file.getName());
System.out.println(pathName + " " + file.getName());
FileInputStream fin = new FileInputStream(file);
BufferedInputStream in = new BufferedInputStream(fin);
jos.putNextEntry(jarEntry);
while ((len = in.read(buf)) >= 0)
jos.write(buf, 0, len);
in.close();
jos.closeEntry();
doneFileCount++;
progressBar.setvalue(doneFileCount);
}
}
public void actionPerformed(ActionEvent e) {
final File warDir = new File(warTextField.getText());
String wfStr1 = new String(warFileField.getText());
String wfStr2;
int temp = wfStr1.trim().lastIndexOf(".");
if (temp != -1) {
wfStr2 = wfStr1.substring(0, temp);
} else {
wfStr2 = wfStr1;
}
final File warFile = new File(wfStr2 + ".war");
if (!(new File(warDir, "WEB-INF")).exists()) {
String msg = "The directory you chose does not appear to be a valid web application directory. Please choose another.";
JOptionPane.showMessageDialog(MakeWAR.this, msg, "Error!", 0);
} else if (wfStr2.trim().equals("")) {
String msg = "The WAR file name should be entered";
JOptionPane.showMessageDialog(MakeWAR.this, msg, "Error", 0);
} else {
countFiles(warDir);
progressBar = new JProgressBar(0, totalFileCount);
progressBar.setvalue(0);
progressBar.setStringPainted(true);
setVisible(false);
getContentPane().removeAll();
JPanel panel = new JPanel();
panel.add(progressBar);
getContentPane().add(panel);
pack();
setVisible(true);
Thread t = new Thread() {
public void run() {
try {
MakeWAR.makeZip(warDir, warFile);
String msg = "The WAR file was successfully written to: "
+ warFile.getCanonicalPath();
JOptionPane.showMessageDialog(MakeWAR.this, msg,
"WAR Completed", -1);
} catch (Exception e) {
System.err.println(e);
}
System.exit(0);
}
};
t.start();
}
}
private static void countFiles(File file) {
if (file.isDirectory()) {
String fileNames[] = file.list();
if (fileNames != null) {
for (int i = 0; i < fileNames.length; i++)
countFiles(new File(file, fileNames[i]));
}
} else {
totalFileCount++;
}
}
private static JarOutputStream jos;
private static byte buf[] = new byte[1024];
private static int len;
private JTextField warTextField;
private JTextField warFileField;
private static int totalFileCount;
private static int doneFileCount;
private static JProgressBar progressBar;
}

引用:http://csdn.eyeah.cn/3116/1119/1122/253156637.html
0

评论Comments