Click here to Skip to main content
15,884,237 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
Hello I'm trying to add the files from a .zip to a .jar

Here is my code:

C#
namespace ConsoleApplication2
{
    class Program
    {
        static void Main(string[] args)
        {
            string appdata = System.Environment.GetFolderPath(System.Environment.SpecialFolder.ApplicationData);
            string minecraft = System.IO.Path.Combine(appdata, ".minecraft");
            string mc = System.IO.Path.Combine(bin, "minecraft.jar");


        string destinationFile = System.IO.Path.Combine(AppDomain.CurrentDomain.BaseDirectory, "toJar.zip");

        using (ZipFile zip = ZipFile.Read(destinationFile))

        zip.ExtractAll(mc, ExtractExistingFileAction.OverwriteSilently);
        }
    }
}


The problem is that when I execute the project is says "Can not create "C:\ Users\Rafael\ AppData\Roaming\.minecraft\bin\minecraft.jar" because there is a file or directory with the same name."

I'm using Ionic Zip

Thanks
Posted
Updated 15-Jul-13 9:53am
v2
Comments
[no name] 15-Jul-13 15:45pm    
Okay.... how is this a "problem"? If you want to replace an existing file the check for it already existing and delete it first. Or if you do not then check for it existing and then not trying to write over it.
Sergey Alexandrovich Kryukov 15-Jul-13 15:51pm    
Of course, use some other name... :-)
—SA

1 solution

Jar is an archive format sort of like winzip. It is not a physical folder you can just copy files to. You need to extract the files from your zip archive and somehow programmatically add them to your jar archive file.

Here's a few links that might be helpful:

http://stackoverflow.com/questions/14465230/extract-files-from-a-zip-into-a-jar-archive[^]

Opening Jars with C#[^]

http://www.dzone.com/snippets/adding-files-existing-jar-file[^]

Good luck!
 
Share this answer
 
Comments
H.Brydon 15-Jul-13 16:37pm    
In fact, a jar file is really a zip file internally. If you rename xx.jar to xx.zip, you can open it with any zip processing software (and renaming is arguably optional).
Johnny J. 15-Jul-13 16:44pm    
I noticed that after I posted my reply. The links still apply, though. Thank you for the clarification.
pasztorpisti 16-Jul-13 8:53am    
Another interesting fact is that even signed jar files can be extracted and rezipped as the signature uses the hash of the unzipped file contents so the signature remains valid. You can rezip a jar for example with higher compression ratio.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900