Java-Helper library for compressing (zipping) files I'm writing a Java-Helper library. I want to include a method to zip files together, and this is what I've come up with. I'm using the java.util.zip.* library (ZipOutputStream and ZipEntry specifically).

/**
* This uses the java.util.zip library to zip the given files to the given destination.
*
* @param destination
* @param files
* @throws FileNotFoundException
* @throws IOException
*/
public static void zipFiles(File destination, File... files) throws FileNotFoundException, IOException {
  try (ZipOutputStream out = new ZipOutputStream(new FileOutputStream(destination))) {
    byte[] buffer = new byte[1024];
    for (int i = 0; i &lt; files.length; i++) {
      try (FileInputStream in = new FileInputStream(files[i])) {
        out.putNextEntry(new ZipEntry(files[i].getName()));

        // Transfer bytes from the file to the ZIP file
        int length;
        while ((length = in.read(buffer)) &gt; 0) {
          out.write(buffer, 0, length);
        }

        // Complete the entry
        out.closeEntry();
      }
    }
  }
}


Any pointers?

Also, from the code, is this a good buffer size?


byte[] buffer = new byte[1024];



I've seen a lot of disks with a blocksize of 512 KB. Would that be a more "optimal" size?
Well, I benchmarked it and found that a buffer size of 10485760 (10 MB) was fastest... Kind of surprised about that and I'm thinking that's not the most efficient. I think I'll leave it at 1 MB unless someone else has any thoughts.