@Override
    public InputStream getAttachment(final Asset asset, final Attachment attachment) throws IOException, BadVersionException, RequestFailureException {
        final ZipFile repoZip = createZipFile();

        if (null == repoZip) {
            return null;
        }

        InputStream retInputStream = null;
        String attachmentId = attachment.get_id();

        try {
            if (attachmentId.contains("#")) {
                String assetId = asset.get_id();
                // new funky code to get an input stream to the license *inside* the main attachment. The start of
                // the assetId will point to the main attachment file.
                ZipEntry entry = createFromRelative(assetId);

                // If the entry wasn't found return null
                if (null == entry) {
                    return null;
                }

                // Get zip input stream to the asset inside the zip
                ZipInputStream zis = new ZipInputStream(repoZip.getInputStream(entry));

                // Get the input stream to the attachment inside the zip
                retInputStream = getInputStreamToLicenseInsideZip(zis, assetId, attachmentId);

            } else {
                // Get input stream to the attachment
                ZipEntry entry = createFromRelative(attachmentId);
                retInputStream = repoZip.getInputStream(entry);
            }
        } finally {
            // If we are throwing an exception the InputStream is never created so the logic below
            // to close the ZipFile when the InputStream is closed will never be called. So lets close
            // the ZipFile now as there is no InputStream to read from.
            if (retInputStream == null) {
                repoZip.close();
            }
        }

        // When the input stream gets closed we also need to close the ZipFile, however
        // the caller only has the input stream. So lets wrap the input stream and close
        // both the inputStream and the ZipFile when the caller calls close on the wrapped
        // input stream
        final InputStream is = retInputStream;
        InputStream wrappedIs = new InputStream() {

            /** {@inheritDoc} */
            @Override
            public int read(byte[] b) throws IOException {
                return is.read(b);
            }

            /** {@inheritDoc} */
            @Override
            public int read(byte[] b, int off, int len) throws IOException {
                return is.read(b, off, len);
            }

            /** {@inheritDoc} */
            @Override
            public long skip(long n) throws IOException {
                return is.skip(n);
            }

            /** {@inheritDoc} */
            @Override
            public int available() throws IOException {
                return is.available();
            }

            /** {@inheritDoc} */
            @Override
            public synchronized void mark(int readlimit) {
                is.mark(readlimit);
            }

            /** {@inheritDoc} */
            @Override
            public synchronized void reset() throws IOException {
                is.reset();
            }

            /** {@inheritDoc} */
            @Override
            public boolean markSupported() {
                return is.markSupported();
            }

            /** {@inheritDoc} */
            @Override
            public int read() throws IOException {
                return is.read();
            }

            /** {@inheritDoc} */
            @Override
            public void close() throws IOException {
                // When the input stream is closed, also close the zip file
                is.close();
                repoZip.close();
            }
        };
        return wrappedIs;
    }
@ Override public InputStream getAttachment ( final Asset asset , final Attachment attachment ) throws IOException , BadVersionException , RequestFailureException { final ZipFile repoZip = createZipFile ( ) ; if ( null == repoZip ) { return null ; } InputStream retInputStream = null ; String attachmentId = attachment . get_id ( ) ; try { if ( attachmentId . contains ( "#" ) ) { String assetId = asset . get_id ( ) ; ZipEntry entry = createFromRelative ( assetId ) ; if ( null == entry ) { return null ; } ZipInputStream zis = new ZipInputStream ( repoZip . getInputStream ( entry ) ) ; retInputStream = getInputStreamToLicenseInsideZip ( zis , assetId , attachmentId ) ; } else { ZipEntry entry = createFromRelative ( attachmentId ) ; retInputStream = repoZip . getInputStream ( entry ) ; } } finally { if ( retInputStream == null ) { repoZip . close ( ) ; } } final InputStream is = retInputStream ; InputStream wrappedIs = new InputStream ( ) { @ Override public int read ( byte [ ] b ) throws IOException { return is . read ( b ) ; } @ Override public int read ( byte [ ] b , int off , int len ) throws IOException { return is . read ( b , off , len ) ; } @ Override public long skip ( long n ) throws IOException { return is . skip ( n ) ; } @ Override public int available ( ) throws IOException { return is . available ( ) ; } @ Override public synchronized void mark ( int readlimit ) { is . mark ( readlimit ) ; } @ Override public synchronized void reset ( ) throws IOException { is . reset ( ) ; } @ Override public boolean markSupported ( ) { return is . markSupported ( ) ; } @ Override public int read ( ) throws IOException { return is . read ( ) ; } @ Override public void close ( ) throws IOException { is . close ( ) ; repoZip . close ( ) ; } } ; return wrappedIs ; }
This gets an input stream to the specified attachment in the zip.