用java打war包的程序

来源:互联网 发布:gta5漂移数据mod 编辑:程序博客网 时间:2024/05/17 00:13

转自:http://topic.csdn.net/t/20050811/14/4202936.html


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/nweb 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;
}

 

 

原创粉丝点击