You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@tika.apache.org by ni...@apache.org on 2014/09/08 23:59:37 UTC

svn commit: r1623593 - /tika/trunk/tika-parsers/src/main/java/org/apache/tika/parser/pkg/PackageParser.java

Author: nick
Date: Mon Sep  8 21:59:37 2014
New Revision: 1623593

URL: http://svn.apache.org/r1623593
Log:
Patch from Luis Filipe Nassif from TIKA-1411 - Avoid 7z file leak through use of TemporaryResources

Modified:
    tika/trunk/tika-parsers/src/main/java/org/apache/tika/parser/pkg/PackageParser.java

Modified: tika/trunk/tika-parsers/src/main/java/org/apache/tika/parser/pkg/PackageParser.java
URL: http://svn.apache.org/viewvc/tika/trunk/tika-parsers/src/main/java/org/apache/tika/parser/pkg/PackageParser.java?rev=1623593&r1=1623592&r2=1623593&view=diff
==============================================================================
--- tika/trunk/tika-parsers/src/main/java/org/apache/tika/parser/pkg/PackageParser.java (original)
+++ tika/trunk/tika-parsers/src/main/java/org/apache/tika/parser/pkg/PackageParser.java Mon Sep  8 21:59:37 2014
@@ -105,34 +105,37 @@ public class PackageParser extends Abstr
             InputStream stream, ContentHandler handler,
             Metadata metadata, ParseContext context)
             throws IOException, SAXException, TikaException {
-        // At the end we want to close the archive stream to release
-        // any associated resources, but the underlying document stream
-        // should not be closed
-        stream = new CloseShieldInputStream(stream);
-
+       
         // Ensure that the stream supports the mark feature
-        if (! TikaInputStream.isTikaInputStream(stream)) {
+        if (! TikaInputStream.isTikaInputStream(stream))
             stream = new BufferedInputStream(stream);
-        }
-
-        ArchiveInputStream ais;
+        
+        
+        TemporaryResources tmp = new TemporaryResources();
+        ArchiveInputStream ais = null;
         try {
-            ArchiveStreamFactory factory = context.get(
-                    ArchiveStreamFactory.class, new ArchiveStreamFactory());
-            ais = factory.createArchiveInputStream(stream);
+            ArchiveStreamFactory factory = context.get(ArchiveStreamFactory.class, new ArchiveStreamFactory());
+            // At the end we want to close the archive stream to release
+            // any associated resources, but the underlying document stream
+            // should not be closed
+            ais = factory.createArchiveInputStream(new CloseShieldInputStream(stream));
+            
         } catch (StreamingNotSupportedException sne) {
             // Most archive formats work on streams, but a few need files
             if (sne.getFormat().equals(ArchiveStreamFactory.SEVEN_Z)) {
                 // Rework as a file, and wrap
                 stream.reset();
-                TikaInputStream tstream = TikaInputStream.get(stream);
+                TikaInputStream tstream = TikaInputStream.get(stream, tmp);
                 
                 // Pending a fix for COMPRESS-269, this bit is a little nasty
                 ais = new SevenZWrapper(new SevenZFile(tstream.getFile()));
+                
             } else {
+                tmp.close();
                 throw new TikaException("Unknown non-streaming format " + sne.getFormat(), sne);
             }
         } catch (ArchiveException e) {
+            tmp.close();
             throw new TikaException("Unable to unpack document stream", e);
         }
 
@@ -159,6 +162,7 @@ public class PackageParser extends Abstr
             }
         } finally {
             ais.close();
+            tmp.close();
         }
 
         xhtml.endDocument();
@@ -173,6 +177,7 @@ public class PackageParser extends Abstr
             // Fetch the metadata on the entry contained in the archive
             Metadata entrydata = new Metadata();
             entrydata.set(TikaCoreProperties.MODIFIED, entry.getLastModifiedDate());
+            entrydata.set(Metadata.CONTENT_LENGTH, Long.toString(entry.getSize()));
             if (name != null && name.length() > 0) {
                 entrydata.set(Metadata.RESOURCE_NAME_KEY, name);
                 AttributesImpl attributes = new AttributesImpl();
@@ -224,5 +229,10 @@ public class PackageParser extends Abstr
         public ArchiveEntry getNextEntry() throws IOException {
             return file.getNextEntry();
         }
+        
+        @Override
+        public void close() throws IOException {
+            file.close();
+        }
     }
 }