0

I'm using ZipInputStream in following way (Code):

public InputStream getArtifactInputStream(InputStream contentInputStream)
  throws ArtifactProviderException
{       
    ZipInputStream zipInputStream = new ZipInputStream(contentInputStream);

    InputStream artifactContent=null;
    Properties externalizedProperties = null;
    ZipEntry ze=null;
    try {
        ze = zipInputStream.getNextEntry();
    } catch (IOException e) {
        throw new ArtifactProviderException(e);
    }
    boolean propertiesFound = false;
    try {
        while (ze  != null) {
          String name = ze.getName();
          if(name.endsWith(PROP_EXTENSION) && !ze.isDirectory())
          {  
              externalizedProperties = new java.util.Properties();
              externalizedProperties.load(zipInputStream);
              propertiesFound = true;
          }
          else if(name.endsWith(ARTIFACT_EXTENSION) && !ze.isDirectory())
          {  
              artifactContent = zipInputStream;

              artifactName = name.substring(name.lastIndexOf("/")+1, name.indexOf(ARTIFACT_EXTENSION));
              break;
          }
            ze=zipInputStream.getNextEntry();  
        }
    } catch (IOException e) {
        throw new ArtifactProviderException(e);
    }   
    return artifactContent;
}

The problem is i'm not sure if PROP_EXTENSION file will appear first or ARTIFACT_EXTENSION while looping on zipEntry, hence i can't use "break" in any of the these cases, if i don't use break the while loop completes the loop and at the end artifactContent points to zipInputStream which cotains 'null' zipEntry.

How do i solve this problem, is there any alternative here?

'contentInputStream' is basically jar or a zip.

Hope I'm able to explain the problem, please revert back if you need more details.

Thanks in advance, Piyush

Bernhard Barker
  • 54,589
  • 14
  • 104
  • 138
  • 1
    I think you need to more clearly explain the structure of your ZIP file and why you want to return the ZipInputStream from a method halfway through iterating over it. – wrschneider Feb 07 '13 at 14:08
  • I'm not sure what your point is in returning the input stream, at the point at which you do both entries have already processed. Also, you don't appear to be using the loaded properties for anything, so why read them at all? – Perception Feb 07 '13 at 14:15
  • @wrschneider99: ZIP file contains resources folder which contains *.prop file, it also has src/artifact folder which contains *.artifact file. If i completely iterate over zip entry it points to null in zipinputstream, so we are breaking from the while loop. – user2050957 Feb 07 '13 at 15:57
  • @wrschneider99: ZIP file contains resources folder which contains *.prop file, it also has src/artifact folder which contains *.artifact file, along with many other files and folders, but we are mainly interested in these 2 files. If i completely iterate over zip entry it points to null in zipinputstream, so we are breaking from the while loop. @Perception : Loaded properties is a class variable, and it is depicted here as local variable, but it is used. Input stream `artfactContent` is not yet processed, and need to be parsed, this is returned from the method. – user2050957 Feb 07 '13 at 16:04

0 Answers0