Tells whether or not the given file is a ZAP add-on. @param file the file to be checked. @return the result of the validation. @since 2.8.0 @see #isAddOn(Path)
(Path file)
| 385 | * @see #isAddOn(Path) |
| 386 | */ |
| 387 | public static ValidationResult isValidAddOn(Path file) { |
| 388 | if (file == null || file.getNameCount() == 0) { |
| 389 | return new ValidationResult(ValidationResult.Validity.INVALID_PATH); |
| 390 | } |
| 391 | |
| 392 | if (!isAddOnFileName(file.getFileName().toString())) { |
| 393 | return new ValidationResult(ValidationResult.Validity.INVALID_FILE_NAME); |
| 394 | } |
| 395 | |
| 396 | if (!Files.isRegularFile(file) || !Files.isReadable(file)) { |
| 397 | return new ValidationResult(ValidationResult.Validity.FILE_NOT_READABLE); |
| 398 | } |
| 399 | |
| 400 | try (ZipFile zip = new ZipFile(file.toFile())) { |
| 401 | ZipEntry manifestEntry = zip.getEntry(MANIFEST_FILE_NAME); |
| 402 | if (manifestEntry == null) { |
| 403 | return new ValidationResult(ValidationResult.Validity.MISSING_MANIFEST); |
| 404 | } |
| 405 | |
| 406 | try (InputStream zis = zip.getInputStream(manifestEntry)) { |
| 407 | ZapAddOnXmlFile manifest; |
| 408 | try { |
| 409 | manifest = new ZapAddOnXmlFile(zis); |
| 410 | } catch (IOException e) { |
| 411 | return new ValidationResult(ValidationResult.Validity.INVALID_MANIFEST, e); |
| 412 | } |
| 413 | |
| 414 | if (hasInvalidLibs(file, manifest, zip)) { |
| 415 | return new ValidationResult(ValidationResult.Validity.INVALID_LIB); |
| 416 | } |
| 417 | return new ValidationResult(manifest); |
| 418 | } |
| 419 | } catch (ZipException e) { |
| 420 | return new ValidationResult(ValidationResult.Validity.UNREADABLE_ZIP_FILE, e); |
| 421 | } catch (Exception e) { |
| 422 | return new ValidationResult(ValidationResult.Validity.IO_ERROR_FILE, e); |
| 423 | } |
| 424 | } |
| 425 | |
| 426 | private static boolean hasInvalidLibs(Path file, ZapAddOnXmlFile manifest, ZipFile zip) { |
| 427 | return manifest.getLibs().stream() |