/* -------------------------------------------------------------------------*
* PKUAS: Peking University Application Server
* Copyright (C) 2006 Peking University
*
* This software is free; you can redistribute it and/or
* modify it under the terms of the GNU Lesser General Public
* License as published by the Free Software Foundation; either
* either version 2.1 or any later version.
*
* This software is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public
* License along with this software; if not, write to the Free Software
* Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307
* USA
* ------------------------------------------------------------------------*/
import java.io.BufferedInputStream;
import java.io.BufferedOutputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.net.URL;
import java.util.ArrayList;
import java.util.jar.JarOutputStream;
import java.util.zip.ZipEntry;
import java.util.zip.ZipInputStream;
/**
* This is the Helper Class to manipulate Java Archive File
* @author Lin Liang
*/
public class JarMaker {
public JarMaker() {
}
public static boolean pack(String jarPath, String jarFileName) {
try {
String t_jarPathName = jarPath.replace('\\', File.separatorChar);
String t_jarFileName = jarFileName.replace('\\', File.separatorChar);
RealPackUtilityJar(t_jarPathName, t_jarFileName);
return true;
} catch (Exception ex) {
return false;
}
}
public static boolean utilityPack(String p_utilityFileName, String jarName) {
try {
// begin to pack the utility jar file.
String t_utilityfilename = p_utilityFileName.replace('\\', File.separatorChar);
String t_jarfilename = jarName.replace('\\', File.separatorChar) + ".jar";
// File utilityFile = new File(p_utilityFileName);
RealPackUtilityJar(t_utilityfilename, t_jarfilename);
// delete the utility directory!
} catch (Exception ex) {
return false;
}
return true;
}
public static void RealPackUtilityJar(String s_src, String s_dest) throws IOException {
URL _src, _dest;
File f_src = new File(s_src);
if (!f_src.isDirectory())
throw new IOException(s_src + " is not a directory");
_src = f_src.toURL();
_dest = new File(s_dest).toURL();
int path_l = s_dest.lastIndexOf("/");
if (path_l > 0) {
File dir = new File(s_dest.substring(0, path_l));
if (!dir.exists())
dir.mkdirs();
}
JarOutputStream jout = new JarOutputStream(new FileOutputStream(_dest.getFile()));
// put all into the jar...
add(jout, new File(_src.getFile()), "");
jout.close();
}
/**
* used by downloadAndPack
* @param _jout
* @param _dir
* @param _prefix
* @throws IOException
*/
public static void add(JarOutputStream _jout, File _dir, String _prefix) throws IOException {
File[] content = _dir.listFiles();
if (_dir.isDirectory()) {
for (int i = 0, l = content.length; i < l; ++i) {
if (content[i].isDirectory()) {
_jout.putNextEntry(new ZipEntry(_prefix + (_prefix.equals("") ? "" : "/") + content[i].getName() + "/"));
add(_jout, content[i], _prefix + (_prefix.equals("") ? "" : "/") + content[i].getName());
} else {
_jout.putNextEntry(new ZipEntry(_prefix + (_prefix.equals("") ? "" : "/") + content[i].getName()));
FileInputStream in = new FileInputStream(content[i]);
write(in, _jout);
in.close();
}
}
} else {
_jout.putNextEntry(new ZipEntry(_prefix + (_prefix.equals("") ? "" : "/") + _dir.getName()));
FileInputStream in = new FileInputStream(_dir);
write(in, _jout);
in.close();
}
}
/**
* writes the content of the InputStream into the OutputStream
* @param _in
* @param _out
* @throws IOException
*/
private static synchronized void write(InputStream _in, OutputStream _out) throws IOException {
byte[] buffer = new byte[1024 * 512];
int read;
while (true) {
read = _in.read(buffer);
if (read == -1)
break;
_out.write(buffer, 0, read);
}
_out.flush();
}
/**
* Deletes all files and subdirectories under dir.
* @param dir
* @return true if all deletions were successful.If a deletion fails, the
* method stops attempting to delete and returns false.
*/
public static boolean deleteDir(File dir) {
if (dir.isDirectory()) {
String[] children = dir.list();
for (int i = 0; i < children.length; i++) {
boolean success = deleteDir(new File(dir, children[i]));
if (!success) {
return false;
}
}
}
// The directory is now empty so delete it
return dir.delete();
}
/**
* @param f_name : source zip file
* @param dir_name : target dir file
*/
public static void unpackJar(File f_name, File dir_name) throws IOException {
unpackJar(f_name.getCanonicalPath(), dir_name.getCanonicalPath());
}
/**
* @param f_name : source zip file path name
* @param dir_name : target dir file path
*/
public static void unpackJar(String f_name, String dir_name) throws IOException {
BufferedInputStream bism = new BufferedInputStream(new FileInputStream(f_name));
ZipInputStream zism = new ZipInputStream(bism);
for (ZipEntry z = zism.getNextEntry(); z != null; z = zism.getNextEntry()) {
File f = new File(dir_name, z.getName());
if (z.isDirectory()) {
f.mkdirs();
} else {
f.getParentFile().mkdirs();
BufferedOutputStream bosm = new BufferedOutputStream(new FileOutputStream(f));
int i;
byte[] buffer = new byte[1024 * 512];
try {
while ((i = zism.read(buffer, 0, buffer.length)) != -1)
bosm.write(buffer, 0, i);
} catch (IOException ie) {
throw ie;
}
bosm.close();
}
}
zism.close();
bism.close();
}
/**
* Unpack the jar file to a directory, till teaf files
* @param file
* @param file1
* @throws IOException
*/
public static void unpackAppJar(File file, File file1) throws IOException {
unpackAppJar(file.getCanonicalPath(), file1.getCanonicalPath());
}
/**
* Unpack the jar file to a directory, till teaf files
* @param file The source file name
* @param file1 The target directory
* @author wangxp
* @version 2003/11/07
*/
public static void unpackAppJar(String file, String file1) throws IOException {
// m_log.debug("unpackAppJar begin. file="+file+"|||file1="+file1);
BufferedInputStream bufferedinputstream = new BufferedInputStream(new FileInputStream(file));
ZipInputStream zipinputstream = new ZipInputStream(bufferedinputstream);
byte abyte0[] = new byte[1024];
for (ZipEntry zipentry = zipinputstream.getNextEntry(); zipentry != null; zipentry = zipinputstream.getNextEntry()) {
File file2 = new File(file1, zipentry.getName());
if (zipentry.isDirectory()) {
if (!file2.exists() && !file2.mkdirs())
throw new IOException("Could not make directory " + file2.getPath());
} else {
File file3 = file2.getParentFile();
if (file3 != null && !file3.exists() && !file3.mkdirs())
throw new IOException("Could not make directory " + file3.getPath());
BufferedOutputStream bufferedoutputstream = new BufferedOutputStream(new FileOutputStream(file2));
int i;
try {
while ((i = zipinputstream.read(abyte0, 0, abyte0.length)) != -1)
bufferedoutputstream.write(abyte0, 0, i);
} catch (IOException ie) {
ie.printStackTrace();
// m_logger.error(ie);
throw ie;
}
bufferedoutputstream.close();
}
}
zipinputstream.close();
bufferedinputstream.close();
// m_log.debug("unpackAppJar end.");
}
/**
* Combine several jar files and some given files into one jar file.
* @param outJar The output jar file's filename.
* @param inJarsList The jar files to be combined
* @param filter A filter that exclude some entries of input jars. User
* should implement the EntryFilter interface.
* @param inFileList The files to be added into the jar file.
* @param prefixs The prefixs of files to be added into the jar file.
* inFileList and prefixs should be paired.
* @throws FileNotFoundException
* @throws IOException
*/
@SuppressWarnings("unchecked")
public static void combineJars(String outJar, String[] inJarsList, EntryFilter filter, String[] inFileList, String[] prefixs)
throws FileNotFoundException, IOException {
JarOutputStream jout = new JarOutputStream(new FileOutputStream(outJar));
ArrayList entryList = new ArrayList();
for (int i = 0; i < inJarsList.length; i++) {
BufferedInputStream bufferedinputstream = new BufferedInputStream(new FileInputStream(inJarsList[i]));
ZipInputStream zipinputstream = new ZipInputStream(bufferedinputstream);
byte abyte0[] = new byte[1024 * 4];
for (ZipEntry zipentry = zipinputstream.getNextEntry(); zipentry != null; zipentry = zipinputstream.getNextEntry()) {
if (filter.accept(zipentry)) {
if (!entryList.contains(zipentry.getName())) {
jout.putNextEntry(zipentry);
if (!zipentry.isDirectory()) {
int j;
try {
while ((j = zipinputstream.read(abyte0, 0, abyte0.length)) != -1)
jout.write(abyte0, 0, j);
} catch (IOException ie) {
throw ie;
}
}
entryList.add(zipentry.getName());
}
}
}
zipinputstream.close();
bufferedinputstream.close();
}
for (int i = 0; i < inFileList.length; i++) {
add(jout, new File(inFileList[i]), prefixs[i]);
}
jout.close();
}
/**
* Test
* @param args
* @throws Exception
*/
public static void main(String args[]) throws Exception {
String[] in = new String[2];
in[1] = "E:\\pkuas03\\pkuas_51\\repository\\deployed\\ecperf.ear\\corp.jar";
in[0] = "E:\\pkuas03\\pkuas_51\\repository\\deployed\\ecperf.ear\\orders.jar";
String[] fs = new String[1];
fs[0] = "E:\\pkuas03\\pkuas_51\\repository\\deployed\\ecperf.ear\\META-INF\\application.xml";
String[] pres = new String[1];
pres[0] = "META-INF";
combineJars("e:\\111.jar", in, new EntryFilter() {
public boolean accept(ZipEntry entry) {
if (entry.getName().endsWith("ejb-jar.xml"))
return false;
return true;
}
}, fs, pres);
}
interface EntryFilter {
public boolean accept(ZipEntry entry);
}
}
|