0

I have been trying to find the best way to do this I have thought of extracting the contents of the .jar then moving the files into the directory then putting it back as a jar. Im not sure is the best solution or how I will do it. I have looked at DotNetZip & SharpZipLib but don't know what one to use.

If anyone can give me a link to the code on how to do this it would be appreciated.

Filburt
  • 17,626
  • 12
  • 64
  • 115
  • Do you mean that your _.zip_ file is really a _.jar_ archive? Then exchanging the file extension to _.jar_ should be enough. – Anders Gustafsson Aug 02 '12 at 14:19
  • Well basically the VB.NET program that downloads a .ZIP which contains files that need to go into the .jar file on users pc. So I basically need a way to extract the .ZIP and put contents into the .JAR – user1378127 Aug 02 '12 at 14:28

2 Answers2

0

For DotNetZip you can find very simple VB.NET examples of both creating a zip archive and extracting a zip archive into a directory here. Just remember to save the compressed file with extension .jar .

For SharpZipLib there are somewhat more comprehensive examples of archive creation and extraction here.

If none of these libraries manage to extract the full JAR archive, you could also consider accessing a more full-fledged compression software such as 7-zip, either starting it as a separate process using Process.Start or using its COM interface to access the relevant methods in the 7za.dll. More information on COM usage can be found here.

Anders Gustafsson
  • 15,837
  • 8
  • 56
  • 114
  • DotNetZip does let me extract zip contents to a folder but I cant seem to get it extract from a .jar just comes up with an error. – user1378127 Aug 02 '12 at 17:10
  • Change the file extension of the _.jar_ to _.zip_ before you extract it, that should do the trick. – Anders Gustafsson Aug 02 '12 at 17:30
  • I have tried it quite a bit now and I can get it to extract some of the .jar but it keeps giving me the error "FileStream will not open Win32 devices such as disk partitions and tape drives. Avoid use of "\\.\" in the path." which I cannot seem to fix. – user1378127 Aug 03 '12 at 08:16
  • @user1378127 I have added additional info. I recommend that you first try out _SharpZipLib_ to see if it handles the JAR contents better. If this does not work, download 7-Zip and verify that its' GUI manages to open your JAR files correctly, then try to incorporate access to the _7za.exe_ or _7za.dll_ from your code. – Anders Gustafsson Aug 03 '12 at 08:41
0

I think you are working with Minecraft 1.3.1 no? If you are, there is a file contained in the zip called aux.class, which unfortunately is a reserved filename in windows. I've been trying to automate the process of modding, while manipulating the jar file myself, and have had little success. The only option I have yet to explore is find a way to extract the contents of the jar file to a temporary location, while watching for that exception. When it occurs, rename the file to a temp name, extract and move on. Then while recreating the zip file, give the file the original name in the archive. From my own experience, SharZipLib doesnt do what you need it do nicely, or at least I couldnt figure out how. I suggest using Ionic Zip (Dot Net Zip) instead, and trying the rename route on the offending files. In addition, I also posted a question about this. You can see how far I got at Extract zip entries to another Zip file

Edit - I tested out .net zip more (available from http://dotnetzip.codeplex.com/), and heres what you need. I imagine it will work with any zip file that contains reserved file names. I know its in C#, but hey cant do all the work for ya :P

    public static void CopyToZip(string inArchive, string outArchive, string tempPath)
    {
        ZipFile inZip = null;
        ZipFile outZip = null;

        try
        {
            inZip = new ZipFile(inArchive);
            outZip = new ZipFile(outArchive);
            List<string> tempNames = new List<string>();
            List<string> originalNames = new List<string>();
            int I = 0;
            foreach (ZipEntry entry in inZip)
            {
                if (!entry.IsDirectory)
                {
                    string tempName = Path.Combine(tempPath, "tmp.tmp");
                    string oldName = entry.FileName;
                    byte[] buffer = new byte[4026];
                    Stream inStream = null;
                    FileStream stream = null;
                    try
                    {
                        inStream = entry.OpenReader();
                        stream = new FileStream(tempName, FileMode.Create, FileAccess.ReadWrite);
                        int size = 0;
                        while ((size = inStream.Read(buffer, 0, buffer.Length)) > 0)
                        {
                            stream.Write(buffer, 0, size);
                        }
                        inStream.Close();
                        stream.Flush();
                        stream.Close();
                        inStream = new FileStream(tempName, FileMode.Open, FileAccess.Read);

                        outZip.AddEntry(oldName, inStream);
                        outZip.Save();
                    }
                    catch (Exception exe)
                    {
                        throw exe;
                    }
                    finally
                    {
                        try { inStream.Close(); }
                        catch (Exception ignore) { }
                        try { stream.Close(); }
                        catch (Exception ignore) { }
                    }
                }
            }

        }
        catch (Exception e)
        {
            throw e;
        }
    }
Community
  • 1
  • 1
Mark W
  • 2,791
  • 1
  • 21
  • 44