0

I wrote a script in Python to clean archives of jar (I know classes which I need with Class Dependency Analyzer) This is my script :

def deleteJarEntrie(pJarPath) :

    # Dictionary contains className of jar file for key 
    # and a list of class for value
    for key in myDict.keys():

        zin = zipfile.ZipFile (pJarPath+key, 'r')
        zout = zipfile.ZipFile (key, 'w')

        # for each entry in zip file
        for item in zin.infolist():

            # if the className of the file finished by one “/” it is a directory 
            # thus I do not reiterate values of the dictionary  
            if (item.filename[-1:] != "/"):

                # For each value of my dictionary (the value are the classes which I need)
                for value in myDict.get(key):
                    buffer = zin.read(item.filename)
                    className = item.filename.split("/")
                    className = className[len(className)-1]
                    if (item.filename == value):
                        zout.writestr(item, buffer)
                        print item.filename
        zout.close()
        zin.close()'

The problem is that item.filename never take class-file name for value, item.filename only take value of directory, but it can take value of MANIFEST.MF.

For example :

I've a class in /org/test/myClass.class

-> item.filename = /org/test/

I've a manifest in /META-INF/MANIFEST.MF

-> item.filename = /META-INF/MANIFEST.MF

I don't understant why I can't see class-file...

Thank you for your answers and sorry if my english is not correct.

MaxDhn
  • 153
  • 1
  • 7
  • 1
    What is myDict? Are you sure you have the right types? You might silently be iterating over a string. Or you might have forgotten the `.class` extension. – Antimony Jul 03 '13 at 02:46
  • Thanks for your answer. myDict is a dictionary, the key is a string and the value is a list of string. All files in the jar have '.class' extension. – MaxDhn Jul 03 '13 at 03:14
  • Can you print out an example of myDict? – Antimony Jul 03 '13 at 03:21
  • I change some things and now it's work, and the instruction `if (item.filename == value)` was not correct, I need to compare with `className`. But I've error for one jar : `buffer = zin.read(item.filename) File "/usr/lib/python2.7/zipfile.py", line 931, in read return self.open(name, "r", pwd).read() File "/usr/lib/python2.7/zipfile.py", line 967, in open raise BadZipfile("Bad magic number for file header") zipfile.BadZipfile: Bad magic number for file header` – MaxDhn Jul 03 '13 at 11:37

0 Answers0