本文介绍了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(路径,路径,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);

请参阅:

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

08-20 00:59