This discussion is archived
11 Replies Latest reply: Oct 9, 2010 2:41 PM by 796440 RSS

Access lastModified property of file in external JAR archive

803815 Newbie
Currently Being Moderated
How can I approach this problem? Need to access the lastModified property of a file located inside external JAR archive.
Thanks ahead.
  • 1. Re: Access lastModified property of file in external JAR archive
    796440 Guru
    Currently Being Moderated
    Since jar files use zip format, [url http://download-llnw.oracle.com/javase/6/docs/api/java/util/zip/ZipEntry.html#getTime%28%29]ZipEntry.getTime() should give you what you want.
  • 2. Re: Access lastModified property of file in external JAR archive
    803815 Newbie
    Currently Being Moderated
    Thanks! But how do we find out the pass of the file with zipEntry?
  • 4. Re: Access lastModified property of file in external JAR archive
    803815 Newbie
    Currently Being Moderated
    *file's path
    Sorry
  • 5. Re: Access lastModified property of file in external JAR archive
    796440 Guru
    Currently Being Moderated
    It's still not clear what you want.

    The path of the entry within the zip archive? I exepct getName() will do that.

    The path of the original file that was put into the zip? You can't get it.

    The path to the zip file? You can't get that either, unless you hold onto it when you create the ZipFile object.
  • 6. Re: Access lastModified property of file in external JAR archive
    803815 Newbie
    Currently Being Moderated
    Of course the idea was to the path of the entry within the zip archive,
    however I'm having problems with getTime(): for 10+ files inside one jar archive, I have two time stamps: 1286637032000 and 1286637030000.
    While it's clear that one of them supposed to be the time stamp for the first buffer when the JAR was created and the second time stamp for the second buffer,
    when I check the lastModefied field with URLConnection, I get the first time stamp:

    URL url = ClassLoader.getSystemResource(PATH);
    URLConnection ul = url.openConnection();
    ul.getLastModified()

    returns 1286637030000, while getTime() that was mentioned here returns 1286637032000.

    The question is why? Or how do I get getLastModified's time stamp from outside the JAR archive?

    Thanks.

    Edited by: 800812 on Oct 9, 2010 9:20 AM
    EDIT: URLConnection's getLAstModefied returns the value that is inside the header of the file , getTime returns the modification time of the entry.
    I still don't know how to access the getLAstModefied of the file.
  • 7. Re: Access lastModified property of file in external JAR archive
    796440 Guru
    Currently Being Moderated
    I really don't understand what you're saying or what problem you're having. ZipEntry.getTime() works just fine for me:

    Here we see me touching a file at 10:37, creating a jar file that holds it at 10:41, and then executing code that properly reflects those times.
    :; touch z
    
    :; ll z
    -rw-rw-r-- 1 jeff.verdegan mkgroup-l-d 0 Oct  9 10:37 z
    
    :; jar cf z.jar z
    
    :; ll z.jar
    -rw-rw-r-- 1 jeff.verdegan mkgroup-l-d 356 Oct  9 10:41 z.jar
    
    :; jar tvf z.jar 
         0 Sat Oct 09 10:41:22 PDT 2010 META-INF/
        44 Sat Oct 09 10:41:22 PDT 2010 META-INF/MANIFEST.MF
         0 Sat Oct 09 10:37:06 PDT 2010 z
    
    :; java -cp C:/dev/scratch/output/classes scratch.JarZip
    C:\cygwin\tmp\z.jar last modified date is 1,286,646,082,478, Sat Oct 09 10:41:22 PDT 2010
    z last modified date is 1,286,645,826,000, Sat Oct 09 10:37:06 PDT 2010
    
    
    
    
    package scratch;
    
    import java.util.jar.JarFile;
    import java.util.Date;
    import java.util.zip.ZipEntry;
    import java.io.File;
    
    public class JarZip {
      public static void main (String[] args) throws Exception {
        File jarFileOnDisk = new File ("C:/cygwin/tmp/z.jar");
        long jfodLastModifiedLong = jarFileOnDisk.lastModified ();
        Date jfodLasModifiedDate = new Date(jfodLastModifiedLong);
    
        JarFile jf = new JarFile (jarFileOnDisk);
    
    
        System.out.printf ("%s last modified date is %,d, %s%n",
                           jarFileOnDisk,
                           jfodLastModifiedLong,
                           jfodLasModifiedDate);
    
        ZipEntry ze = jf.getEntry ("z");
        long zeTimeLong = ze.getTime ();
        Date zeTimeDate = new Date(zeTimeLong);
    
    
        System.out.printf ("%s last modified date is %,d, %s%n",
                           ze,
                           zeTimeLong,
                           zeTimeDate);
    
    
      }
    }
  • 8. Re: Access lastModified property of file in external JAR archive
    803815 Newbie
    Currently Being Moderated
    My problem is to get the time from within the JAR archive and from outside the JAR archive for the same file (that is inside that JAR). I receive different time stamps as can be seen, and that is my problem.
    Your code shows the formated output, but I need the time up to the last ms.

    Thanks again.
  • 9. Re: Access lastModified property of file in external JAR archive
    796440 Guru
    Currently Being Moderated
    800812 wrote:
    My problem is to get the time from within the JAR archive and from outside the JAR archive for the same file (that is inside that JAR). I receive different time stamps as can be seen, and that is my problem.
    Your code shows the formated output, but I need the time up to the last ms.
    Zip file format specifies a resolution of 2 seconds. You can't get millisecond precision from a zip or jar file. It is specified not to be there. This is not a Java issue.
  • 10. Re: Access lastModified property of file in external JAR archive
    803815 Newbie
    Currently Being Moderated
    Thanks a lot!
  • 11. Re: Access lastModified property of file in external JAR archive
    796440 Guru
    Currently Being Moderated
    What you can do is figure out the zip file's rounding rules and round the millisecond timestamp you get for the out-of-jar file the same way.

Legend

  • Correct Answers - 10 points
  • Helpful Answers - 5 points