Index: pom.xml
===================================================================
--- pom.xml	(revision 1152367)
+++ pom.xml	(working copy)
@@ -154,6 +154,12 @@
       <version>1.5.6</version>
       <scope>test</scope>
     </dependency>
+    <dependency>
+      <groupId>commons-io</groupId>
+      <artifactId>commons-io</artifactId>
+      <version>1.3.2</version>
+      <scope>test</scope>
+    </dependency>
   </dependencies>
 
   <dependencyManagement>
Index: src/main/java/org/apache/tika/detect/ZipContainerDetector.java
===================================================================
--- src/main/java/org/apache/tika/detect/ZipContainerDetector.java	(revision 1152367)
+++ src/main/java/org/apache/tika/detect/ZipContainerDetector.java	(working copy)
@@ -71,21 +71,24 @@
         try {
             File file = TikaInputStream.get(input).getFile();
             ZipFile zip = new ZipFile(file);
-
-            MediaType type = detectOpenDocument(zip);
-            if (type == null) {
-                type = detectOfficeOpenXML(zip, TikaInputStream.get(input));
+            try {
+	            MediaType type = detectOpenDocument(zip);
+	            if (type == null) {
+	                type = detectOfficeOpenXML(zip, TikaInputStream.get(input));
+	            }
+	            if (type == null) {
+	                type = detectIWork(zip);
+	            }
+	            if (type == null && zip.getEntry("META-INF/MANIFEST.MF") != null) {
+	                type = MediaType.application("java-archive");
+	            }
+	            if (type == null) {
+	                type = MediaType.APPLICATION_ZIP;
+	            }
+	            return type;
+            } finally {
+            	zip.close();
             }
-            if (type == null) {
-                type = detectIWork(zip);
-            }
-            if (type == null && zip.getEntry("META-INF/MANIFEST.MF") != null) {
-                type = MediaType.application("java-archive");
-            }
-            if (type == null) {
-                type = MediaType.APPLICATION_ZIP;
-            }
-            return type;
         } catch (IOException e) {
             return MediaType.APPLICATION_ZIP;
         }
Index: src/test/java/org/apache/tika/detect/TestContainerAwareDetector.java
===================================================================
--- src/test/java/org/apache/tika/detect/TestContainerAwareDetector.java	(revision 1152367)
+++ src/test/java/org/apache/tika/detect/TestContainerAwareDetector.java	(working copy)
@@ -16,11 +16,16 @@
  */
 package org.apache.tika.detect;
 
+import java.io.ByteArrayInputStream;
+import java.io.File;
 import java.io.IOException;
 import java.io.InputStream;
 
 import junit.framework.TestCase;
 
+import org.apache.commons.io.FileUtils;
+import org.apache.commons.io.IOUtils;
+import org.apache.commons.io.filefilter.FileFilterUtils;
 import org.apache.poi.poifs.filesystem.NPOIFSFileSystem;
 import org.apache.tika.io.TikaInputStream;
 import org.apache.tika.metadata.Metadata;
@@ -96,6 +101,28 @@
         assertDetect("testEXCEL.xlsb", "application/vnd.ms-excel.sheet.binary.macroEnabled.12");
     }
 
+	public void testRemovalTempfiles() throws Exception {
+		assertRemovalTempfiles("testWORD.docx");
+		assertRemovalTempfiles("test-documents.zip");
+	}
+
+	
+	private void assertRemovalTempfiles(String fileName) throws Exception {
+		// Test that temporary files created by tika are removed after closing TikaInputStream
+		int numberOfTempFiles = FileUtils.listFiles(new File(System.getProperty("java.io.tmpdir")), FileFilterUtils.prefixFileFilter("apache-tika"),null).size();
+		
+		// read file in bytearray
+		InputStream in = TestContainerAwareDetector.class.getResourceAsStream("/test-documents/" + fileName);
+		InputStream bis = new ByteArrayInputStream(IOUtils.toByteArray(in));
+		TikaInputStream stream = TikaInputStream.get(bis);
+        try {
+            detector.detect(stream, new Metadata());
+        } finally {
+            stream.close();
+        }		
+        assertEquals(numberOfTempFiles,FileUtils.listFiles(new File(System.getProperty("java.io.tmpdir")), FileFilterUtils.prefixFileFilter("apache-tika"),null).size());	
+	}
+		
     public void testDetectIWork() throws Exception {
         assertDetect("testKeynote.key", "application/vnd.apple.keynote");
         assertDetect("testNumbers.numbers", "application/vnd.apple.numbers");
