return;
}
}
byte[] buffer = new byte[1024 * 1024];
ZipArchiveOutputStream newZipStream = new ZipArchiveOutputStream(zipArchive);
int zipFileCounter = 0;
for (File currentFile : filesToPackage){
try{
FileInputStream currentFileStream = new FileInputStream(currentFile);
zipFileCounter++;
if (!currentFile.getName().toLowerCase().endsWith(".zip")){
logger.debug("Adding uncompressed file...");
//add this uncompressed file to the archive
int bytesRead;
String entryName = currentFile.getName();
logger.debug("Zipping: " + entryName);
ZipArchiveEntry zipEntry = new ZipArchiveEntry(entryName);
newZipStream.putArchiveEntry(zipEntry);
while ((bytesRead = currentFileStream.read(buffer))!= -1) {
newZipStream.write(buffer, 0, bytesRead);
}
newZipStream.closeArchiveEntry();
} else {
logger.debug("Adding entries from compressed file...");
//read the entries from the zip file and copy them to the new zip archive
//so that we don't have to recompress them.
ZipArchiveInputStream currentZipStream = new ZipArchiveInputStream(currentFileStream);
ArchiveEntry currentEntry;
while ((currentEntry = currentZipStream.getNextEntry()) != null) {
String entryName = currentEntry.getName();
logger.debug("Zipping: " + entryName);
ZipArchiveEntry zipEntry = new ZipArchiveEntry(entryName);
try {
newZipStream.putArchiveEntry(zipEntry);
} catch (Exception e){
//duplicate names should never happen.
entryName = Math.round(Math.random() * 10000) + "_" + entryName;
ZipArchiveEntry zipEntry2 = new ZipArchiveEntry(entryName);
newZipStream.putArchiveEntry(zipEntry2);
}
int bytesRead;
while ((bytesRead = currentZipStream.read(buffer))!= -1) {
newZipStream.write(buffer, 0, bytesRead);
}
newZipStream.closeArchiveEntry();
}
currentZipStream.close();
}
} catch (FileNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} finally {
//always delete the file
logger.debug("Deleting: " + currentFile.getName());
currentFile.delete();
}
}
if (zipFileCounter > 0){
try {
newZipStream.close();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}