📄 makewar.java
字号:
/** * $RCSfile: MakeWAR.java,v $ * $Revision: 1.3 $ * $Date: 2002/04/29 21:43:14 $ * * Copyright (C) 1999-2002 CoolServlets, Inc. All rights reserved. * * This software is the proprietary information of CoolServlets, Inc. * Use is subject to license terms. */package com.jivesoftware.util;import java.io.*;import java.util.zip.*;import java.awt.*;import java.awt.event.*;import javax.swing.*;import javax.swing.border.EmptyBorder;/** * A simple tool to create a WAR file given a directory. Currently, it only * operates in GUI mode. Command-line support may be added later. * * @author Matt Tucker */public class MakeWAR extends JFrame implements ActionListener { private static ZipOutputStream zos; private static JProgressBar progressBar; private JFileChooser warFileChooser; private JTextField warTextField; public static void main(String[] args) { // Use the native look and feel of the OS. try { UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName()); } catch (Exception e) { e.printStackTrace(); } // Create a new WAR building frame. new MakeWAR(); } public MakeWAR() { super("WAR Maker"); // Initialize text field and file chooser. warTextField = new JTextField(20); warFileChooser = new JFileChooser(); warFileChooser.setFileSelectionMode(JFileChooser.DIRECTORIES_ONLY); // If the WAR tool is started from a Jive installation, the application // directory would contain the directory to create a WAR with. Try // to make this the default directory. File warDir = new File("application"); if (warDir.exists()) { try { warTextField.setText(warDir.getCanonicalPath()); } catch (Exception e) { e.printStackTrace(); } warFileChooser.setCurrentDirectory(warDir.getAbsoluteFile().getParentFile()); } // Shut-down application when window closes. addWindowListener(new WindowAdapter() { public void windowClosing(WindowEvent e) { System.exit(0); } }); // Layout the GUI. JPanel mainPanel = new JPanel(); mainPanel.setBorder(new EmptyBorder(3, 3, 3, 3)); mainPanel.setLayout(new BoxLayout(mainPanel, BoxLayout.Y_AXIS)); JPanel dirPanel = new JPanel(); dirPanel.setBorder(BorderFactory.createTitledBorder( "Choose a Directory to Make Into a WAR")); dirPanel.add(warTextField); JButton button = new JButton("Browse"); button.addActionListener(this); dirPanel.add(button); 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, BorderLayout.EAST); mainPanel.add(dirPanel); mainPanel.add(panel3); // Add the panel to the frame getContentPane().add(mainPanel); pack(); // Center the window on the screen. Dimension screen = Toolkit.getDefaultToolkit().getScreenSize(); Dimension size = getSize(); setLocation((screen.width-size.width)/2,(screen.height-size.height)/2); setVisible(true); } public void actionPerformed(ActionEvent e) { if (e.getActionCommand().equals("Browse")) { warFileChooser.setCurrentDirectory(new File(warTextField.getText())); int returnVal = warFileChooser.showDialog(this, "Choose Directory"); if (returnVal == JFileChooser.APPROVE_OPTION) { try { warTextField.setText(warFileChooser.getSelectedFile().getCanonicalFile().getPath()); } catch (Exception ex) { ex.printStackTrace(); } } } else if (e.getActionCommand().equals("Create WAR")) { final File warDir = new File(warTextField.getText()); // See if this is a valid WAR directory. if (!(new File(warDir, "WEB-INF").exists())) { String msg = "The directory you chose does not appear to be a valid\n" + "web application directory. Please choose another."; JOptionPane.showMessageDialog(this, msg, "Error!", JOptionPane.ERROR_MESSAGE); } 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); final JFrame frame = this; Thread t = new Thread() { public void run() { // Use the makeZip method to create the WAR. try { File warFile = new File("jive.war"); makeZip(warDir, warFile); // Now display a message to the user saying where // the WAR file was written to. String msg = "The WAR file was successfully " + "written to: " + warFile.getCanonicalPath(); JOptionPane.showMessageDialog(frame, msg, "WAR Completed", JOptionPane.PLAIN_MESSAGE); } // Print out any errors we encounter. catch (Exception e) { System.err.println(e); } System.exit(0); } }; t.start(); } } } /** * Creates a Zip archive. If the name of the file passed in is a * directory, the directory's contents will be made into a Zip file. */ public static void makeZip(File directory, File zipFile) throws IOException, FileNotFoundException { dir = directory.getAbsolutePath(); zos = new ZipOutputStream(new FileOutputStream(zipFile)); recurseFiles(directory); // We are done adding entries to the zip archive, // so close the Zip output stream. zos.close(); } private static String dir; private static byte [] buf = new byte[1024]; private static int len; private static int totalFileCount; private static int doneFileCount; private static void countFiles(File file) { if (file.isDirectory()) { // Create an array with all of the files and subdirectories // of the current directory. String[] fileNames = file.list(); if (fileNames != null) { // Recursively add each array entry to make sure that we get // subdirectories as well as normal files in the directory. for (int i=0; i<fileNames.length; i++) { countFiles(new File(file, fileNames[i])); } } } // Otherwise, a file so add it as an entry to the Zip file. else { totalFileCount++; } } /** * Recurses down a directory and its subdirectories to look for * files to add to the Zip. If the current file being looked at * is not a directory, the method adds it to the Zip file. */ private static void recurseFiles(File file) throws IOException, FileNotFoundException { if (file.isDirectory()) { // Create an array with all of the files and subdirectories // of the current directory. String[] fileNames = file.list(); if (fileNames != null) { // Recursively add each array entry to make sure that we get // subdirectories as well as normal files in the directory. for (int i=0; i<fileNames.length; i++) { recurseFiles(new File(file, fileNames[i])); } } } // Otherwise, a file so add it as an entry to the Zip file. else { // Create a new Zip entry with the file's name. We want to use // relative path names, so subtract the main directory name // from the entry name. ZipEntry zipEntry = new ZipEntry((file.getAbsolutePath()).substring( dir.length() + File.separator.length())); // Create a buffered input stream out of the file // we're trying to add into the Zip archive. FileInputStream fin = new FileInputStream(file); BufferedInputStream in = new BufferedInputStream(fin); zos.putNextEntry(zipEntry); // Read bytes from the file and write into the Zip archive. while ((len = in.read(buf)) >= 0) { zos.write(buf, 0, len); } // Close the input stream. in.close(); // Close this entry in the Zip stream. zos.closeEntry(); doneFileCount++; progressBar.setValue(doneFileCount); } }}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -