in ch-commons-util/src/main/java/com/cloudhopper/commons/util/CompressionUtil.java [449:490]
public void compress(File srcFile, File destFile) throws IOException {
FileInputStream in = null;
ZipOutputStream out = null;
try {
// create an input stream from the source file
in = new FileInputStream(srcFile);
// create an output stream that's gzipped
out = new ZipOutputStream(new FileOutputStream(destFile));
// add a new zip entry to the output stream
out.putNextEntry(new ZipEntry(srcFile.getName()));
// transfer bytes from the input file to the zip output stream
byte[] buf = new byte[1024];
int len;
while ((len = in.read(buf)) > 0) {
out.write(buf, 0, len);
}
// close the input stream
in.close();
in = null;
// finish and close the gzip file
out.closeEntry();
out.finish();
out.close();
out = null;
} finally {
// always make sure the input stream is closed!
if (in != null) {
try { in.close(); } catch (Exception e) { }
}
// if the out var is not null, then something went wrong above
// and we did not compress the destination file correctly
if (out != null) {
logger.warn("Output stream for ZIP compressed file was not null -- indicates error with compression occurred");
try { out.close(); } catch (Exception e) { }
}
}
}