NOTE: This is a followup to my question here.
I have a program that takes the contents of a directory and bundles everything into a JAR file. The code I use to do this is here:
try
{
FileOutputStream stream = new FileOutputStream(target);
JarOutputStream jOS = new JarOutputStream(stream);
LinkedList<File> fileList = new LinkedList<File>();
buildList(directory, fileList);
JarEntry jarAdd;
String basePath = directory.getAbsolutePath();
byte[] buffer = new byte[4096];
for(File file : fileList)
{
String path = file.getPath().substring(basePath.length() + 1);
path.replaceAll("\\\\", "/");
jarAdd = new JarEntry(path);
jarAdd.setTime(file.lastModified());
jOS.putNextEntry(jarAdd);
FileInputStream in = new FileInputStream(file);
while(true)
{
int nRead = in.read(buffer, 0, buffer.length);
if(nRead <= 0)
break;
jOS.write(buffer, 0, nRead);
}
in.close();
}
jOS.close();
stream.close();
So, all is well and good and the jar gets created, and when I explore its contents with 7-zip it has all the files I need. However, when I try to access the contents of the Jar via a URLClassLoader (the jar is not on the classpath and I don't intend it to be), I get null pointer exceptions.
The odd thing is, when I use a Jar that I've exported from Eclipse, I can access the contents of it in the way I want. This leads me to believe that I'm somehow not creating the Jar correctly, and am leaving something out. Is there anything missing from the method up above?
I figured it out based on this question - the problem was me not properly handling backslashes.
Fixed code is here:
FileOutputStream stream = new FileOutputStream(target);
JarOutputStream jOS = new JarOutputStream(stream);
LinkedList<File> fileList = new LinkedList<File>();
buildList(directory, fileList);
JarEntry entry;
String basePath = directory.getAbsolutePath();
byte[] buffer = new byte[4096];
for(File file : fileList)
{
String path = file.getPath().substring(basePath.length() + 1);
path = path.replace("\\", "/");
entry = new JarEntry(path);
entry.setTime(file.lastModified());
jOS.putNextEntry(entry);
FileInputStream in = new FileInputStream(file);
while(true)
{
int nRead = in.read(buffer, 0, buffer.length);
if(nRead <= 0)
break;
jOS.write(buffer, 0, nRead);
}
in.close();
jOS.closeEntry();
}
jOS.close();
stream.close();
Related
I have a program that needs to be able to create an executable JAR-file of itself, but I'm unfortunately having some trouble making it work. This is the method I'm currently using to create the JAR:
public static void createJar() throws IOException {
Manifest manifest = new Manifest();
manifest.getMainAttributes().put(Attributes.Name.MANIFEST_VERSION, "1.0");
manifest.getMainAttributes().put(Attributes.Name.MAIN_CLASS, "JarTest");
JarOutputStream jos = null;
try {
String jarPath = System.getProperty("user.dir") + "/Test.jar";
jos = new JarOutputStream(new FileOutputStream(jarPath), manifest);
}
catch (IOException e) {e.printStackTrace();}
ArrayList<String> fileList = new ArrayList<String>();
String codeDir = System.getProperty("user.dir") + "/bin/jartest/";
Files.list(Paths.get(codeDir)).forEach(entry -> {
fileList.add(((Path)entry).toString());
});
int len = 0;
byte[] buffer = new byte[1024];
for(String file : fileList ) {
//create JarEntry
JarEntry je = new JarEntry(file);
je.setComment("Creating Jar");
je.setTime(Calendar.getInstance().getTimeInMillis());
System.out.println(je);
jos.putNextEntry(je);
//write the bytes of file into jar
InputStream is = new BufferedInputStream(new FileInputStream(file));
while((len = is.read(buffer, 0, buffer.length)) != -1)
jos.write(buffer, 0, len);
is.close();
jos.closeEntry();
System.out.println("Done");
}
jos.close();
}
When I execute this no errors happen and I do get a JAR file called "Test.jar" generated in my Eclipse project folder. But when I open the JAR the .class files are not there, instead I see this:
I know that it's finding the .class files since the line System.out.println(je); successfully prints out the absolute path of each of them, so why aren't they getting put into the JAR?
It solved itself when I changed these two lines:
String jarPath = System.getProperty("user.dir") + "/Test.jar";
String codeDir = System.getProperty("user.dir") + "/bin/jartest/";
To these:
String jarPath = "Test.jar";
String codeDir = "bin/jartest/";
I can now see the .class files inside the JAR.
In my case I have to download images from the resources folder in my web app. Right now I am using the following code to download images through URL.
url = new URL(properties.getOesServerURL() + "//resources//WebFiles//images//" + imgPath);
filename = url.getFile();
is = url.openStream();
os = new FileOutputStream(sClientPhysicalPath + "//resources//WebFiles//images//" + imgPath);
b = new byte[2048];
while ((length = is.read(b)) != -1) {
os.write(b, 0, length);
}
But I want a single operation to read all images at once and create a zip file for this.
I don't know so much about the use of sequence input streams and zip input streams so if it is possible through these, please let me know.
The only way I can see you being able to do this is something like the following:
try {
ZipOutputStream zip = new ZipOutputStream(new FileOutputStream("C:/archive.zip"));
//GetImgURLs() is however you get your image URLs
for(URL imgURL : GetImgURLs()) {
is = imgURL.openStream();
zip.putNextEntry(new ZipEntry(imgURL.getFile()));
int length;
byte[] b = new byte[2048];
while((length = is.read(b)) > 0) {
zip.write(b, 0, length);
}
zip.closeEntry();
is.close();
}
zip.close();
}
Ref: ZipOutputStream Example
The url should return zip file. Else you have to take one by one and create a zip using your program
I currently have a function makeBackup() which zips an entire directory into a zip file, the files are however too big so we decided to switch to LZMA.
We found a library which does this (lzma-java) however it seems to compress only a single file, while the zip function we used permits to add files and directories to a zip file.
How can we implement the same with LZMA by changing our function? I added our current function below:
private static void makeBackup()
{
String backupPathString = "/home/backups";
/* zip remote file */
try
{
//name of zip file to create
String zipFilename = "backup.zip";
//create ZipOutputStream object
ZipOutputStream zipOutStream = new ZipOutputStream(new FileOutputStream(zipFilename));
//path to the currentFile to be zipped
File zipFolder = new File(backupPathString);
//get path prefix so that the zip file does not contain the whole path
// eg. if currentFile to be zipped is /home/lalit/test
// the zip file when opened will have test currentFile and not home/lalit/test currentFile
int len = zipFolder.getAbsolutePath().lastIndexOf(File.separator);
String baseName = zipFolder.getAbsolutePath().substring(0, len + 1) + File.separator + "todaybackups";
zipFilesInPath(zipOutStream, backupPathString, baseName);
zipOutStream.flush();
zipOutStream.close();
}
catch (IOException e)
{
}
}
private static void zipFilesInPath(ZipOutputStream zipOutputStream, String filePath, String baseName) throws IOException
{
File currentFile = new File(filePath);
ArrayList<File> filesArrayList = new ArrayList<File>(Arrays.asList(currentFile.listFiles()));
if (filesArrayList.isEmpty())
{
String name = currentFile.getAbsolutePath().substring(baseName.length());
ZipEntry zipEntry = new ZipEntry(name + "/" + ".");
zipOutputStream.putNextEntry(zipEntry);
}
for (File file : filesArrayList)
{
if (file.isDirectory())
{
zipFilesInPath(zipOutputStream, file.getAbsolutePath(), baseName);
}
else
{
String name = file.getAbsolutePath().substring(baseName.length());
ZipEntry zipEntry = new ZipEntry(name);
zipOutputStream.putNextEntry(zipEntry);
IOUtils.copy(new FileInputStream(file), zipOutputStream);
zipOutputStream.closeEntry();
}
}
}
private static void unzipFilesToPath(ZipInputStream zipInputStream, String fileExtractPath) throws IOException
{
ZipEntry entry;
while ((entry = zipInputStream.getNextEntry()) != null)
{
int count;
byte[] data = new byte[2048];
/*let's make the directory structure needed*/
File destFile = new File(fileExtractPath, entry.getName());
File destinationParent = destFile.getParentFile();
// create the parent directory structure if needed
destinationParent.mkdirs();
if (!entry.isDirectory() && !entry.getName().substring(entry.getName().length() - 1).equals("."))
{
final FileOutputStream fos = new FileOutputStream(fileExtractPath + File.separator + entry.getName());
final BufferedOutputStream dest = new BufferedOutputStream(fos, 2048);
while ((count = zipInputStream.read(data, 0, 2048)) != -1)
{
dest.write(data, 0, count);
}
dest.flush();
dest.close();
}
}
}
apparently is not possible, and what you have to do is package all your files into a tar/zip and then apply the lzma. take a look at this How to use LZMA SDK to compress/decompress in Java
Scenario: Uncompress a tar file using Apache commons.
Problem: The tar i am using is a build tar which gets deployed into a web server. This tar contains duplicate entries like below.
appender_class.xml
APPENDER_CLASS.xml
when extracting using the below code only appender_class.xml is extracted but i want both the files how can i do that ? Renaming in fly is fine but how can i accomplish that?
public static void untar(File[] files) throws Exception {
String path = files[0].toString();
File tarPath = new File(path);
TarEntry entry;
TarInputStream inputStream = null;
FileOutputStream outputStream = null;
try {
inputStream = new TarInputStream(new FileInputStream(tarPath));
while (null != (entry = inputStream.getNextEntry())) {
int bytesRead;
System.out.println("tarpath:" + tarPath.getName());
System.out.println("Entry:" + entry.getName());
String pathWithoutName = path.substring(0, path.indexOf(tarPath.getName()));
System.out.println("pathname:" + pathWithoutName);
if (entry.isDirectory()) {
File directory = new File(pathWithoutName + entry.getName());
directory.mkdir();
continue;
}
byte[] buffer = new byte[1024];
outputStream = new FileOutputStream(pathWithoutName + entry.getName());
while ((bytesRead = inputStream.read(buffer, 0, 1024)) > -1) {
outputStream.write(buffer, 0, bytesRead);
}
System.out.println("Extracted " + entry.getName());
}
}
Try opening your FileOutputstream like this instead:
File outputFile = new File(pathWithoutName + entry.getName());
for(int i = 2; outputFile.exists(); i++) {
outputFile = new File(pathWithoutName + entry.getName() + i);
}
outputStream = new FileOutputStream(outputFile);
It should generate a file called APPENDER_CLASS.xml2 if it encounters a previously created file called APPENDER_CLASS.xml. If a APPENDER_CLASS.xml2 exists it will create a APPENDER_CLASS.xml3, ad infinitum.
File.exists() takes case sensitivity into account (windows filenames are case insensitive, whereas unix, linux and mac are case sensitive). Thus with the above code on case insensitive filesystems the file would be renamed and on case sensitive filesystems the file would not be renamed.
I need to add a config file to an existing tar file. I am using apache.commons.compress library. The following code snippet adds the entry correctly but overwrites the existing entries of the tar file.
public static void injectFileToTar () throws IOException, ArchiveException {
String agentSourceFilePath = "C:\\Work\\tar.gz\\";
String fileToBeAdded = "activeSensor.cfg";
String unzippedFileName = "sample.tar";
File f2 = new File(agentSourceFilePath+unzippedFileName); // Refers to the .tar file
File f3 = new File(agentSourceFilePath+fileToBeAdded); // The new entry to be added to the .tar file
// Injecting an entry in the tar
OutputStream tarOut = new FileOutputStream(f2);
TarArchiveOutputStream aos = (TarArchiveOutputStream) new ArchiveStreamFactory().createArchiveOutputStream("tar", tarOut);
TarArchiveEntry entry = new TarArchiveEntry(fileToBeAdded);
entry.setMode(0100000);
entry.setSize(f3.length());
aos.putArchiveEntry(entry);
FileInputStream fis = new FileInputStream(f3);
IOUtils.copy(fis, aos);
fis.close();
aos.closeArchiveEntry();
aos.finish();
aos.close();
tarOut.close();
}
On checking the tar, only "activeSensor.cfg" file is found and the initial content of the tar is found missing. Is the "mode" not set correctly ?
The problem is that the TarArchiveOutputStream does not automatically read in the existing archive, which is something that you'd need to do. Something along the lines of:
CompressorStreamFactory csf = new CompressorStreamFactory();
ArchiveStreamFactory asf = new ArchiveStreamFactory();
String tarFilename = "test.tgz";
String toAddFilename = "activeSensor.cfg";
File toAddFile = new File(toAddFilename);
File tempFile = File.createTempFile("updateTar", "tgz");
File tarFile = new File(tarFilename);
FileInputStream fis = new FileInputStream(tarFile);
CompressorInputStream cis = csf.createCompressorInputStream(CompressorStreamFactory.GZIP, fis);
ArchiveInputStream ais = asf.createArchiveInputStream(ArchiveStreamFactory.TAR, cis);
FileOutputStream fos = new FileOutputStream(tempFile);
CompressorOutputStream cos = csf.createCompressorOutputStream(CompressorStreamFactory.GZIP, fos);
ArchiveOutputStream aos = asf.createArchiveOutputStream(ArchiveStreamFactory.TAR, cos);
// copy the existing entries
ArchiveEntry nextEntry;
while ((nextEntry = ais.getNextEntry()) != null) {
aos.putArchiveEntry(nextEntry);
IOUtils.copy(ais, aos, (int)nextEntry.getSize());
aos.closeArchiveEntry();
}
// create the new entry
TarArchiveEntry entry = new TarArchiveEntry(toAddFilename);
entry.setSize(toAddFile.length());
aos.putArchiveEntry(entry);
IOUtils.copy(new FileInputStream(toAddFile), aos, (int)toAddFile.length());
aos.closeArchiveEntry();
aos.finish();
ais.close();
aos.close();
// copies the new file over the old
tarFile.delete();
tempFile.renameTo(tarFile);
A couple of notes:
This code does not include any exception handling (please add the appropriate try-catch-finally blocks)
This code does not handle files with a size over 2147483647 (Integer.MAX_VALUE) as it only reads file sizes to integer precision bytes (see the cast to int). However, that's not a problem as Apache Compress does not handle files over 2 GB anyway.
Try changing
OutputStream tarOut = new FileOutputStream(f2);
to
OutputStream tarOut = new FileOutputStream(f2, true); //Set append to true