Delete Folder From .Jar While Treating as a Zip

只谈情不闲聊 提交于 2020-01-16 00:51:17

问题


I need to delete a folder from a .jar file, but treating it as a .zip file with sharpziplib. Is there any way to do so?

I have tried before to "unzip" the .jar file using sharpziplib and then delete it and rezip it, but to no avail- not to mention it is much slower to do it that way.

How can I directly delete the folder?


回答1:


A JAR is similar to a ZIP file. A JAR is a Java ARchive. Just like you can't directly delete a file/folder without unpacking a ZIP file, you have to unpack a JAR file to access it.




回答2:


I am not familiar with SharpZibLib, but I have used DotNetZip before where you can extract the zip file to a specific folder and delete the file/folder and then rezip the contents in the new location.

Yes you need to completely extract it if you want to delete and re zip it again.




回答3:


I suggest to do this if, and only if, you really know what you are doing, you really need this, you know what will be the destiny of those jars.

As pointed out from other users a jar is a really similar file format to a zip file or a generic compressed archive, the main differences are a security mechanism and the presence of a manifest, the thing is that a jar doesn't really need this last 2 pieces to exist or to be accepted as a jar, but if you just delete what you want to delete from the jar you can break all the hashing and the manifest ( if it's a jar with a manifest ) and in the end your jar will probably be useless.




回答4:


Well using DotNetZip (it's also available on nuget) you can do something like this (or at least it should give you the idea):

// define your paths accordingly
string unpackDir = "C:\\unpackDir";
string originalJarFilePath = "C:\\...\\your_file.jar";
string destinationJarFilePath = "C:\\...\\shiny_new_archive.jar";

// unpack
using (var jar = ZipFile.Read(originalJarFilePath))
{
    if (!Directory.Exists(unpackDir))
    {
        Directory.CreateDirectory(unpackDir);
    }
    jar.ExtractAll(unpackDir, ExtractExistingFileAction.OverwriteSilently);
}

// make a new jar excluding the files you don't want
using (var editedJar = new ZipFile())
{
    foreach (string file in Directory.GetFiles(unpackDir).Where(file => file != "IDontWantThisOne.txt"))
    {
        editedJar.AddFile(file);
    }
    editedJar.Save(destinationJarFilePath);
}

I hope this helps you.




回答5:


Huh, contrary to popular belief, I didn't need to unzip the .jar file. I only needed to do this(Using dotnetzip btw)-

//Code to delete META-INF folder
using (ZipFile zip = ZipFile.Read(jarFile))
{
    List<string> files = zip.EntryFileNames.ToList<string>();
    List<string> delete = new List<string>();
    for (int i = 0; i < files.Count; i++)
    {
        if (files[i].Contains("META-INF"))
        {
            delete.Add(files[i]);
        }
    }
zip.RemoveEntries(delete);
zip.Save();
}
MessageBox.Show("Success!", "Success", MessageBoxButtons.OK, MessageBoxIcon.None);


来源:https://stackoverflow.com/questions/17904973/delete-folder-from-jar-while-treating-as-a-zip

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!