Java 有没有办法在将文件添加到 zip 存档时保留“创建”和“访问”属性?
我可以处理 Modified (lastModified) 属性,这意味着在存档中我可以保留文件的 Modified 属性。
下面是一个示例:
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();
现在,输出 Zip 文件将保留 Modified 属性,但不会保留 Created 或 Accessed 属性。 有办法做到这一点吗?
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?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
不,这是不可能的。简单来说:zip目录不支持属性。
但是,您可以使用
setExtra(byte[])
并在那里存储您需要的任何信息。不幸的是,您需要一个自定义提取器来保留属性。希望这有帮助!
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!