/**
*
*/
package jarutils;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.util.Collections;
import java.util.Iterator;
import java.util.List;
import java.util.jar.JarEntry;
import java.util.jar.JarFile;
import java.util.jar.JarOutputStream;
import java.util.zip.ZipEntry;
/**
* @author dw-wang
*
*/
public final class JarUtils
{
private static final int BUFFER_SIZE = 4096;
/**
* Utility class - no instances allowed.
*/
private JarUtils ()
{
// no instances allowed -- provides only static helper functions
}
/**
* Extract a jar archive into the base directory
* <code>baseDir</code>.
*
* @param baseDir the root directory where the archive is extracted
* to.
* @param archive jar file.
* @throws IOException in case of an I/O error.
*/
public static void extractJarArchive (File baseDir, File archive)
throws IOException
{
final JarFile archiveFile = new JarFile(archive);
final List archiveEntries = Collections.list(archiveFile.entries());
for (final Iterator iterator = archiveEntries.iterator(); iterator
.hasNext();)
{
InputStream in = null;
FileOutputStream out = null;
try
{
final ZipEntry e = (ZipEntry) iterator.next();
in = archiveFile.getInputStream(e);
final File f = new File(baseDir, e.getName());
if (e.isDirectory())
{
if (!f.exists())
{
if (!f.mkdirs())
{
throw new IOException("Cannot create directory "
+ f);
}
}
}
else
{
if (!f.getParentFile().exists())
{
if (!f.getParentFile().mkdirs())
{
throw new IOException("Cannot create directory "
+ f.getParentFile());
}
}
out = new FileOutputStream(f);
copy(in, out);
}
}
finally
{
IoUtil.close(out);
IoUtil.close(in);
}
}
}
/**
* Creates a jar archive from the directory.
*
* @param baseDir for the jar archive.
* @param archive the jar file.
* @throws IOException in case of an I/O error.
*/
public static void createJarArchive (File baseDir, File archive)
throws IOException
{
JarOutputStream jarArchive = null;
try
{
jarArchive = new JarOutputStream(new FileOutputStream(archive));
addFileToJar(baseDir, baseDir, jarArchive);
}
finally
{
IoUtil.close(jarArchive);
}
}
private static void addFileToJar (File baseDir, File file,
JarOutputStream archive)
throws IOException
{
if (file == null)
{
// done
}
else if (file.isDirectory())
{
String path = FileUtils.getRelativePath(baseDir, file);
(1)(2)(3)(4) if(!(3)path.equals("/") && !(4)path.equals(""))
{
if (!path.endsWith("/"))
{
(5) path (5)+= "/";
}
final JarEntry entry = new JarEntry(path);
archive.putNextEntry(entry);
(6) archive.closeEntry();
}
final File[] files = file.listFiles();
for (int i = 0; i < files.length; i++)
{
addFileToJar(baseDir, files[i], archive);
}
}
else
{
final String path = FileUtils.getRelativePath(baseDir, file);
final JarEntry entry = new JarEntry(path);
archive.putNextEntry(entry);
InputStream in = null;
try
{
in = new FileInputStream(file);
copy(in, archive);
}
finally
{
IoUtil.close(in);
}
archive.closeEntry();
}
}
/**
* Copies the content of the input stream <code>in</code> to the
* output stream <code>out</code>.
*
* @param in input stream.
* @param out output stream.
* @throws IOException in case of an I/O error.
*/
private static void copy (InputStream in, OutputStream out)
throws IOException
{
final byte[] buffer = new byte[BUFFER_SIZE];
int nread;
while ((nread = in.read(buffer)) != -1)
{
out.write(buffer, 0, nread);
}
}
}