ZipDataSource.java
01 /**
02  * BSD-style license; for more info see http://pmd.sourceforge.net/license.html
03  */
04 package net.sourceforge.pmd.util.datasource;
05 
06 import java.io.IOException;
07 import java.io.InputStream;
08 import java.util.zip.ZipEntry;
09 import java.util.zip.ZipFile;
10 
11 
12 /**
13  * DataSource implementation to read data from an entry
14  * in a zip or jar file.
15  */
16 public class ZipDataSource implements DataSource {
17     private ZipFile zipFile;
18     private ZipEntry zipEntry;
19 
20     /**
21      @param zipFile  the ZipFile
22      @param zipEntry the ZipEntry containing the file to read
23      */
24     public ZipDataSource(ZipFile zipFile, ZipEntry zipEntry) {
25         this.zipFile = zipFile;
26         this.zipEntry = zipEntry;
27     }
28 
29     public InputStream getInputStream() throws IOException {
30         return zipFile.getInputStream(zipEntry);
31     }
32 
33     public String getNiceFileName(boolean shortNames, String inputFileName) {
34         // FIXME: this could probably be done better
35         return zipFile.getName() ":" + zipEntry.getName();
36     }
37 }