Note that there are some explanatory texts on larger screens.

plurals
  1. POjava.util.zip - Recreating directory structure
    text
    copied!<p>While trying to zip an archive using the <code>java.util.zip</code> I ran into a lot of problems most of which I solved. Now that I finally get some output I struggle with getting the "right" output. I have an extracted ODT file (directory would be more fitting a description) to which I did some modifications. Now I want to compress that directory as to recreate the ODT file structure. Zipping the directory and renaming it to end with .odt works fine so there should be no problem.</p> <p>The main problem is that I lose the internal structure of the directory. Everything becomes "flat" and I do not seem to find a way to preserve the original multi-layered structure. I would appreciate some help on this as I can not seem to find the problem.</p> <p>Here are the relevant code snippets:</p> <pre><code>ZipOutputStream out = new ZipOutputStream(new FileOutputStream( FILEPATH.substring(0, FILEPATH.lastIndexOf(SEPARATOR) + 1).concat("test.zip"))); compressDirectory(TEMPARCH, out); </code></pre> <p>The <code>SEPARATOR</code> is the system file separator and the <code>FILEPATH</code> is the filepath of the original ODT which I will override but have not done here for testing purposes. I simply write to a test.zip file in the same directory.</p> <pre><code>private void compressDirectory(String directory, ZipOutputStream out) throws IOException { File fileToCompress = new File(directory); // list contents. String[] contents = fileToCompress.list(); // iterate through directory and compress files. for(int i = 0; i &lt; contents.length; i++) { File f = new File(directory, contents[i]); // testing type. directories and files have to be treated separately. if(f.isDirectory()) { // add empty directory out.putNextEntry(new ZipEntry(f.getName() + SEPARATOR)); // initiate recursive call compressDirectory(f.getPath(), out); // continue the iteration continue; }else{ // prepare stream to read file. FileInputStream in = new FileInputStream(f); // create ZipEntry and add to outputting stream. out.putNextEntry(new ZipEntry(f.getName())); // write the data. int len; while((len = in.read(data)) &gt; 0) { out.write(data, 0, len); } out.flush(); out.closeEntry(); in.close(); } } } </code></pre> <p>The directory that contains the files to zip is somewhere in the user space and not in the same directory as the resulting file. I assume this could be trouble but I can not really see how. Also I figured that the problem could be in using the same stream for outputting but again I can not see how. I saw in some examples and tutorials that they use <code>getPath()</code> instead of <code>getName()</code> but changing that gives me an empty zip file.</p>
 

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