This function will create a Jar archive containing the src * file/directory. The archive will be written to the specified * OutputStream.
* *This is a shortcut for
* jar(out, new File[] { src }, null, null, null);
This function will create a Jar archive containing the src * file/directory. The archive will be written to the specified * OutputStream.
* *This is a shortcut for
* jar(out, src, null, null, null);
This function will create a Jar archive containing the src * file/directory. The archive will be written to the specified * OutputStream. Directories are processed recursively, applying the * specified filter if it exists. * *
This is a shortcut for
* jar(out, src, filter, null, null);
This function will create a Jar archive containing the src
* file/directory. The archive will be written to the specified
* OutputStream. Directories are processed recursively, applying the
* specified filter if it exists.
*
* @param out The output stream to which the generated Jar archive is
* written.
* @param src The file or directory to jar up. Directories will be
* processed recursively.
* @param filter The filter to use while processing directories. Only
* those files matching will be included in the jar archive. If
* null, then all files are included.
* @param prefix The name of an arbitrary directory that will precede all
* entries in the jar archive. If null, then no prefix will be
* used.
* @param man The manifest to use for the Jar archive. If null, then no
* manifest will be included.
* @throws IOException
*/
public static void jar(OutputStream out, File[] src, FileFilter filter,
String prefix, Manifest man) throws IOException
{
for (int i = 0; i < src.length; i++)
{
if (!src[i].exists())
{
throw new FileNotFoundException(src.toString());
}
}
JarOutputStream jout;
if (man == null)
{
jout = new JarOutputStream(out);
}
else
{
jout = new JarOutputStream(out, man);
}
if (prefix != null && prefix.length() > 0 && !prefix.equals("/"))
{
// strip leading '/'
if (prefix.charAt(0) == '/')
{
prefix = prefix.substring(1);
}
// ensure trailing '/'
if (prefix.charAt(prefix.length() - 1) != '/')
{
prefix = prefix + "/";
}
}
else
{
prefix = "";
}
JarInfo info = new JarInfo(jout, filter);
for (int i = 0; i < src.length; i++)
{
jar(src[i], prefix, info);
}
jout.close();
}
/**
* This simple convenience class is used by the jar method to reduce the
* number of arguments needed. It holds all non-changing attributes
* needed for the recursive jar method.
*/
private static class JarInfo
{
public JarOutputStream out;
public FileFilter filter;
public byte[] buffer;
public JarInfo(JarOutputStream out, FileFilter filter)
{
this.out = out;
this.filter = filter;
buffer = new byte[1024];
}
}
/**
* This recursive method writes all matching files and directories to
* the jar output stream.
*/
private static void jar(File src, String prefix, JarInfo info)
throws IOException
{
JarOutputStream jout = info.out;
if (src.isDirectory())
{
// create / init the zip entry
prefix = prefix + src.getName() + "/";
ZipEntry entry = new ZipEntry(prefix);
entry.setTime(src.lastModified());
entry.setMethod(JarOutputStream.STORED);
entry.setSize(0L);
entry.setCrc(0L);
jout.putNextEntry(entry);
jout.closeEntry();
// process the sub-directories
File[] files = src.listFiles(info.filter);
for (int i = 0; i < files.length; i++)
{
jar(files[i], prefix, info);
}
}
else if (src.isFile())
{
// get the required info objects
byte[] buffer = info.buffer;
// create / init the zip entry
ZipEntry entry = new ZipEntry(prefix + src.getName());
entry.setTime(src.lastModified());
jout.putNextEntry(entry);
// dump the file
FileInputStream in = new FileInputStream(src);
int len;
while ((len = in.read(buffer, 0, buffer.length)) != -1)
{
jout.write(buffer, 0, len);
}
in.close();
jout.closeEntry();
}
}
public static void unjar(InputStream in, File dest) throws IOException
{
if (!dest.exists())
{
dest.mkdirs();
}
if (!dest.isDirectory())
{
throw new IOException("Destination must be a directory.");
}
JarInputStream jin = new JarInputStream(in);
byte[] buffer = new byte[1024];
ZipEntry entry = jin.getNextEntry();
while (entry != null)
{
String fileName = entry.getName();
if (fileName.charAt(fileName.length() - 1) == '/')
{
fileName = fileName.substring(0, fileName.length() - 1);
}
if (fileName.charAt(0) == '/')
{
fileName = fileName.substring(1);
}
if (File.separatorChar != '/')
{
fileName = fileName.replace('/', File.separatorChar);
}
File file = new File(dest, fileName);
if (entry.isDirectory())
{
// make sure the directory exists
file.mkdirs();
jin.closeEntry();
}
else
{
// make sure the directory exists
File parent = file.getParentFile();
if (parent != null && !parent.exists())
{
parent.mkdirs();
}
// dump the file
OutputStream out = new FileOutputStream(file);
int len = 0;
while ((len = jin.read(buffer, 0, buffer.length)) != -1)
{
out.write(buffer, 0, len);
}
out.flush();
out.close();
jin.closeEntry();
file.setLastModified(entry.getTime());
}
entry = jin.getNextEntry();
}
/* Explicity write out the META-INF/MANIFEST.MF so that any headers such
as the Class-Path are see for the unpackaged jar
*/
Manifest mf = jin.getManifest();
if (mf != null)
{
File file = new File(dest, "META-INF/MANIFEST.MF");
File parent = file.getParentFile();
if( parent.exists() == false )
{
parent.mkdirs();
}
OutputStream out = new FileOutputStream(file);
mf.write(out);
out.flush();
out.close();
}
jin.close();
}
/** Given a URL check if its a jar url(jar: