Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>Try something like this. The parameter useFullFileNames specifies whether you want to preserve the full names of the paths to the files which you're about to zip.</p> <p>So if you have two files <strong>/dir1/dir2/a.txt</strong> and <strong>/dir1/b.txt</strong> the useFullFileNames specifies if you want to finally see in the zip file those original paths to the two files or just the two files with no paths like this <strong>a.txt</strong> and <strong>b.txt</strong> in the root of the zip file which you create.</p> <p>Note that in my example, the files which are zipped are actually read and then written to <strong>out</strong>. I think you're missing that part.</p> <pre><code> public static boolean createZip(String fNameZip, boolean useFullFileNames, String... fNames) throws Exception { try { int cntBufferSize = 256 * 1024; BufferedInputStream origin = null; FileOutputStream dest = new FileOutputStream(fNameZip); ZipOutputStream out = new ZipOutputStream(new BufferedOutputStream(dest)); byte bBuffer[] = new byte[cntBufferSize]; File ftmp = null; for (int i = 0; i &lt; fNames.length; i++) { if (fNames[i] != null) { FileInputStream fi = new FileInputStream(fNames[i]); origin = new BufferedInputStream(fi, cntBufferSize); ftmp = new File(fNames[i]); ZipEntry entry = new ZipEntry(useFullFileNames ? fNames[i] : ftmp.getName()); out.putNextEntry(entry); int count; while ((count = origin.read(bBuffer, 0, cntBufferSize)) != -1) { out.write(bBuffer, 0, count); } origin.close(); } } out.close(); return true; } catch (Exception e) { return false; } } </code></pre>
    singulars
    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