Java - 从jar中删除META-INF无法正常工作 [英] Java - Removal of META-INF from jar not working

查看:422
本文介绍了Java - 从jar中删除META-INF无法正常工作的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在开发一个将mods安装到Minecraft中的应用程序,我几乎完成了版本3.1DEV,唯一让我停止的是我的代码只是不会删除META-INF,这是我的代码

I'm currently working on a application that installs mods into Minecraft and I have almost finished version 3.1DEV, the only thing that is stoping me is that my code just won't remove the META-INF, here is my code

        ZipInputStream modZip = new ZipInputStream(new FileInputStream(mod.getDir()));
        ZipInputStream minecraftZip = new ZipInputStream(new FileInputStream(new File(mcDir + "\\bin\\", "minecraft.jar")));
        ZipOutputStream tmpZip = new ZipOutputStream(new FileOutputStream(new File("temp\\tmp.jar")));
        byte[] buffer = new byte[1024];

        for(ZipEntry ze = modZip.getNextEntry(); ze != null; ze = modZip.getNextEntry())
        {
            tmpZip.putNextEntry(ze);
            for(int read = modZip.read(buffer); read != -1; read = modZip.read(buffer))
            {
                tmpZip.write(buffer, 0, read);
            }
            tmpZip.closeEntry();
        }
        modZip.close();


        for(ZipEntry ze = minecraftZip.getNextEntry(); ze != null; ze = minecraftZip.getNextEntry())
        {
            try
            {
                boolean isMetaInf = false;

                if(ze.getName().contains("META-INF"))
                {
                    isMetaInf = true;
                }

                if(!isMetaInf)
                {
                    tmpZip.putNextEntry(ze);
                    for(int read = minecraftZip.read(buffer); read != -1; read = minecraftZip.read(buffer))
                    {
                        tmpZip.write(buffer, 0, read);
                    }
                    tmpZip.closeEntry();
                }
            }
            catch(Exception e)
            {
                continue;
            }
        }
        minecraftZip.close();

        tmpZip.flush();
        tmpZip.close();

        File tmp = new File("temp//tmp.jar");
        tmp.renameTo(new File("temp//minecraft.jar"));
        File minecraft = new File(mcDir + "\\bin\\minecraft.jar");
        minecraft.delete();
        FileUtils.copyFile(new File("temp\\minecraft.jar"), minecraft);
        tmp.delete();

欢迎任何链接或示例


  • Liam,Hachi Software CEO

推荐答案

问题出在逻辑,让我们来看看:

The problem is with the logic, let's walk through it:


  1. 第一次找到META-INF文件夹时,将标志设置为
    true 。循环中没有其他内容可以发生,所以我们继续

  2. 在下一个循环迭代中,将标志重置为false并进入
    !isMetaInf 部分

  3. zip条目已添加到临时zip:

  1. The first time you find the META-INF folder, you set the flag to true. Nothing else in the loop can occur, so we continue
  2. On the next loop iteration, you reset the flag to false and go into the !isMetaInf section
  3. The zip entry is added to the temporary zip:

tmpZip.putNextEntry(ze);

你写的整个minecraft zip ,来自开始完成临时zip:

You write the entire minecraft zip, from start to finish into the temp zip:

for(int read = minecraftZip.read(buffer); read != -1; read = minecraftZip.read(buffer))
{
    tmpZip.write(buffer, 0, read);
}
tmpZip.closeEntry();


  • 此时你没有突破循环,所以重复这个过程对于jar中的每个文件。

  • At this point you don't break out of the loop, so this process is repeated for every file inside the jar.

    如果您只是删除手动读写循环,则可以允许ZipOutputStream在最后调用 close()时为你做所有的写作,或者如果你使用Java 7,你可以通过try-with-resources使这段代码变得非常简单:

    If you simply remove the manual reading and writing loop, you can allow the ZipOutputStream to do all the writing for you when you call close() at the end, or if you use Java 7 you can make this code very simple with a try-with-resources:

    public static void copyWithoutMetaInf(final String originalZip, final String newZip) throws IOException
    {
        try (final ZipInputStream zip = new ZipInputStream(new FileInputStream(originalZip));
             final ZipOutputStream zop = new ZipOutputStream(new FileOutputStream(newZip)))
        {
            ZipEntry entry;
            while((entry = zip.getNextEntry()) != null)
            {
                if(!entry.getName().contains("META-INF"))
                {
                    zop.putNextEntry(entry);
                }
            }
        }
    }
    
    public static void main(String[] args) throws IOException
    {
        copyWithoutMetaInf("1.6.4.jar", "copy.jar");
    }
    

    或者没有旧版本:

    public static void copyWithoutMetaInf(final String originalZip, final String newZip) throws IOException
    {
        final ZipInputStream zip = new ZipInputStream(new FileInputStream(originalZip));
        final ZipOutputStream zop = new ZipOutputStream(new FileOutputStream(newZip));
        ZipEntry entry;
        while((entry = zip.getNextEntry()) != null)
        {
            if(!entry.getName().contains("META-INF"))
            {
                zop.putNextEntry(entry);
            }
        }
        zip.close();
        zop.close();
    }
    

    这篇关于Java - 从jar中删除META-INF无法正常工作的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

  • 查看全文
    登录 关闭
    扫码关注1秒登录
    发送“验证码”获取 | 15天全站免登陆