staticclass ZipFileEntryInfo { // permissions to set initially privatefinal Set<PosixFilePermission> intialPerms; // permissions to set in a later call privatefinal Set<PosixFilePermission> laterPerms; // permissions that should be effective in the zip file privatefinal Set<PosixFilePermission> permsInZip; // permissions that should be returned by zipfs w/Posix support privatefinal Set<PosixFilePermission> permsPosix; // entry is a directory privatefinalboolean isDir; // need additional read flag in copy test privatefinalboolean setReadFlag;
static {
ENTRIES.put("dir", new ZipFileEntryInfo(ALLPERMS, null, ALLPERMS, ALLPERMS, true, false));
ENTRIES.put("uread", new ZipFileEntryInfo(UR, null, UR, UR, false, false));
ENTRIES.put("uwrite", new ZipFileEntryInfo(UW, null, UW, UW, false, true));
ENTRIES.put("uexec", new ZipFileEntryInfo(UE, null, UE, UE, false, true));
ENTRIES.put("gread", new ZipFileEntryInfo(GR, null, GR, GR, false, true));
ENTRIES.put("gwrite", new ZipFileEntryInfo(GW, null, GW, GW, false, true));
ENTRIES.put("gexec", new ZipFileEntryInfo(GE, null, GE, GE, false, true));
ENTRIES.put("oread", new ZipFileEntryInfo(OR, null, OR, OR, false, true));
ENTRIES.put("owrite", new ZipFileEntryInfo(OW, null, OW, OW, false, true));
ENTRIES.put("oexec", new ZipFileEntryInfo(OE, null, OE, OE, false, true));
ENTRIES.put("emptyperms", new ZipFileEntryInfo(EMPTYPERMS, null, EMPTYPERMS, EMPTYPERMS, false, true));
ENTRIES.put("noperms", new ZipFileEntryInfo(null, null, null, ALLPERMS, false, false));
ENTRIES.put("permslater", new ZipFileEntryInfo(null, UR, UR, UR, false, false));
}
privatestatic String expectedDefaultOwner(Path zf) { try { try {
PrivilegedExceptionAction<String> pa = ()->Files.getOwner(zf).getName(); return AccessController.doPrivileged(pa);
} catch (UnsupportedOperationException e) { // if we can't get the owner of the file, we fall back to system property user.name
PrivilegedAction<String> pa = ()->System.getProperty("user.name"); return AccessController.doPrivileged(pa);
}
} catch (PrivilegedActionException | SecurityException e) {
System.out.println("Caught " + e.getClass().getName() + "(" + e.getMessage() + ") when running a privileged operation to get the default owner."); returnnull;
}
}
privatestatic String expectedDefaultGroup(Path zf, String defaultOwner) { try { try {
PosixFileAttributeView zfpv = Files.getFileAttributeView(zf, PosixFileAttributeView.class); if (zfpv == null) { return defaultOwner;
}
PrivilegedExceptionAction<String> pa = ()->zfpv.readAttributes().group().getName(); return AccessController.doPrivileged(pa);
} catch (UnsupportedOperationException e) { return defaultOwner;
}
} catch (PrivilegedActionException | SecurityException e) {
System.out.println("Caught an exception when running a privileged operation to get the default group.");
e.printStackTrace(); returnnull;
}
}
try (FileSystem srcZip = createTestZipFile(ZIP_FILE, ENV_DEFAULT)) {
Path from = srcZip.getPath("/");
Files.walkFileTree(from, new CopyVisitor(from, UNZIP_DIR));
}
// we just check that the entries got extracted to file system
checkEntries(UNZIP_DIR, checkExpects.contentOnly);
// the target zip file is opened with Posix support // but we expect no permission data to be copied using the default copy method try (FileSystem tgtZip = createEmptyZipFile(ZIP_FILE_COPY, ENV_POSIX)) {
Files.walkFileTree(UNZIP_DIR, new CopyVisitor(UNZIP_DIR, tgtZip.getPath("/")));
}
// check entries on copied zipfs - no permission data should exist if (System.getProperty("os.name").toLowerCase().contains("windows")) try (FileSystem zip = FileSystems.newFileSystem(ZIP_FILE_COPY,
ENV_DEFAULT)) {
checkEntries(zip, checkExpects.noPermDataInZip);
}
}
try {
Files.getPosixFilePermissions(UNZIP_DIR);
} catch (Exception e) { // if we run into any exception here, be it because of the fact that the file system // is not Posix or if we have insufficient security permissions, we can't do this test.
System.out.println("This can't be tested here because of " + e); return;
}
try (FileSystem srcZip = createTestZipFile(ZIP_FILE, ENV_POSIX)) {
Path from = srcZip.getPath("/"); // copy permissions as well
Files.walkFileTree(from, new CopyVisitor(from, UNZIP_DIR, true));
}
// permissions should have been propagated to file system
checkEntries(UNZIP_DIR, checkExpects.permsPosix);
try (FileSystem tgtZip = createEmptyZipFile(ZIP_FILE_COPY, ENV_POSIX)) { // Make some files owner readable to be able to copy them into the zipfs
addOwnerRead(UNZIP_DIR);
// copy permissions as well
Files.walkFileTree(UNZIP_DIR, new CopyVisitor(UNZIP_DIR, tgtZip.getPath("/"), true));
// Fix back all the files in the target zip file which have been made readable before
removeOwnerRead(tgtZip.getPath("/"));
}
// check entries on copied zipfs - permission data should have been propagated try (FileSystem zip = FileSystems.newFileSystem(ZIP_FILE_COPY, ENV_POSIX)) {
checkEntries(zip, checkExpects.permsPosix);
}
}
// test with posix = true -> default values try (FileSystem zipIn = FileSystems.newFileSystem(ZIP_FILE, ENV_POSIX)) {
String defaultOwner = expectedDefaultOwner(ZIP_FILE);
String defaultGroup = expectedDefaultGroup(ZIP_FILE, defaultOwner); var entry = zipIn.getPath("/noperms");
comparePermissions(ALLPERMS, Files.getPosixFilePermissions(entry)); var owner = Files.getOwner(entry);
assertNotNull(owner, "owner should not be null"); if (defaultOwner != null) {
assertEquals(owner.getName(), defaultOwner);
}
Files.setOwner(entry, DUMMY_USER);
assertEquals(Files.getOwner(entry), DUMMY_USER); var view = Files.getFileAttributeView(entry, PosixFileAttributeView.class); var group = view.readAttributes().group();
assertNotNull(group, "group must not be null"); if (defaultGroup != null) {
assertEquals(group.getName(), defaultGroup);
}
view.setGroup(DUMMY_GROUP);
assertEquals(view.readAttributes().group(), DUMMY_GROUP);
entry = zipIn.getPath("/uexec");
Files.setPosixFilePermissions(entry, GR); // will be persisted
comparePermissions(GR, Files.getPosixFilePermissions(entry));
}
// test with posix = true + custom defaults of type String try (FileSystem zipIn = FileSystems.newFileSystem(ZIP_FILE, Map.of("enablePosixFileAttributes", true, "defaultOwner", "auser", "defaultGroup", "agroup", "defaultPermissions", "r--------")))
{ var entry = zipIn.getPath("/noperms");
comparePermissions(UR, Files.getPosixFilePermissions(entry));
assertEquals(Files.getOwner(entry).getName(), "auser"); var view = Files.getFileAttributeView(entry, PosixFileAttributeView.class);
assertEquals(view.readAttributes().group().getName(), "agroup"); // check if the change to permissions of /uexec was persisted
comparePermissions(GR, Files.getPosixFilePermissions(zipIn.getPath("/uexec")));
}
// test with posix = true + custom defaults as Objects try (FileSystem zipIn = FileSystems.newFileSystem(ZIP_FILE, Map.of("enablePosixFileAttributes", true, "defaultOwner", DUMMY_USER, "defaultGroup", DUMMY_GROUP, "defaultPermissions", UR)))
{ var entry = zipIn.getPath("/noperms");
comparePermissions(UR, Files.getPosixFilePermissions(entry));
assertEquals(Files.getOwner(entry), DUMMY_USER); var view = Files.getFileAttributeView(entry, PosixFileAttributeView.class);
assertEquals(view.readAttributes().group(), DUMMY_GROUP);
}
}
/** *Sanitychecktotestwhetherthezipfilecanbeunzippedwiththejava.util.zipAPI. * *@throwsIOException
*/
@Test publicvoid testUnzipWithJavaUtilZip() throws IOException {
createTestZipFile(ZIP_FILE, ENV_DEFAULT).close();
delTree(UNZIP_DIR);
Files.createDirectory(UNZIP_DIR);
File targetDir = UNZIP_DIR.toFile(); try (ZipFile zf = new ZipFile(ZIP_FILE.toFile())) {
Enumeration<? extends ZipEntry> zenum = zf.entries(); while (zenum.hasMoreElements()) {
ZipEntry ze = zenum.nextElement();
File target = new File(targetDir + File.separator + ze.getName()); if (ze.isDirectory()) {
target.mkdir(); continue;
} try (InputStream is = zf.getInputStream(ze);
FileOutputStream fos = new FileOutputStream(target))
{ while (is.available() > 0) {
fos.write(is.read());
}
}
}
}
}
/** *Sanitychecktotestwhetherajarfilecreatedwithzipfscanbe *extractedwiththejava.util.jarAPI. * *@throwsIOException
*/
@Test publicvoid testJarFile() throws IOException { // create jar file using zipfs with default options
createTestZipFile(JAR_FILE, ENV_DEFAULT).close();
// extract it using java.util.jar.JarFile
delTree(UNZIP_DIR);
Files.createDirectory(UNZIP_DIR);
File targetDir = UNZIP_DIR.toFile(); try (JarFile jf = new JarFile(ZIP_FILE.toFile())) {
Enumeration<? extends JarEntry> zenum = jf.entries(); while (zenum.hasMoreElements()) {
JarEntry ze = zenum.nextElement();
File target = new File(targetDir + File.separator + ze.getName()); if (ze.isDirectory()) {
target.mkdir(); continue;
} try (InputStream is = jf.getInputStream(ze);
FileOutputStream fos = new FileOutputStream(target))
{ while (is.available() > 0) {
fos.write(is.read());
}
}
}
}
// extract it using the jar tool
delTree(UNZIP_DIR);
System.out.println("jar xvf " + JAR_FILE);
// the run method catches IOExceptions, we need to expose them int rc = JAR_TOOL.run(System.out, System.err, "xvf", JAR_FILE.toString());
assertEquals(rc, 0, "Return code of jar call is " + rc + " but expected 0");
}
}
Messung V0.5 in Prozent
¤ Dauer der Verarbeitung: 0.19 Sekunden
(vorverarbeitet am 2026-06-10)
¤
Die Informationen auf dieser Webseite wurden
nach bestem Wissen sorgfältig zusammengestellt. Es wird jedoch weder Vollständigkeit, noch Richtigkeit,
noch Qualität der bereit gestellten Informationen zugesichert.
Bemerkung:
Die farbliche Syntaxdarstellung und die Messung sind noch experimentell.