Java NiO zip filesystem is equivalent to Java util. zip. Setmethod() in zipentry

I have some existing code to create a zip file in ePub 2 format, which works properly

When trying to update my code to support ePub 3 format, I think I will try the Java NiO zip file system instead of Java util. zip. ZipFile. I'm almost finished except for a small item

EPub format requires a 20 byte mimeType file, which must be put into zip in uncompressed form java. util. zip. The zipentry API provides setmethod (zipentry. Stored) to achieve this

I can't find any references to this in the Java NiO filesystem API documentation Is there a zipentry The equivalent of setmethod()?

Edit 1

OK, so I saw how to display attributes and thank you for your example, but I can't find any documentation on how to create attributes, such as (zip: method, 0), even on Oracle's own oracle In my opinion, NiO enhancements in Java 7 are only about 20% recorded API doc attributes are very sparse, especially how to create attributes

I began to get the feeling that the NiO zip file system may not be for Java util. Zip, and more code is needed to achieve the same result

Edit 2

I tried the following methods:

String contents = "application/epub+zip"; /* contents of mimetype file */
Map<String,String> map = new HashMap<>();
map.put("create","true");

Path zipPath = Paths.get("zipfstest.zip");
Files.deleteIfExists(zipPath);

URI fileUri = zipPath.toUri(); // here
URI zipUri = new URI("jar:" + fileUri.getScheme(),fileUri.getPath(),null);

try (FileSystem zipfs = FileSystems.newFileSystem(zipUri,map)) {
    Path pathInZip = zipfs.getPath("mimetype");
    Files.createFile(pathInZip,new ZipFileAttribute<Integer>("zip:method",0));
    byte[] bytes = contents.getBytes();
    Files.write(pathInZip,bytes,StandardOpenOption.WRITE);
    }

Zipfileattribute class is the smallest implementation of attribute interface I can publish it, but it's just a key value pair (name, value)

This code successfully creates the zipfile, but when I open the zipfile with 7zip, I see that the mimeType file is stored in the zip as deflated (8) instead of stored (0) I need So the question is, how do I correctly encode the attribute so that it can be stored as stored

Solution

This is not well documented, but the JDK's zip file system provider supports fileattributeview. Zip

This is my zip code:

public static void main(final String... args)
    throws IOException
{
    final Path zip = Paths.get("/home/fge/t.zip");
    final Map<String,?> env = Collections.singletonMap("readonly","true");
    final URI uri = URI.create("jar:" + zip.toUri());

    try (
        final FileSystem fs = FileSystems.newFileSystem(uri,env);
    ) {
        final Path slash = fs.getPath("/");
        Files.readAttributes(slash,"zip:*").forEach( (key,val) -> {
            System.out.println("Attribute name: " + key);
            System.out.printf("Value: %s (class: %s)\n",val,val != null ? val.getClass(): "N/A");
        });
    }
}

Output:

Attribute name: size
Value: 0 (class: class java.lang.Long)
Attribute name: creationTime
Value: null (class: N/A)
Attribute name: lastAccessTime
Value: null (class: N/A)
Attribute name: lastModifiedTime
Value: 1969-12-31T23:59:59.999Z (class: class java.nio.file.attribute.FileTime)
Attribute name: isDirectory
Value: true (class: class java.lang.Boolean)
Attribute name: isRegularFile
Value: false (class: class java.lang.Boolean)
Attribute name: isSymbolicLink
Value: false (class: class java.lang.Boolean)
Attribute name: isOther
Value: false (class: class java.lang.Boolean)
Attribute name: fileKey
Value: null (class: N/A)
Attribute name: compressedSize
Value: 0 (class: class java.lang.Long)
Attribute name: crc
Value: 0 (class: class java.lang.Long)
Attribute name: method
Value: 0 (class: class java.lang.Integer)

It looks like the "zip: Method" attribute is what you want

So if you want to change the method, if you have a path into your zip file system, it looks like you can do (untested!):

Files.setAttribute(thePath,"zip:method",ZipEntry.DEFLATED);
The content of this article comes from the network collection of netizens. It is used as a learning reference. The copyright belongs to the original author.
THE END
分享
二维码
< <上一篇
下一篇>>