Question

I am trying to do my own Minecraft Launcher for Moded Minecraft. And I have a issue: I need to extract content of one *.jar file to another. I tried a lot of things and I am totally desperate.

Basicly, I need to do this in code.

Was it helpful?

Solution

As Andrew briefly mentioned, there are some good .NET libraries for manipulating zip files. I've found DotNetZip to be very useful, and there are lots of helpful worked examples here.

In answer to your question, if you just want to copy the contents of one *.jar file to another, keeping original content in the target file, please try the following. I'm using the .NET zip mentioned above (which also has a NuGet package!):

using (ZipFile sourceZipFile = ZipFile.Read("zip1.jar"))
        using (ZipFile targetZipFile = ZipFile.Read("zip2.jar"))
        {
            foreach (var zipItem in sourceZipFile)
            {
                if (!targetZipFile.ContainsEntry(zipItem.FileName))
                {
                    using (Stream stream = new MemoryStream())
                    {
                        // Write the contents of this zip item to a stream
                        zipItem.Extract(stream);
                        stream.Position = 0;

                        // Now use the contents of this stream to write to the target file
                        targetZipFile.AddEntry(zipItem.FileName, stream);

                        // Save the target file. We need to do this each time before we close
                        // the stream
                        targetZipFile.Save();
                    }
                }
            }
        }

Hope this helps!

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top