Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>I see 2 problems in your code,</p> <ol> <li>You don't save the directory path so there is no way to get it back.</li> <li>On Windows, you need to use "/" as path separator. Some unzip program doesn't like \.</li> </ol> <p>I include my own version for your reference. We use this one to zip up photos to download so it works with various unzip programs. It preserves the directory structure and timestamps. </p> <pre><code> public static void createZipFile(File srcDir, OutputStream out, boolean verbose) throws IOException { List&lt;String&gt; fileList = listDirectory(srcDir); ZipOutputStream zout = new ZipOutputStream(out); zout.setLevel(9); zout.setComment("Zipper v1.2"); for (String fileName : fileList) { File file = new File(srcDir.getParent(), fileName); if (verbose) System.out.println(" adding: " + fileName); // Zip always use / as separator String zipName = fileName; if (File.separatorChar != '/') zipName = fileName.replace(File.separatorChar, '/'); ZipEntry ze; if (file.isFile()) { ze = new ZipEntry(zipName); ze.setTime(file.lastModified()); zout.putNextEntry(ze); FileInputStream fin = new FileInputStream(file); byte[] buffer = new byte[4096]; for (int n; (n = fin.read(buffer)) &gt; 0;) zout.write(buffer, 0, n); fin.close(); } else { ze = new ZipEntry(zipName + '/'); ze.setTime(file.lastModified()); zout.putNextEntry(ze); } } zout.close(); } public static List&lt;String&gt; listDirectory(File directory) throws IOException { Stack&lt;String&gt; stack = new Stack&lt;String&gt;(); List&lt;String&gt; list = new ArrayList&lt;String&gt;(); // If it's a file, just return itself if (directory.isFile()) { if (directory.canRead()) list.add(directory.getName()); return list; } // Traverse the directory in width-first manner, no-recursively String root = directory.getParent(); stack.push(directory.getName()); while (!stack.empty()) { String current = (String) stack.pop(); File curDir = new File(root, current); String[] fileList = curDir.list(); if (fileList != null) { for (String entry : fileList) { File f = new File(curDir, entry); if (f.isFile()) { if (f.canRead()) { list.add(current + File.separator + entry); } else { System.err.println("File " + f.getPath() + " is unreadable"); throw new IOException("Can't read file: " + f.getPath()); } } else if (f.isDirectory()) { list.add(current + File.separator + entry); stack.push(current + File.separator + f.getName()); } else { throw new IOException("Unknown entry: " + f.getPath()); } } } } return list; } } </code></pre>
    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. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      1. This table or related slice is empty.
    3. VO
      singulars
      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