Hey guys, I'm not entirely new to java, but how it handles jar files puzzles me a bit. I want to be able to back up an already existing jar file (which I have working great), then detect and add all of the class files I have in my program's jar into the other jar, and finally delete a specific folder in the other jar. How may I do this?

(This is what I have so far):

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.zip.ZipEntry;
import java.util.zip.ZipInputStream;
import java.util.zip.ZipOutputStream;


public class Patcher 
{
	public static void main(String[] args)
	{
		File minecraftJar=new File(System.getenv("APPDATA")+"/.minecraft/bin/minecraft.jar");
		File backup=new File(System.getenv("APPDATA")+"/.minecraft/bin/minecraft.backup");
		File[] test={new File("config.txt")};
		try 
		{
			backup.createNewFile();
			copy(minecraftJar,backup);
			addFiles(minecraftJar,test);
		} 
		catch (IOException e) 
		{
			e.printStackTrace();
		}	
	}
	public static void copy(File src, File dst) throws IOException 
	{
	    InputStream in = new FileInputStream(src);
	    OutputStream out = new FileOutputStream(dst);
	    byte[] buf = new byte[1024];
	    int len;
	    while ((len = in.read(buf)) > 0) 
	    {
	        out.write(buf, 0, len);
	    }
	    in.close();
	    out.close();
	}
	public static void addFiles(File zipFile,File[] files) throws IOException 
	{
		File tempFile = File.createTempFile(zipFile.getName(), null);
		tempFile.delete();
		boolean renameOk=zipFile.renameTo(tempFile);
		if (!renameOk)
		{
			throw new RuntimeException("could not rename the file "+zipFile.getAbsolutePath()+" to "+tempFile.getAbsolutePath());
		}
		byte[] buf = new byte[1024];		
		ZipInputStream zin = new ZipInputStream(new FileInputStream(tempFile));
		ZipOutputStream out = new ZipOutputStream(new FileOutputStream(zipFile));		
		ZipEntry entry = zin.getNextEntry();
		while (entry != null) 
		{
			String name = entry.getName();
			boolean notInFiles = true;
			for (File f : files) 
			{
				if (f.getName().equals(name)) 
				{
					notInFiles = false;
					break;
				}
			}
			if (notInFiles) 
			{
				out.putNextEntry(new ZipEntry(name));
				int len;
				//The line below is where java gives me an EOFException
				while ((len = zin.read(buf)) > 0) 
				{
					out.write(buf, 0, len);
				}
			}
			entry = zin.getNextEntry();
		}
		zin.close();
		for (int i = 0; i < files.length; i++) 
		{
			InputStream in = new FileInputStream(files[i]);
			out.putNextEntry(new ZipEntry(files[i].getName()));
			int len;
			while ((len = in.read(buf)) > 0) 
			{
				out.write(buf, 0, len);
			}
			out.closeEntry();
			in.close();
		}
		out.close();
		tempFile.delete();
	}
}

Where are you in your project? What does the posted code do?
Does it give errors when executed?

So far, I've made the backup work. That's what the copy method does. I then looked up how to put files into a jar (Which it mostly just gave me tutorials on how to alter the jar from the command line), which is what the addFiles method is (but this doesn't work, it throws an EOFException at line 73, so I need to learn how to 1)Detect all of the files in my jar that I want to add 2)copy those files into the other jar and 2)Delete the folder in the other jar

So the "my jar" is the R/O source and the "other jar" is the one being changed.

I compiled and executed the code and got no exceptions.
I started with a jar file and a text file.
I end up with three files: a new jar file containing the old jar file's contents plus the text file, a backup of the old jar file and the text file.

Oh, I accidentally had a corrupted jar I was trying to insert the file into, thanks.

Now I need to know how to detect all of the files in my jar (besides the actual code) and delete a folder in a jar

Since you are copying all the files from one jar to the other, skip copying those you do not want to be in the output jar.

But how do I get the files in my jar?

Please explain.
Your code currently gets all the files in my jar.
Pseudo code

begin loop
  get my jar entry
  if (to skip this one) continue; // skip copying this entry to other jar
  write this entry to other jar
end loop

How would I get the jar though? I know how to find the other jar, but I don't know how to get the jar I'm using. Also, how do I tell if an entry is what I want to copy over? Is there a way to detect if it's in a folder?

How would I get the jar though

Your program has it hard coded.
Why not use the JFileChooser to "get" the name and path to the my jar.

I know how to find the other jar

Now this is confusing. Earlier I asked about the jars being used.
There were two, my jar and the other jar.
The other jar was created by copying from my jar and adding a file.
Is there now a third jar?

Also, how do I tell if an entry is what I want to copy over

This is really a hard one to answer. What determines what files you want to copy?
That is an question I can not answer. You have to provide that.

Is there a way to detect if it's in a folder?

Where is this folder? In the my jar?
You need to read the API doc for the zip file classes to see what information is available about the contents of a zip file

Okay, thanks. That makes it easy =D

Now I just need to know how to delete a folder from the other jar (The jar I'm copying files into)

Why copy a file if you are going to delete it?
Don't copy it.

No, the jar already has files/folders inside of it, I'm making a patcher, so I want to be able to delete one of the folders already in it.

I understood that there are two jar files being being used. The 'my' was R/O, the 'other' jar was a copy of the 'my' jar. When you copy from the 'my' jar to the 'other'jar do NOT copy the files you do not want in the 'other' jar.

If you don't copy the files, you will not have to delete them.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.