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 < 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)) > 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?