karlpauls commented on a change in pull request #14: Zip file extract
URL:
https://github.com/apache/sling-org-apache-sling-feature-analyser/pull/14#discussion_r291049976
##########
File path:
src/main/java/org/apache/sling/feature/scanner/impl/ContentPackageScanner.java
##########
@@ -82,117 +83,120 @@ private void extractContentPackage(final
ContentPackageDescriptor cp,
toDir.mkdirs();
final List<File> toProcess = new ArrayList<>();
-
- try (final ZipInputStream zis = new ZipInputStream(new
FileInputStream(archive)) ) {
- boolean done = false;
- while ( !done ) {
- final ZipEntry entry = zis.getNextEntry();
- if ( entry == null ) {
- done = true;
- } else {
- final String entryName = entry.getName();
- if ( !entryName.endsWith("/") &&
entryName.startsWith("jcr_root/") ) {
- final String contentPath = entryName.substring(8);
-
- FileType fileType = null;
-
- if ( entryName.endsWith(".zip") ) {
- // embedded content package
- fileType = FileType.PACKAGE;
-
- // check for libs or apps
- } else if ( entryName.startsWith("jcr_root/libs/")
|| entryName.startsWith("jcr_root/apps/") ) {
-
- // check if this is an install folder (I)
- // install folders are either named:
- // "install" or
- // "install.{runmode}"
- boolean isInstall =
entryName.indexOf("/install/") != -1;
+
+ ZipFile zipFile = null;
+ try {
+ zipFile = new ZipFile(archive);
+ Enumeration<? extends ZipEntry> entries = zipFile.entries();
+
+ while( entries.hasMoreElements() ) {
+ final ZipEntry entry = entries.nextElement();
+ final String entryName = entry.getName();
+ logger.debug("Content package entry {}", entryName);
+ final InputStream zis = zipFile.getInputStream(entry);
+
+ if ( !entryName.endsWith("/") &&
entryName.startsWith("jcr_root/") ) {
+ final String contentPath = entryName.substring(8);
+
+ FileType fileType = null;
+
+ if ( entryName.endsWith(".zip") ) {
+ // embedded content package
+ fileType = FileType.PACKAGE;
+
+ // check for libs or apps
+ } else if ( entryName.startsWith("jcr_root/libs/") ||
entryName.startsWith("jcr_root/apps/") ) {
+
+ // check if this is an install folder (I)
+ // install folders are either named:
+ // "install" or
+ // "install.{runmode}"
+ boolean isInstall = entryName.indexOf("/install/")
!= -1;
+ if ( !isInstall ) {
+ final int pos = entryName.indexOf("/install.");
+ if ( pos != -1 ) {
+ final int endSlashPos =
entryName.indexOf('/', pos + 1);
+ if ( endSlashPos != -1 ) {
+ isInstall = true;
+ }
+ }
+ }
+ if ( !isInstall ) {
+ // check if this is an install folder (II)
+ // config folders are either named:
+ // "config" or
+ // "config.{runmode}"
+ isInstall = entryName.indexOf("/config/") !=
-1;
if ( !isInstall ) {
- final int pos =
entryName.indexOf("/install.");
+ final int pos =
entryName.indexOf("/config.");
if ( pos != -1 ) {
final int endSlashPos =
entryName.indexOf('/', pos + 1);
if ( endSlashPos != -1 ) {
isInstall = true;
}
}
}
- if ( !isInstall ) {
- // check if this is an install folder (II)
- // config folders are either named:
- // "config" or
- // "config.{runmode}"
- isInstall = entryName.indexOf("/config/")
!= -1;
- if ( !isInstall ) {
- final int pos =
entryName.indexOf("/config.");
- if ( pos != -1 ) {
- final int endSlashPos =
entryName.indexOf('/', pos + 1);
- if ( endSlashPos != -1 ) {
- isInstall = true;
- }
- }
- }
- }
+ }
- if (isInstall ) {
+ if (isInstall ) {
- if ( entryName.endsWith(".jar") ) {
- fileType = FileType.BUNDLE;
- } else if ( entryName.endsWith(".xml") ||
entryName.endsWith(".config") ) {
- fileType = FileType.CONFIG;
- }
- }
+ if ( entryName.endsWith(".jar") ) {
+ fileType = FileType.BUNDLE;
+ } else if ( entryName.endsWith(".xml") ||
entryName.endsWith(".config") ) {
+ fileType = FileType.CONFIG;
+ }
}
+ }
- if ( fileType != null ) {
- logger.debug("- extracting : {}", entryName);
- final File newFile = new File(toDir,
entryName.replace('/', File.separatorChar));
- newFile.getParentFile().mkdirs();
+ if ( fileType != null ) {
+ logger.debug("- extracting : {}", entryName);
+ final File newFile = new File(toDir,
entryName.replace('/', File.separatorChar));
+ newFile.getParentFile().mkdirs();
- try (final FileOutputStream fos = new
FileOutputStream(newFile)) {
- int len;
- while ((len = zis.read(buffer)) > -1) {
- fos.write(buffer, 0, len);
- }
+ try (final FileOutputStream fos = new
FileOutputStream(newFile)) {
Review comment:
Like only open the input stream in this try()
----------------------------------------------------------------
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
For queries about this service, please contact Infrastructure at:
[email protected]
With regards,
Apache Git Services