java.lang.UnsupportedOperationException: 'comment' is unknown or read-only attribute

JDK Bug System | Webbug Group | 6 months ago
Your exception is missing from the Samebug knowledge base.
Here are the best solutions we found on the Internet.
Click on the to mark the helpful solution and get rewards for you help.
  1. 0

    A DESCRIPTION OF THE REQUEST : Setting/getting a comment for an entry in a zip/jar archive is not possible via the zip file system provider. JUSTIFICATION : It would be nice if the com.sun.nio.zipfs.ZipFileSystem would support the same features as ZipFile/ZipOuputStream in order to be able to phase out the old-school APIs and use NIO for everything. EXPECTED VERSUS ACTUAL BEHAVIOR : EXPECTED - Setting/getting comments for archive entries is supported through NIO. ACTUAL - Trying to set the comment attribute yields the following exception: java.lang.UnsupportedOperationException: 'comment' is unknown or read-only attribute at com.sun.nio.zipfs.ZipFileAttributeView.setAttribute( at com.sun.nio.zipfs.ZipPath.setAttribute( at com.sun.nio.zipfs.ZipFileSystemProvider.setAttribute( at java.nio.file.Files.setAttribute( Trying to read the comment attribute always returns 'null', even when a comment is set. ---------- BEGIN SOURCE ---------- import; import; import; import java.nio.charset.StandardCharsets; import java.nio.file.FileSystem; import java.nio.file.FileSystems; import java.nio.file.Files; import java.nio.file.Path; import java.util.Collections; import java.util.HashMap; import java.util.Map; import; import; import static org.junit.Assert.assertEquals; import static org.junit.Assert.assertNull; import org.junit.Test; public class ZipFileSystemCommentTest { @Test public void getComment() throws IOException { Path archive = createOldschoolArchive(); try { Object comment = null; Object plain = ""; URI uri = URI.create("jar:" + archive.toUri()); try (FileSystem zipfs = FileSystems.newFileSystem(uri, new HashMap<>())) { comment = Files.getAttribute(zipfs.getPath("annotated.txt"), "comment"); plain = Files.getAttribute(zipfs.getPath("file.txt"), "comment"); } assertEquals("This is an entry with a comment", comment); assertNull(plain); } finally { Files.deleteIfExists(archive); } } @Test public void setComment() throws IOException { Path archive = Files.createTempFile(null, ".zip"); Files.deleteIfExists(archive); try { URI uri = URI.create("jar:" + archive.toUri()); Map<String, ?> env = Collections.singletonMap("create", "true"); try (FileSystem zipfs = FileSystems.newFileSystem(uri, env)) { Path entry = zipfs.getPath("test.txt"); Files.write(entry, text("some text")); Files.setAttribute(entry, "comment", "This is a comment"); } Object comment = null; try (FileSystem zipfs = FileSystems.newFileSystem(uri, new HashMap<String, String>())) { comment = Files.getAttribute(zipfs.getPath("test.txt"), "comment"); } assertEquals("This is a comment", comment); } finally { Files.deleteIfExists(archive); } } private Path createOldschoolArchive() throws IOException { Path archive = Files.createTempFile(null, ".zip"); try (ZipOutputStream out = new ZipOutputStream( new FileOutputStream(archive.toFile()))) { ZipEntry commented = new ZipEntry("annotated.txt"); commented.setComment("This is an entry with a comment"); out.putNextEntry(commented); out.write(text("Entry with comment")); out.putNextEntry(new ZipEntry("file.txt")); out.write(text("Entry without comment")); } return archive; } private byte[] text(String text) { return text.getBytes(StandardCharsets.UTF_8); } } ---------- END SOURCE ---------- CUSTOMER SUBMITTED WORKAROUND : Other than using the legacy API, there is no immediate workaround. But it's trivial to extend the zip file system provider to support comments. The basic bits are already there. The functionality is just not exposed. When desired, I could setup a repository with the necessary changes.

    JDK Bug System | 6 months ago | Webbug Group
    java.lang.UnsupportedOperationException: 'comment' is unknown or read-only attribute

    Root Cause Analysis

    1. java.lang.UnsupportedOperationException

      'comment' is unknown or read-only attribute

      at com.sun.nio.zipfs.ZipFileAttributeView.setAttribute()
    2. com.sun.nio
      1. com.sun.nio.zipfs.ZipFileAttributeView.setAttribute(
      2. com.sun.nio.zipfs.ZipPath.setAttribute(
      3. com.sun.nio.zipfs.ZipFileSystemProvider.setAttribute(
      3 frames
    3. Java RT
      1. java.nio.file.Files.setAttribute(
      1 frame