in plugins/actions/zipfile/src/main/java/org/apache/hop/workflow/actions/zipfile/ActionZipFile.java [222:582]
public boolean processRowFile(
IWorkflowEngine<WorkflowMeta> parentWorkflow,
Result result,
String realZipFilename,
String realWildCard,
String realExcludeWildcard,
String realSourceDirectoryOrFile,
String realMoveToDirectory,
boolean createParentFolder)
throws HopException {
boolean fileExists = false;
File tempFile = null;
File fileZip;
boolean successResult = true;
boolean renameOk = false;
boolean orginExist;
// Check if target file/folder exists!
String localSourceFilename;
try (FileObject originFile = HopVfs.getFileObject(realSourceDirectoryOrFile, getVariables())) {
localSourceFilename = HopVfs.getFilename(originFile);
orginExist = originFile.exists();
} catch (Exception e) {
throw new HopException(
"Error finding source file or directory: " + realSourceDirectoryOrFile, e);
}
String localrealZipfilename = realZipFilename;
if (realZipFilename != null && orginExist) {
try (FileObject fileObject = HopVfs.getFileObject(localrealZipfilename, getVariables())) {
localrealZipfilename = HopVfs.getFilename(fileObject);
// Check if Zip File exists
if (fileObject.exists()) {
fileExists = true;
if (isDebug()) {
logDebug(
BaseMessages.getString(PKG, "ActionZipFile.Zip_FileExists1.Label")
+ localrealZipfilename
+ BaseMessages.getString(PKG, "ActionZipFile.Zip_FileExists2.Label"));
}
}
// Let's see if we need to create parent folder of destination zip filename
if (createParentFolder) {
createParentFolder(localrealZipfilename);
}
// Let's start the process now
if (ifZipFileExists == 3 && fileExists) {
// the zip file exists and user wants this to fail.
successResult = false;
} else if (ifZipFileExists == 2 && fileExists) {
// the zip file exists and user want to do nothing
if (addFileToResult) {
// Add file to result files name
ResultFile resultFile =
new ResultFile(
ResultFile.FILE_TYPE_GENERAL,
fileObject,
parentWorkflow.getWorkflowName(),
toString());
result.getResultFiles().put(resultFile.getFile().toString(), resultFile);
}
} else if (afterZip == 2 && realMoveToDirectory == null) {
// After Zip, Move files..User must give a destination Folder
successResult = false;
logError(
BaseMessages.getString(
PKG, "ActionZipFile.AfterZip_No_DestinationFolder_Defined.Label"));
} else {
// After Zip, Move files..User must give a destination Folder
// Let's see if we deal with file or folder
FileObject[] fileList;
FileObject sourceFileOrFolder = HopVfs.getFileObject(localSourceFilename, getVariables());
boolean isSourceDirectory = sourceFileOrFolder.getType().equals(FileType.FOLDER);
final Pattern pattern;
final Pattern excludePattern;
if (isSourceDirectory) {
// Let's prepare the pattern matcher for performance reasons.
// We only do this if the target is a folder !
//
if (!Utils.isEmpty(realWildCard)) {
pattern = Pattern.compile(realWildCard);
} else {
pattern = null;
}
if (!Utils.isEmpty(realExcludeWildcard)) {
excludePattern = Pattern.compile(realExcludeWildcard);
} else {
excludePattern = null;
}
// Target is a directory
// Get all the files in the directory...
//
if (includingSubFolders) {
fileList =
sourceFileOrFolder.findFiles(
new ZipJobEntryPatternFileSelector(pattern, excludePattern));
} else {
fileList = sourceFileOrFolder.getChildren();
}
} else {
pattern = null;
excludePattern = null;
// Target is a file
fileList = new FileObject[] {sourceFileOrFolder};
}
if (fileList.length == 0) {
successResult = false;
logError(
BaseMessages.getString(
PKG, "ActionZipFile.Log.FolderIsEmpty", localSourceFilename));
} else if (!checkContainsFile(localSourceFilename, fileList, isSourceDirectory)) {
successResult = false;
logError(
BaseMessages.getString(
PKG, "ActionZipFile.Log.NoFilesInFolder", localSourceFilename));
} else {
if (ifZipFileExists == 0 && fileExists) {
// the zip file exists and user want to create new one with unique name
// Format Date
// do we have already a .zip at the end?
if (localrealZipfilename.toLowerCase().endsWith(".zip")) {
// strip this off
localrealZipfilename =
localrealZipfilename.substring(0, localrealZipfilename.length() - 4);
}
localrealZipfilename += "_" + StringUtil.getFormattedDateTimeNow(true) + ".zip";
if (isDebug()) {
logDebug(
BaseMessages.getString(PKG, "ActionZipFile.Zip_FileNameChange1.Label")
+ localrealZipfilename
+ BaseMessages.getString(PKG, "ActionZipFile.Zip_FileNameChange1.Label"));
}
} else if (ifZipFileExists == 1 && fileExists) {
// the zip file exists and user want to append
// get a temp file
fileZip = getFile(localrealZipfilename);
tempFile = File.createTempFile(fileZip.getName(), null);
// delete it, otherwise we cannot rename existing zip to it.
if (!tempFile.delete()) {
throw new HopException("Unable to delete temporary file " + tempFile);
}
renameOk = fileZip.renameTo(tempFile);
if (!renameOk) {
logError(
BaseMessages.getString(PKG, "ActionZipFile.Cant_Rename_Temp1.Label")
+ fileZip.getAbsolutePath()
+ BaseMessages.getString(PKG, "ActionZipFile.Cant_Rename_Temp2.Label")
+ tempFile.getAbsolutePath()
+ BaseMessages.getString(PKG, "ActionZipFile.Cant_Rename_Temp3.Label"));
}
if (isDebug()) {
logDebug(
BaseMessages.getString(PKG, "ActionZipFile.Zip_FileAppend1.Label")
+ localrealZipfilename
+ BaseMessages.getString(PKG, "ActionZipFile.Zip_FileAppend2.Label"));
}
}
if (isDetailed()) {
logDetailed(
BaseMessages.getString(PKG, "ActionZipFile.Files_Found1.Label")
+ fileList.length
+ BaseMessages.getString(PKG, "ActionZipFile.Files_Found2.Label")
+ localSourceFilename
+ BaseMessages.getString(PKG, "ActionZipFile.Files_Found3.Label"));
}
// Keep track of the files added to the zip archive.
//
List<FileObject> zippedFiles = new ArrayList<>();
// Prepare Zip File
try (OutputStream dest =
HopVfs.getOutputStream(localrealZipfilename, false, getVariables())) {
try (BufferedOutputStream buff = new BufferedOutputStream(dest)) {
try (ZipOutputStream out = new ZipOutputStream(buff)) {
HashSet<String> fileSet = new HashSet<>();
if (renameOk) {
// User want to append files to existing Zip file
// The idea is to rename the existing zip file to a temporary file
// and then adds all entries in the existing zip along with the new files,
// excluding the zip entries that have the same name as one of the new files.
//
moveRenameZipArchive(tempFile, fileSet, out);
}
// Set the method
out.setMethod(ZipOutputStream.DEFLATED);
// Set the compression level
if (compressionRate == 0) {
out.setLevel(Deflater.NO_COMPRESSION);
} else if (compressionRate == 1) {
out.setLevel(Deflater.DEFAULT_COMPRESSION);
}
if (compressionRate == 2) {
out.setLevel(Deflater.BEST_COMPRESSION);
}
if (compressionRate == 3) {
out.setLevel(Deflater.BEST_SPEED);
}
// Specify Zipped files (After that we will move,delete them...)
int fileNum = 0;
// Get the files in the list...
for (int i = 0; i < fileList.length && !parentWorkflow.isStopped(); i++) {
boolean getIt = true;
boolean getItexclude = false;
// First see if the file matches the regular expression.
// Do this only if the target is a folder.
//
if (isSourceDirectory) {
// If we include sub-folders, we match on the whole name, not just the
// basename
//
String filename;
if (includingSubFolders) {
filename = fileList[i].getName().getPath();
} else {
filename = fileList[i].getName().getBaseName();
}
if (pattern != null) {
// Matches the base name of the file (backward compatible!)
//
Matcher matcher = pattern.matcher(filename);
getIt = matcher.matches();
}
if (excludePattern != null) {
Matcher excludeMatcher = excludePattern.matcher(filename);
getItexclude = excludeMatcher.matches();
}
}
// Get processing File
//
String targetFilename = HopVfs.getFilename(fileList[i]);
if (sourceFileOrFolder.getType().equals(FileType.FILE)) {
targetFilename = localSourceFilename;
}
try (FileObject file = HopVfs.getFileObject(targetFilename, getVariables())) {
boolean isTargetDirectory =
file.exists() && file.getType().equals(FileType.FOLDER);
if (getIt
&& !getItexclude
&& !isTargetDirectory
&& !fileSet.contains(targetFilename)) {
// We can add the file to the Zip Archive
if (isDebug()) {
logDebug(
BaseMessages.getString(PKG, "ActionZipFile.Add_FilesToZip1.Label")
+ fileList[i]
+ BaseMessages.getString(
PKG, "ActionZipFile.Add_FilesToZip2.Label")
+ localSourceFilename
+ BaseMessages.getString(
PKG, "ActionZipFile.Add_FilesToZip3.Label"));
}
// Associate a file input stream for the current file.
//
addFileToZip(file, fileList, i, sourceFileOrFolder, isSourceDirectory, out);
// Get Zipped File
zippedFiles.add(fileList[i]);
fileNum = fileNum + 1;
}
}
}
}
}
}
if (isBasic()) {
logBasic(
BaseMessages.getString(
PKG, "ActionZipFile.Log.TotalZippedFiles", "" + zippedFiles.size()));
}
// Delete Temp File
if (tempFile != null) {
if (!tempFile.delete()) {
throw new HopException("Unable to delete temporary file " + tempFile);
}
}
// -----Get the list of Zipped Files and Move or Delete Them
if (afterZip == 1 || afterZip == 2) {
// iterate through the array of Zipped files
for (FileObject fileObjectd : zippedFiles) {
// Delete, Move File
if (!isSourceDirectory) {
fileObjectd = HopVfs.getFileObject(localSourceFilename, getVariables());
}
// Here we can move, delete files
if (afterZip == 1) {
successResult = deleteFile(successResult, fileObjectd, localSourceFilename);
} else if (afterZip == 2) {
successResult = moveFile(successResult, fileObjectd, realMoveToDirectory);
}
// We no longer need this file, close it.
//
fileObjectd.close();
}
}
if (addFileToResult) {
// Add file to result files name
ResultFile resultFile =
new ResultFile(
ResultFile.FILE_TYPE_GENERAL,
fileObject,
parentWorkflow.getWorkflowName(),
toString());
result.getResultFiles().put(resultFile.getFile().toString(), resultFile);
}
successResult = true;
}
}
} catch (Exception e) {
logError(
BaseMessages.getString(PKG, "ActionZipFile.Cant_CreateZipFile1.Label")
+ localrealZipfilename
+ BaseMessages.getString(PKG, "ActionZipFile.Cant_CreateZipFile2.Label"),
e);
successResult = false;
}
} else {
successResult = false;
if (localrealZipfilename == null) {
logError(BaseMessages.getString(PKG, "ActionZipFile.No_ZipFile_Defined.Label"));
}
if (!orginExist) {
logError(
BaseMessages.getString(
PKG, "ActionZipFile.No_FolderCible_Defined.Label", localSourceFilename));
}
}
// return a verifier
return successResult;
}