Is there a way in Java to preserve Created and Accessed attributes of a file when are added to a zip archive?

1.7k views Asked by At

I could handle the Modified (lastModified) attribute, meaning in the archive I could preserve the Modified attribute of the file.
Here is a sample:

ZipOutputStream out = new ZipOutputStream(new BufferedOutputStream( new FileOutputStream(outFilename)));
File f = new File(filename);
long longLastMod = f.lastModified();
FileInputStream in = new FileInputStream(filename);
// Add ZIP entry to output stream.
ZipEntry ze = new ZipEntry(filename);
ze.setTime(longLastMod);  // the "magic" to store the Modified date/time of the file
out.putNextEntry( ze );
// Transfer bytes from the file to the ZIP file
int len;
while ((len = in.read(buf)) > 0) {
    out.write(buf, 0, len);
}
out.closeEntry();
in.close();
out.close();

Now, the output Zip file will preserve the Modified attribute but it will not preserve the Created or Accessed attributes. Is there a way to accomplish this?

1

There are 1 answers

1
bestsss On BEST ANSWER

Is there a way to accomplish this?

No it's not possible. To put it simply: the zip directory doesn't support the attributes.

What you can do, however, is using setExtra(byte[]) and store whatever information you need there. Unfortunately, you'd need a custom extractor to preserve the attributes.

Hope this helps!