1
public static BufferedReader fileReaderAsResource(String filePath) throws IOException {
            InputStream is = Thread.currentThread().getContextClassLoader().getResourceAsStream(filePath);
            if (is == null) {
                throw new FileNotFoundException(" Not found: " + filePath);
            }
            return new BufferedReader(new InputStreamReader(is, DEFAULT_ENCODING));
        }

This works for non-zip file. But for zip file, how to return a BufferedReader? The following doesn't work since 'fileName' is a relative path under my 'resources' directory:

public static BufferedReader fileZipReader(String fileName) throws IOException {
        ZipFile zip = new ZipFile(fileName);
        for(Enumeration e = zip.entries(); e.hasMoreElements();){
            ZipEntry zipEntry = (ZipEntry) e.nextElement();
            if(!zipEntry.isDirectory()){
                return new BufferedReader(new InputStreamReader(zip.getInputStream(zipEntry)));
            }
        }
        throw new FileNotFoundException("File not found: " + fileName);
    }

How to change the 'fileZipReader' to make it work?

user697911
  • 10,043
  • 25
  • 95
  • 169
  • *"is a relative path under my 'resources' directory"* - Do you mean it's embedded within you application binary to exists as a stand alone directory from within the working directory context? – MadProgrammer Apr 05 '18 at 04:24
  • src/main/resources – user697911 Apr 05 '18 at 06:03
  • Out so it's an embedded resource, this basically makes it a Zip file in a Zip. You will need to use `Class#getResource` or `Class#getResourceAsStream` and extract the file to a known location – MadProgrammer Apr 05 '18 at 06:08
  • Could you give an answer on this? Thank you. I always got null pointer based on Shailesh suggestion. – user697911 Apr 05 '18 at 06:10
  • [As a conceptual example](https://stackoverflow.com/questions/30430508/java-how-to-open-a-file-located-in-a-jar-file/30430577#30430577) ... you'd have to use `/main/resources` as your path – MadProgrammer Apr 05 '18 at 06:14

1 Answers1

0

Create a URL object giving a relative path to zip file.

public class IOUtil {

    public BufferedReader fileZipReader(String fileName) throws IOException, URISyntaxException {
        URL zipUrl = IOUtils.class.getClassLoader().getResource(fileName);
        File zipFile = new File(zipUrl.toURI());
        ZipFile zip = new ZipFile(zipFile);
        for (Enumeration e = zip.entries(); e.hasMoreElements(); ) {
            ZipEntry zipEntry = (ZipEntry) e.nextElement();
            if (!zipEntry.isDirectory()) {
                return new BufferedReader(new InputStreamReader(zip.getInputStream(zipEntry)));
            }
        }
        throw new FileNotFoundException("File not found: " + fileName);
    }

    public static void main(String[] args) throws Exception {
        IOUtil util = new IOUtil();

        BufferedReader br = util.fileZipReader("dia/test.txt.zip");
    }
}
Shailesh Pratapwar
  • 4,054
  • 3
  • 35
  • 46