Java 有没有办法在将文件添加到 zip 存档时保留“创建”和“访问”属性?

发布于 2024-10-19 11:59:34 字数 757 浏览 1 评论 0原文

我可以处理 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

三生一梦 2024-10-26 11:59:34

有办法实现这一点吗?

不,这是不可能的。简单来说:zip目录不支持属性。

但是,您可以使用 setExtra(byte[]) 并在那里存储您需要的任何信息。不幸的是,您需要一个自定义提取器来保留属性。

希望这有帮助!

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!

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文