Question

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?

Was it helpful?

Solution 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);

OTHER TIPS

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.

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.

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.

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.

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