karlpauls commented on issue #14: Zip file extract
URL: 
https://github.com/apache/sling-org-apache-sling-feature-analyser/pull/14#issuecomment-500862327
 
 
   Might be as easy as:
   
   ```
   diff --cc 
src/main/java/org/apache/sling/feature/scanner/impl/ContentPackageScanner.java
   index c1a5871,b57b199..0000000
   --- 
a/src/main/java/org/apache/sling/feature/scanner/impl/ContentPackageScanner.java
   +++ 
b/src/main/java/org/apache/sling/feature/scanner/impl/ContentPackageScanner.java
   @@@ -20,6 -20,6 +20,7 @@@ import org.apache.sling.feature.Artifac
     import org.apache.sling.feature.ArtifactId;
     import org.apache.sling.feature.Configuration;
     import org.apache.sling.feature.scanner.BundleDescriptor;
   ++import org.apache.sling.feature.io.IOUtils;
     import org.slf4j.Logger;
     import org.slf4j.LoggerFactory;
     
   @@@ -27,11 -27,11 +28,12 @@@ import java.io.File
     import java.io.FileOutputStream;
     import java.io.FileReader;
     import java.io.IOException;
    +import java.io.InputStream;
     import java.io.Reader;
   + import java.net.URL;
     import java.nio.file.Files;
     import java.util.ArrayList;
    +import java.util.Enumeration;
     import java.util.HashSet;
     import java.util.List;
     import java.util.Properties;
   @@@ -83,49 -80,34 +82,49 @@@ public class ContentPackageScanner 
                 toDir.mkdirs();
     
                 final List<File> toProcess = new ArrayList<>();
    -
    -            try (final ZipInputStream zis = new 
ZipInputStream(archive.openStream()) ) {
    -                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;
    +            
   -             try (final ZipFile zipFile = new ZipFile(archive)) {
   ++            try (final ZipFile zipFile = 
IOUtils.getJarFileFromURL(archive, true, null)) {
    +                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);
    +                    
    +                    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 ) {
   @@@ -133,71 -115,86 +132,69 @@@
                                             }
                                         }
                                     }
    -                                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();
    -
    -                                try (final FileOutputStream fos = new 
FileOutputStream(newFile)) {
    -                                    int len;
    -                                    while ((len = zis.read(buffer)) > -1) {
    -                                        fos.write(buffer, 0, len);
    -                                    }
    +                        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);
    +                                    final InputStream zis = 
zipFile.getInputStream(entry);
    +                            ) {
    +                                int len;
    +                                while ((len = zis.read(buffer)) > -1) {
    +                                    fos.write(buffer, 0, len);
    +                                }
    +                            } 
    +
    +                            if ( fileType == FileType.BUNDLE ) {
    +                                int startLevel = 20;
    +                                final int lastSlash = 
contentPath.lastIndexOf('/');
    +                                final int nextSlash = 
contentPath.lastIndexOf('/', lastSlash - 1);
    +                                final String part = 
contentPath.substring(nextSlash + 1, lastSlash);
    +                                try {
    +                                    startLevel = Integer.valueOf(part);
    +                                } catch ( final NumberFormatException 
ignore ) {
    +                                    // ignore
                                     }
     
    -                                if ( fileType == FileType.BUNDLE ) {
    -                                    int startLevel = 20;
    -                                    final int lastSlash = 
contentPath.lastIndexOf('/');
    -                                    final int nextSlash = 
contentPath.lastIndexOf('/', lastSlash - 1);
    -                                    final String part = 
contentPath.substring(nextSlash + 1, lastSlash);
    -                                    try {
    -                                        startLevel = Integer.valueOf(part);
    -                                    } catch ( final NumberFormatException 
ignore ) {
    -                                        // ignore
    -                                    }
    -
    -                                    final Artifact bundle = new 
Artifact(extractArtifactId(tempDir, newFile));
    -                                    final BundleDescriptor info = new 
BundleDescriptorImpl(bundle, newFile.toURI().toURL(), startLevel);
    -                                    
bundle.getMetadata().put("content-package", cp.getArtifact().getId().toMvnId());
    -                                    
bundle.getMetadata().put("content-path", contentPath);
    -
    -                                    cp.bundles.add(info);
    +                                final Artifact bundle = new 
Artifact(extractArtifactId(tempDir, newFile));
   -                                 final BundleDescriptor info = new 
BundleDescriptorImpl(bundle, newFile, startLevel);
   -                                 
bundle.getMetadata().put("content-package", cp.getArtifact().getId().toMvnId());
   -                                 bundle.getMetadata().put("content-path", 
contentPath);
   ++                                final BundleDescriptor info = new 
BundleDescriptorImpl(bundle, newFile.toURI().toURL(), startLevel);
     
    -                                } else if ( fileType == FileType.CONFIG ) {
    +                                cp.bundles.add(info);
     
    -                                    final Configuration configEntry = 
this.process(newFile, cp.getArtifact(), contentPath);
    -                                    if ( configEntry != null ) {
    +                            } else if ( fileType == FileType.CONFIG ) {
     
    -                                        cp.configs.add(configEntry);
    -                                    }
    +                                final Configuration configEntry = 
this.process(newFile, cp.getArtifact(), contentPath);
    +                                if ( configEntry != null ) {
     
    -                                } else if ( fileType == FileType.PACKAGE ) 
{
    -                                    toProcess.add(newFile);
    +                                    cp.configs.add(configEntry);
                                     }
     
    +                            } else if ( fileType == FileType.PACKAGE ) {
    +                                toProcess.add(newFile);
                                 }
     
                             }
    -                        zis.closeEntry();
                         }
                     }
    -
    -            }
    +                
    +            } 
     
                 for(final File f : toProcess) {
   -                 extractContentPackage(cp, infos, f);
   +                 extractContentPackage(cp, infos, f.toURI().toURL());
                     final ContentPackageDescriptor i = new 
ContentPackageDescriptor(f.getName());
                     final int lastDot = f.getName().lastIndexOf(".");
                     i.setName(f.getName().substring(0, lastDot));
   diff --cc 
src/main/java/org/apache/sling/feature/scanner/impl/FelixFrameworkScanner.java
   index 3faa72b,956a90c..0000000
   --- 
a/src/main/java/org/apache/sling/feature/scanner/impl/FelixFrameworkScanner.java
   +++ 
b/src/main/java/org/apache/sling/feature/scanner/impl/FelixFrameworkScanner.java
   @@@ -27,13 -27,13 +27,15 @@@ import org.apache.sling.feature.builder
     import org.apache.sling.feature.scanner.BundleDescriptor;
     import org.apache.sling.feature.scanner.PackageInfo;
     import org.apache.sling.feature.scanner.spi.FrameworkScanner;
   ++import org.apache.sling.feature.io.IOUtils;
     import org.osgi.framework.Constants;
     import org.osgi.resource.Capability;
     
     import java.io.File;
    -import java.io.FileInputStream;
     import java.io.IOException;
    +import java.io.InputStream;
    +import java.util.Enumeration;
   + import java.net.URL;
     import java.util.HashMap;
     import java.util.List;
     import java.util.Map;
   @@@ -141,17 -141,16 +143,17 @@@ public class FelixFrameworkScanner impl
     
         private static final String DEFAULT_PROPERTIES = "default.properties";
     
   -     Map<String,String> getFrameworkProperties(final Map<String,String> 
appProps, final File framework)
   +     Map<String,String> getFrameworkProperties(final Map<String,String> 
appProps, final URL framework)
         throws IOException {
             final Map<String, Properties> propsMap = new HashMap<>();
    -        try (final ZipInputStream zis = new 
ZipInputStream(framework.openStream()) ) {
    -            boolean done = false;
    -            while ( !done ) {
    -                final ZipEntry entry = zis.getNextEntry();
    -                if ( entry == null ) {
    -                    done = true;
    -                } else {
    +       
   -         try (final ZipFile zipFile = new ZipFile(framework)) {
   ++        try (final ZipFile zipFile = IOUtils.getJarFileFromURL(framework, 
true, null)) {
    +            Enumeration<? extends ZipEntry> entries = zipFile.entries();
    +            
    +            while ( entries.hasMoreElements() ) {
    +                final ZipEntry entry = entries.nextElement();
    +                 
    +                try (final InputStream zis = 
zipFile.getInputStream(entry)) {
                         final String entryName = entry.getName();
                         if ( entryName.endsWith(".properties") ) {
                             final Properties props = new Properties();
   diff --git 
a/src/test/java/org/apache/sling/feature/scanner/impl/ContentPackageScannerTest.java
 
b/src/test/java/org/apache/sling/feature/scanner/impl/ContentPackageScannerTest.java
   index ceab2d4..593bc2c 100644
   --- 
a/src/test/java/org/apache/sling/feature/scanner/impl/ContentPackageScannerTest.java
   +++ 
b/src/test/java/org/apache/sling/feature/scanner/impl/ContentPackageScannerTest.java
   @@ -27,6 +27,7 @@ import java.util.Enumeration;
    import java.util.Set;
    import java.util.zip.ZipEntry;
    import java.util.zip.ZipFile;
   +import java.net.URL;
    
    import org.apache.sling.feature.Artifact;
    import org.apache.sling.feature.ArtifactId;
   @@ -40,7 +41,7 @@ public class ContentPackageScannerTest {
        private static final String COORDINATES_TEST_PACKAGE_A_10 = 
"my_packages:test_a:1.0";
        private static ArtifactId TEST_PACKAGE_AID_A_10 = 
ArtifactId.fromMvnId(COORDINATES_TEST_PACKAGE_A_10);
    
   -    private File file;
   +    private URL file;
        
        private Artifact artifact;
        
   @@ -52,10 +53,10 @@ public class ContentPackageScannerTest {
            
            artifact = new Artifact(TEST_PACKAGE_AID_A_10);
            
   -        test_descriptor = new ContentPackageDescriptor(file.getName());
   +        test_descriptor = new ContentPackageDescriptor(file.getPath());
            test_descriptor.setName("test-content");
            test_descriptor.setArtifact(artifact);
   -        test_descriptor.setArtifactFile(file);
   +        test_descriptor.setArtifactFile(file.toURI().toURL());
        }
        
        @Test
   @@ -65,14 +66,14 @@ public class ContentPackageScannerTest {
            
assetDescriptor((ContentPackageDescriptor)descriptors_a.toArray()[0]);
        }
        
   -    private File getTestFile(String path) throws URISyntaxException {
   -        return new File(getClass().getResource(path).toURI());
   +    private URL getTestFile(String path) throws URISyntaxException {
   +        return getClass().getResource(path);
        }
        
        private void assetDescriptor(ContentPackageDescriptor desc) {
            assertEquals(desc.getName(), test_descriptor.getName());
            assertEquals(desc.getArtifact().getId().getArtifactId(), 
test_descriptor.getArtifact().getId().getArtifactId());
   -        assertEquals(desc.getArtifactFile().getAbsolutePath(), 
test_descriptor.getArtifactFile().getAbsolutePath());
   +        assertEquals(desc.getArtifactFile(), 
test_descriptor.getArtifactFile());
            
            assertTrue(desc.bundles != null && !desc.bundles.isEmpty());
            BundleDescriptor bundles[] = desc.bundles.toArray(new 
BundleDescriptor[desc.bundles.size()]);
   ```

----------------------------------------------------------------
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

Reply via email to