
org.jboss.maven.plugins.retro.util.JarUtil Maven / Gradle / Ivy
The newest version!
package org.jboss.maven.plugins.retro.util;
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.util.ArrayList;
import java.util.Collection;
import java.util.jar.Attributes;
import java.util.jar.JarEntry;
import java.util.jar.JarOutputStream;
import java.util.jar.Manifest;
import java.util.zip.Deflater;
import org.apache.commons.io.FileUtils;
public class JarUtil
{
public static ArrayList createJarEntries(File directory) throws IOException
{
ArrayList fileEntries = new ArrayList ();
//this.getLog().info("DEBUG: " + directory.getAbsolutePath());
Collection files = FileUtils.listFiles(directory, null, true);
for (Object fileObj : files)
{
String relativePath = fileObj.toString().replace(directory.getAbsolutePath() + File.separator, "");
relativePath = relativePath.replace('\\', '/');
byte[] content = FileUtils.readFileToByteArray((File) fileObj);
fileEntries.add(new JarFileEntry(relativePath, content));
}
return fileEntries;
}
public static File createJarFile(String inputPath, String jarFilePath) throws IOException
{
File outputDir = new File(inputPath);
ArrayList fileEntries = createJarEntries(outputDir);
Manifest manifest = new Manifest();
Attributes attributes = manifest.getMainAttributes();
attributes.putValue("Manifest-Version", "1.0");
attributes.putValue("Created-By", System.getProperty("java.vm.version") + " ("
+ System.getProperty("java.vm.vendor") + ")");
JarOutputStream stream = new JarOutputStream(new FileOutputStream(jarFilePath), manifest);
stream.setLevel(Deflater.BEST_COMPRESSION);
for (JarFileEntry fileEntry : fileEntries)
{
JarEntry jarEntry = new JarEntry(fileEntry.getName());
stream.putNextEntry(jarEntry);
stream.write(fileEntry.getContent());
}
stream.close();
return new File(jarFilePath);
}
}
© 2015 - 2025 Weber Informatics LLC | Privacy Policy