/**
* Add a single file to a Zip file.
*
* Zip file may not necessary exist.
* If Zip file exist, then this file will be added to it.
* If Zip file does not exist, then a new Zip file is created with the file mentioned inside it.
*
* ZipPath: Specify the Zip file Path (eg. "C:\DestFolder\MyZipFile.zip")
* FileToAddPath: Specify the file Path of file to add in the Zip file (eg. "C:\MyFile.avi")
* CompressionMethod: Specify compression method constant (eg. COMP_DEFLATE, COMP_STORE, COMP_AES_ENC)
* CompressionLevel: Specify compression level constant for Deflate compression
* (eg. DEFLATE_LEVEL_NORMAL, DEFLATE_LEVEL_FAST, DEFLATE_LEVEL_FASTEST, DEFLATE_LEVEL_MAXIMUM, DEFLATE_LEVEL_ULTRA)
*
* Note: If the Zip file already exists and if this Zip file is a split file
* then this method throws an exception as Zip Format Specification does not
* allow updating split zip files.
*
* Return: True if file was successfully added to the Zip File, else False
*
* Raise Events
* _Progress
* _Error
*/
public boolean AddFile(String ZipPath, String FileToAddPath, int CompressionMethod, int CompressionLevel) {
// Initiate ZipFile object with the path/name of the zip file.
ZipFile zipFile = new ZipFile(ZipPath);
final String evt = eventName + "_progress";
final String errEvt = eventName + "_error";
try {
Percent = 0; oldPercent = 0;
// Set true to monitor progress state
zipFile.setRunInThread(true);
// Initiate Zip Parameters which define various properties such as compression method, etc.
ZipParameters parameters = new ZipParameters();
// Deflate compression or store(no compression) can be set below
parameters.setCompressionMethod(CompressionMethodFromInt(CompressionMethod));
// Set the compression level. This value has to be in between 0 to 9
parameters.setCompressionLevel(CompressionLevelFromInt(CompressionLevel));
// Now add file to the zip file
File file = new File(FileToAddPath);
zipFile.addFile(file, parameters);
// Get progress
if (ba.subExists(evt)) {
ProgressMonitor progressMonitor = zipFile.getProgressMonitor();
while (progressMonitor.getState() == ProgressMonitor.State.BUSY) {
//BA.Log("Processing. Percent done: " + progressMonitor.getPercentDone());
Percent = (int)progressMonitor.getPercentDone();
if (oldPercent != Percent) {
ba.raiseEvent(this, evt, "AddFile", Percent, false);
oldPercent = Percent;
}
}
// HERE OPERATION IS DONE
BA.Log("Zip4j: AddFile -> File added to " + ZipPath +
" Result: " + getResultString(progressMonitor.getResult()));
ba.raiseEvent(this, evt, "AddFile", 100, true);
} else {
BA.Log("Zip4j: AddFile -> Sub " + OriginalEventName +
"_Progress does not exists. Use Async Mode to add file " + FileToAddPath + " to " + ZipPath);
}
return true;
} catch (ZipException e) {
String err = e.getMessage().replace("net.lingala.zip4j.exception.ZipException: net.lingala.zip4j.exception.ZipException: ", "");
err = err.replaceAll(":", "");
//BA.Log("Zip4j: AddFile -> " + err);
if (ba.subExists(errEvt)) {
ba.raiseEvent(this, errEvt, "AddFile", err);
}
return false;
} finally {
try {
zipFile.close();
} catch (IOException e) {
e.printStackTrace();
if (ba.subExists(errEvt)) {
ba.raiseEvent(this, errEvt, "AddFile", e.getMessage());
}
return false;
};
}
}