Here you can find the source of addToZip(File f, int truncate, ZipOutputStream os, byte[] buff)
Parameter | Description |
---|---|
f | file or directory to be added |
truncate | truncate name of the file |
os | output stream |
buff | buffer to use in I/O operations |
Parameter | Description |
---|---|
IOException | in case of I/O error |
private static void addToZip(File f, int truncate, ZipOutputStream os, byte[] buff) throws IOException
//package com.java2s; //License from project: LGPL import java.io.BufferedInputStream; import java.io.File; import java.io.FileInputStream; import java.io.IOException; import java.util.zip.ZipEntry; import java.util.zip.ZipOutputStream; public class Main { /**/*from w ww .j a va 2 s. com*/ * Adds file or directory to the ZIP output stream. * * @param f file or directory to be added * @param truncate truncate name of the file * @param os output stream * @param buff buffer to use in I/O operations * @throws IOException in case of I/O error */ private static void addToZip(File f, int truncate, ZipOutputStream os, byte[] buff) throws IOException { if (f.isFile()) { os.putNextEntry(new ZipEntry(f.getAbsolutePath().substring(truncate))); try (FileInputStream fis = new FileInputStream(f); BufferedInputStream bis = new BufferedInputStream(fis)) { int read; while ((read = bis.read(buff)) != -1) { os.write(buff, 0, read); } } os.closeEntry(); } else { for (File sub : f.listFiles()) { addToZip(sub, truncate, os, buff); } } } }