My situation is that I have a zip file that contains some files (txt, png, ...) and I want to read it directly by their names, I have tested the following code but no result (NullPointerExcepion):
InputStream in = Main.class.getResourceAsStream("/resouces/zipfile/test.txt");
BufferedReader br = new BufferedReader(new InputStreamReader(in, "UTF-8"));
resources is a package and zipfile is a zip file.
Your current approach is definitely not going to work. You made up an arbitrary 'access' scheme and used it in a class that has no idea what you are trying to do. What you can do is use a ZipInputStream to read the entry you are looking for:
This is adhoc code, fix it up to do what you need. Also, there might be some more efficient classes to handle Zip files, for example in the Apache Commons IO library.
If you can be sure that your zip file will never be packed inside another jar, you can use something like:
Or:
Otherwise, your choices are:
What is the path of the test.txt within the zip file? You need to use the path within the zip file to read this file. Also make sure that your zipfile is in the classpath. In fact, you can bundle this in a jar file.