Java:以编程方式创建 zip 时缺少“A"存档属性 [英] Java: `A` Archive attribute missing while creating zip programmatically

查看:41
本文介绍了Java:以编程方式创建 zip 时缺少“A"存档属性的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我们正在处理使用属性来检查 zip 中是否存在目录/文件的解压库/实用程序.

We are dealing with the decompression libraries/utility that uses attribute to check for the presence of directories/files within the zip.

问题是我们无法在创建时为 zip 设置存档位.当我们以编程方式创建 zip 时,它也会清除以前的属性.

Problem is that we are not able to set archive bit for a zip while creation. When we create zip programmatically, it wash out previous attributes as well.

我们将尝试使用以下提到的步骤设置存档位,但到目前为止还没有得到预期的结果:

We will try to set archive bit with below mentioned steps but not getting desired result so far:

1. Parse each zip entry and getExtra byte[].
2. Use Int value=32 and perform bitwise 'OR' operation.
3. setExtra byte[] after 'OR' operation.

添加更多细节:我们尝试了以下方法,但仍然无法解决此问题.

Adding some more details: We tried following approaches but still this issue is unresolved.

  1. 在文件系统上使用 setAttribute() 方法,但在创建 zip 时获取属性会被重置.Files.setAttribute(file, dos:archive", true)

  1. Using setAttribute() method on File system but getting the attributes are getting reset while creating zip. Files.setAttribute(file, "dos:archive", true)

使用 File.copy() 将与文件关联的文件属性复制到目标文件,但没有成功.甚至现有属性也不会保留到目标文件中.Files.copy(path, path, StandardCopyOption.COPY_ATTRIBUTES)

Using File.copy() which copies the file attributes associated with the file to the target file but no success. Even existing attributes are not being retained to target file. Files.copy(path, path, StandardCopyOption.COPY_ATTRIBUTES)

使用 ZipEntry.setExtra(byte[]).在网上找到了一些信息,java 没有任何直接的方法来设置属性,但根据一些在线文章,我们发现额外的字段用于设置 unix 和 MS DOS 文件属性的文件权限.这是一个未记录的领域,我们在网上找不到任何可靠的信息.基本上,前 2 个字节用于 unix,后 2 个字节用于 DOS 文件属性.我们尝试在其中设置具有不同值的 DOS 文件属性.ZipEntry.setExtra(byte[]) - 为条目设置可选的额外字段数据.

Using ZipEntry.setExtra(byte[]). found some info online that the java doesn’t have any direct method to set attributes but as per some online articles we found that the extra field is used to set the file permissions on unix and MS DOS file attributes. This is an undocumented field and we didn’t find any reliable information online. Basically, initial 2 bytes are used for unix and last 2 bytes are used for DOS file attributes. We tried setting DOS file attributes with different values in it. ZipEntry.setExtra(byte[]) - Sets the optional extra field data for the entry.

使用 winzip 命令行工具,但不是一个优雅的解决方案.

Using winzip command line tool but not an elegant solution.

推荐答案

我假设是 DOS (Windows)

I assume it is DOS (Windows)

使用 Java 7

import java.nio.file.Files;
import java.nio.file.Path;

File theFile = new File("yourfile.zip");
Path file = theFile.toPath();
Files.setAttribute(file, "dos:archive", true);

参见:http://kodejava.org/how-do-i-set-the-value-of-file-attributes/

这篇关于Java:以编程方式创建 zip 时缺少“A"存档属性的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆