private void zipCompress(String nameOfFile2zip, String nameOfZippedFile, String innerEntryName) {
File file2zip = new File(nameOfFile2zip);
if (!file2zip.exists()) {
addStatus(new WarnStatus("The file to compress named [" + nameOfFile2zip
+ "] does not exist.", this));
return;
}
if (innerEntryName == null) {
addStatus(new WarnStatus("The innerEntryName parameter cannot be null", this));
return;
}
if (!nameOfZippedFile.endsWith(".zip")) {
nameOfZippedFile = nameOfZippedFile + ".zip";
}
File zippedFile = new File(nameOfZippedFile);
if (zippedFile.exists()) {
addStatus(new WarnStatus("The target compressed file named ["
+ nameOfZippedFile + "] exist already.", this));
return;
}
addInfo("ZIP compressing [" + file2zip + "] as ["+zippedFile+"]");
createMissingTargetDirsIfNecessary(zippedFile);
BufferedInputStream bis = null;
ZipOutputStream zos = null;
try {
bis = new BufferedInputStream(new FileInputStream(nameOfFile2zip));
zos = new ZipOutputStream(new FileOutputStream(nameOfZippedFile));
ZipEntry zipEntry = computeZipEntry(innerEntryName);
zos.putNextEntry(zipEntry);
byte[] inbuf = new byte[BUFFER_SIZE];
int n;
while ((n = bis.read(inbuf)) != -1) {
zos.write(inbuf, 0, n);
}
bis.close();
bis = null;
zos.close();
zos = null;
if (!file2zip.delete()) {
addStatus(new WarnStatus("Could not delete [" + nameOfFile2zip + "].",
this));
}
} catch (Exception e) {
addStatus(new ErrorStatus("Error occurred while compressing ["
+ nameOfFile2zip + "] into [" + nameOfZippedFile + "].", this, e));