Jar/zip files are not editable. You can't delete an entry from a jar/zip file.
What you can do is "re-create" the jar file like this: start a new jar file, iterate over the entries of the current jar file, and add those entries to the new jar file which you do not want to delete.
Theoretically it would be possible to delete an entry like this (but the standard Java lib is not suitable for this, meaning the ZipFile
, ZipInputStream
, JarFile
, JarInputStream
classes):
Entries in a jar/zip file are sequencial. Each entry has a header with info about the entry (which may be a file or folder entry). This header also contains the byte-length of the entry. So you could iterate over the entries sequencially, and if you encounter an entry which you want to delete (and you know its size from its header), you could copy the remaining of the file (content after this entry) to the begining of the current entry (obviously the file size should be trunced by the length of the current / deleted entry).
Or your other options include not doing this via Java but using an external tool like the zip -d
command itself.