使用Zip4J API添加具有指定文件权限的流数据的方法如下:

huangapple go评论74阅读模式
英文:

How do you use the Zip4J API to add streamed data with specified file permissions

问题

Zip4J API提供了一种方便的方法来向zip文件添加流式条目:

ZipFile.addStream(InputStream stream, ZipParameters pars)

似乎没有一种方法可以在ZipFile或ZipParameter类的实例上指定“文件权限”或“默认文件权限”。

默认行为是在条目上将所有文件属性设置为false,这在Unix系统上意味着所有者、组和其他用户都没有读取、写入或执行权限。这很不方便。我希望至少为所有者设置读权限标志。

  1. 是否有一种方法可以在“流式”zip文件条目上设置文件权限(即使用ZipFile.addStream方法添加的条目)?

  2. 如果没有(1),是否有一种方法可以在创建条目之后添加文件权限(实际上存储在底层磁盘上的zip文件中 - 有关此注意事项,请参见附加信息)?

附加信息

请注意,一旦将流式条目添加到Zip文件中,可以从其标头数据中获取并设置文件属性信息,可以使用ZipFile.getHeader(entryName)方法获取标头数据。然而,使用此API设置文件权限值不会直接影响底层zip文件。此外,似乎没有办法将更新后的标头信息保存到磁盘上(尽管我可能遗漏了某些内容)。

供参考,获取和设置文件属性的方法如下:

byte[] FileHeader.getInternalFileAttributes()
void FileHeader.setInternalFileAttributes(byte[] attributes)
byte[] FileHeader.getExternalFileAttributes()
void FileHeader.setExternalFileAttributes(byte[] attributes)

深入研究zip4j代码表明,这些文件属性存储在一个4字节的数组中,其中字节2和3(从字节0开始)表示Unix文件权限位。这可以在net.lingala.zip4j.util.FileUtils类的apply posix文件属性中找到。

潜在的解决方法(我试图避免的)

我能看到的一个解决方法是将流中的数据写入临时文件,确保文件具有所需的权限,然后将文件添加到zip存档中,最后删除临时文件(因为它已经完成了其目的)。这种方法假设磁盘上的文件权限得到了正确维护,这在“posix系统”上似乎是情况如此。

我更愿意避免使用这种方法。

英文:

The Zip4J API provides a convenient method for adding a streamed entry to a zip file:

ZipFile.addStream(InputStream stream, ZipParameters pars)

There does not appear to be a method of specifying the 'file permissions' or 'default file permissions' on instances of either the ZipFile or the ZipParameter classes.

The default behaviour is to have all file properties set to false on the entry, which on a unix system, means that there is no read, write, or execute permissions for owner, group, and other. This is inconvenient. I would like to set at least the read permission flag for the owner.

  1. Is there a means for setting the file permissions on a 'streamed' zip file entry (ie one added using the ZipFile.addStream method?

  2. If not (1) is there a means for adding the file permission after the entry has been created (which is actually stored in the underpinning zip-file on disk - see additional information for this caveat)?

Additional Information

Note, once a stream entry has been added to a Zip file it is possible to get and set the file property information from its header data, which can be obtained using the ZipFile.getHeader(entryName) method. However, setting the file permission values using this API does not affect the underpinning zip file directly. Further, there appears to be no way of saving the updated header information to disk (though I might be missing something).

For reference the methods for getting and setting file attributes are:

byte[] FileHeader.getInternalFileAttributes()
void   FileHeader.setInternalFileAttributes(byte[] attributes)
byte[] FileHeader.getExternalFileAttributes()
void   FileHeader.setExternalFileAttributes(byte[] attributes)

Digging into the zip4j code, indicates that these file attributes are stored in a 4-byte array, where the bits in bytes 2 and 3 (starting from byte 0) represent the unix file permission bits. This is found in the net.lingala.zip4j.util.FileUtils class's apply posix file attributes.

Potential Workaround (which I am trying to avoid)

One workaround that I can see is writing the data from the stream to a temporary file, ensuring that file has the wanted permissions, adding the file to the zip archive, and then removing the temporary file (as it has served its purpose). This approach assumes that the on disk file permissions are correctly maintained, which appears to be the case, when on a 'posix system'.

I would prefer not to use this approach.

答案1

得分: 0

我遇到了相同的错误,我使用的是版本2.6.1,然后我发现了这个问题:
Unix权限混乱
该问题已在版本2.6.2及更高版本中修复。
在Linux系统上运行时只需添加标准权限,不允许用户更改权限。
请检查是否对您有用。

英文:

I had this same error, I was using version 2.6.1 and then I found this issue:
Unix permissions are messed up
It was fixed for version 2.6.2 and up.
Just adding standard permissions when running in a Linux Box, not letting you as a user to change those.
Check if that version is useful for you.

huangapple
  • 本文由 发表于 2020年8月10日 19:59:51
  • 转载请务必保留本文链接:https://go.coder-hub.com/63339756.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定