Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>You could scan the ZIP file and keep the references to its entries. Then select the entry from the list, extract the file and do whatever you need to do with it. It is not necessary to fully read all files initially. One drawback: You must not call <code>ZipFile.close()</code> until you are finished with processing the ZIP archive.</p> <p>For example:</p> <pre><code>import java.io.BufferedReader; import java.io.File; import java.io.IOException; import java.io.InputStream; import java.io.InputStreamReader; import java.nio.charset.Charset; import java.util.ArrayList; import java.util.Collections; import java.util.Enumeration; import java.util.zip.ZipEntry; import java.util.zip.ZipException; import java.util.zip.ZipFile; public class SO { static class ZipFileRecord { final ZipFile zf; final ZipEntry ze; public ZipFileRecord(ZipFile zf, ZipEntry ze) { super(); this.zf = zf; this.ze = ze; } public String content() throws IOException { InputStream is = zf.getInputStream(ze); BufferedReader br = new BufferedReader(new InputStreamReader(is, Charset.forName("UTF-8"))); StringBuilder sb = new StringBuilder(); String line = null; int count = 5; // limit loop for reduced output while ((line = br.readLine())!=null) { sb.append(line); sb.append("\n"); if (count--&lt;=0) { sb.append("...\n"); break; } } br.close(); return sb.toString(); } } public static class ZipWalker { final ArrayList&lt;ZipFileRecord&gt; list = new ArrayList&lt;&gt;(); ZipFile zf; public void scan(String fn) throws ZipException, IOException { File f = new File(fn); if (!f.exists()) throw new RuntimeException("File not found: " + f); zf = new ZipFile(f); Enumeration&lt;? extends ZipEntry&gt; entries = zf.entries(); for (ZipEntry ze : Collections.list(entries)) { if (ze.getName().endsWith(".java")) { list.add(new ZipFileRecord(zf, ze)); } } } public void dump() throws IOException { int count = 3; // limit loop for reduced output for (ZipFileRecord fr : list) { System.out.println("=============&gt; " + fr.ze); System.out.println(fr.content()); if (count--&lt;=0) break; } zf.close(); } } public static void main(String[] args) throws ZipException, IOException { ZipWalker zw = new ZipWalker(); zw.scan("path/to/your/file.zip"); zw.dump(); } } </code></pre> <p>Noteworthy things:</p> <ol> <li>The <code>scan</code> method does the scanning, the <code>dump</code> method represents all the other things, which you want to do with the files.</li> <li><code>ZipFile.close()</code> must be called after you are finished with the ZIP file. You can't read data from its entries, if <code>close()</code> has been called.</li> <li>The <code>count</code> variables in the code are for output brevity - you need to remove those and the associated <code>if</code>s.</li> <li>For simplicity I used <code>Reader</code> and <code>UTF-8</code>. If you present the file content to the user, you may have to deal with the file's charset. You may also have to deal with it for compilation.</li> </ol>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload