List of usage examples for org.apache.commons.vfs FileObject delete
public boolean delete() throws FileSystemException;
From source file:com.panet.imeta.trans.steps.blockingstep.BlockingStep.java
private Object[] getBuffer() { Object[] retval;//from w ww .j a v a 2s .c om // Open all files at once and read one row from each file... if (data.files.size() > 0 && (data.dis.size() == 0 || data.fis.size() == 0)) { if (log.isBasic()) logBasic(Messages.getString("BlockingStep.Log.Openfiles")); try { FileObject fileObject = (FileObject) data.files.get(0); String filename = KettleVFS.getFilename(fileObject); if (log.isDetailed()) logDetailed(Messages.getString("BlockingStep.Log.Openfilename1") + filename + Messages.getString("BlockingStep.Log.Openfilename2")); InputStream fi = KettleVFS.getInputStream(fileObject); DataInputStream di; data.fis.add(fi); if (meta.getCompress()) { GZIPInputStream gzfi = new GZIPInputStream(new BufferedInputStream(fi)); di = new DataInputStream(gzfi); data.gzis.add(gzfi); } else { di = new DataInputStream(fi); } data.dis.add(di); // How long is the buffer? int buffersize = di.readInt(); if (log.isDetailed()) logDetailed(Messages.getString("BlockingStep.Log.BufferSize1") + filename + Messages.getString("BlockingStep.Log.BufferSize2") + buffersize + " " + Messages.getString("BlockingStep.Log.BufferSize3")); if (buffersize > 0) { // Read a row from temp-file data.rowbuffer.add(data.outputRowMeta.readData(di)); } } catch (Exception e) { logError(Messages.getString("BlockingStepMeta.ErrorReadingFile") + e.toString()); logError(Const.getStackTracker(e)); } } if (data.files.size() == 0) { if (data.buffer.size() > 0) { retval = (Object[]) data.buffer.get(0); data.buffer.remove(0); } else { retval = null; } } else { if (data.rowbuffer.size() == 0) { retval = null; } else { retval = (Object[]) data.rowbuffer.get(0); data.rowbuffer.remove(0); // now get another FileObject file = (FileObject) data.files.get(0); DataInputStream di = (DataInputStream) data.dis.get(0); InputStream fi = (InputStream) data.fis.get(0); GZIPInputStream gzfi = (meta.getCompress()) ? (GZIPInputStream) data.gzis.get(0) : null; try { data.rowbuffer.add(0, data.outputRowMeta.readData(di)); } catch (SocketTimeoutException e) { logError(Messages.getString("System.Log.UnexpectedError") + " : " + e.toString()); //$NON-NLS-1$ //$NON-NLS-2$ logError(Const.getStackTracker(e)); setErrors(1); stopAll(); } catch (KettleFileException fe) // empty file or EOF mostly { try { di.close(); fi.close(); if (gzfi != null) gzfi.close(); file.delete(); } catch (IOException e) { logError(Messages.getString("BlockingStepMeta.UnableDeleteFile") + file.toString()); setErrors(1); stopAll(); return null; } data.files.remove(0); data.dis.remove(0); data.fis.remove(0); if (gzfi != null) data.gzis.remove(0); } } } return retval; }
From source file:com.panet.imeta.job.entries.ssh2put.JobEntrySSH2PUT.java
private boolean deleteOrMoveFiles(FileObject file, String destinationFolder) throws KettleException { try {//from ww w . j a v a 2 s. com boolean retval = false; // Delete the file if this is needed! // if (afterFtpPut.equals("delete_file")) { file.delete(); retval = true; if (log.isDetailed()) log.logDetailed(toString(), Messages.getString("JobSSH2PUT.Log.DeletedFile", file.toString())); } else if (afterFtpPut.equals("move_file")) { // Move File FileObject destination = null; FileObject source = null; try { destination = KettleVFS .getFileObject(destinationFolder + Const.FILE_SEPARATOR + file.getName().getBaseName()); file.moveTo(destination); retval = true; } catch (Exception e) { log.logError(toString(), Messages.getString("JobSSH2PUT.Cant_Move_File.Label", file.toString(), destinationFolder, e.getMessage())); } finally { if (destination != null) { try { destination.close(); } catch (Exception ex) { } ; } if (source != null) { try { source.close(); } catch (Exception ex) { } ; } } if (log.isDetailed()) log.logDetailed(toString(), Messages.getString("JobSSH2PUT.Log.MovedFile", file.toString(), ftpDirectory)); } return retval; } catch (Exception e) { throw new KettleException(e); } }
From source file:com.panet.imeta.job.entries.deletefile.JobEntryDeleteFile.java
public Result execute(Result previousResult, int nr, Repository rep, Job parentJob) { LogWriter log = LogWriter.getInstance(); Result result = previousResult; result.setResult(false);/* w w w.j a v a 2s . c om*/ if (filename != null) { String realFilename = getRealFilename(); FileObject fileObject = null; try { fileObject = KettleVFS.getFileObject(realFilename); if (!fileObject.exists()) { if (isFailIfFileNotExists()) { // File doesn't exist and fail flag is on. result.setResult(false); log.logError(toString(), Messages .getString("JobEntryDeleteFile.ERROR_0004_File_Does_Not_Exist", realFilename)); //$NON-NLS-1$ } else { // File already deleted, no reason to try to delete it result.setResult(true); if (log.isBasic()) log.logBasic(toString(), Messages.getString("JobEntryDeleteFile.File_Already_Deleted", realFilename)); //$NON-NLS-1$ } } else { // Here gc() is explicitly called if e.g. createfile is used // in the same // job for the same file. The problem is that after creating // the file the // file object is not properly garbaged collected and thus // the file cannot // be deleted anymore. This is a known problem in the JVM. System.gc(); boolean deleted = fileObject.delete(); if (!deleted) { log.logError(toString(), Messages .getString("JobEntryDeleteFile.ERROR_0005_Could_Not_Delete_File", realFilename)); //$NON-NLS-1$ result.setResult(false); result.setNrErrors(1); } if (log.isBasic()) log.logBasic(toString(), Messages.getString("JobEntryDeleteFile.File_Deleted", realFilename)); //$NON-NLS-1$ result.setResult(true); } } catch (IOException e) { log.logError(toString(), Messages.getString("JobEntryDeleteFile.ERROR_0006_Exception_Deleting_File", //$NON-NLS-1$ realFilename, e.getMessage())); result.setResult(false); result.setNrErrors(1); } finally { if (fileObject != null) { try { fileObject.close(); } catch (IOException ex) { } ; } } } else { log.logError(toString(), Messages.getString("JobEntryDeleteFile.ERROR_0007_No_Filename_Is_Defined")); //$NON-NLS-1$ } return result; }
From source file:com.panet.imeta.trans.steps.sort.SortRows.java
private Object[] getBuffer() throws KettleValueException { Object[] retval;/* w w w.ja v a2 s . c o m*/ // Open all files at once and read one row from each file... if (data.files.size() > 0 && (data.dis.size() == 0 || data.fis.size() == 0)) { if (log.isBasic()) logBasic("Opening " + data.files.size() + " tmp-files..."); try { for (int f = 0; f < data.files.size() && !isStopped(); f++) { FileObject fileObject = (FileObject) data.files.get(f); String filename = KettleVFS.getFilename(fileObject); if (log.isDetailed()) logDetailed("Opening tmp-file: [" + filename + "]"); InputStream fi = KettleVFS.getInputStream(fileObject); DataInputStream di; data.fis.add(fi); if (data.compressFiles) { GZIPInputStream gzfi = new GZIPInputStream(new BufferedInputStream(fi)); di = new DataInputStream(gzfi); data.gzis.add(gzfi); } else { di = new DataInputStream(new BufferedInputStream(fi, 50000)); } data.dis.add(di); // How long is the buffer? int buffersize = data.bufferSizes.get(f); if (log.isDetailed()) logDetailed("[" + filename + "] expecting " + buffersize + " rows..."); if (buffersize > 0) { Object[] row = (Object[]) data.outputRowMeta.readData(di); data.rowbuffer.add(row); // new row from input stream data.tempRows.add(new RowTempFile(row, f)); } } // Sort the data row buffer Collections.sort(data.tempRows, data.comparator); } catch (Exception e) { logError("Error reading back tmp-files : " + e.toString()); logError(Const.getStackTracker(e)); } } if (data.files.size() == 0) { if (data.getBufferIndex < data.buffer.size()) { retval = (Object[]) data.buffer.get(data.getBufferIndex); data.getBufferIndex++; } else { retval = null; } } else { if (data.rowbuffer.size() == 0) { retval = null; } else { // We now have "filenr" rows waiting: which one is the smallest? // if (log.isRowLevel()) { for (int i = 0; i < data.rowbuffer.size() && !isStopped(); i++) { Object[] b = (Object[]) data.rowbuffer.get(i); logRowlevel("--BR#" + i + ": " + data.outputRowMeta.getString(b)); } } RowTempFile rowTempFile = data.tempRows.remove(0); retval = rowTempFile.row; int smallest = rowTempFile.fileNumber; // now get another Row for position smallest FileObject file = (FileObject) data.files.get(smallest); DataInputStream di = (DataInputStream) data.dis.get(smallest); InputStream fi = (InputStream) data.fis.get(smallest); GZIPInputStream gzfi = (data.compressFiles) ? (GZIPInputStream) data.gzis.get(smallest) : null; try { Object[] row2 = (Object[]) data.outputRowMeta.readData(di); RowTempFile extra = new RowTempFile(row2, smallest); int index = Collections.binarySearch(data.tempRows, extra, data.comparator); if (index < 0) { data.tempRows.add(index * (-1) - 1, extra); } else { data.tempRows.add(index, extra); } } catch (KettleFileException fe) // empty file or EOF mostly { try { di.close(); fi.close(); if (gzfi != null) gzfi.close(); file.delete(); } catch (IOException e) { logError("Unable to close/delete file #" + smallest + " --> " + file.toString()); setErrors(1); stopAll(); return null; } data.files.remove(smallest); data.dis.remove(smallest); data.fis.remove(smallest); if (gzfi != null) data.gzis.remove(smallest); // Also update all file numbers in in data.tempRows if they are larger than smallest. // for (RowTempFile rtf : data.tempRows) { if (rtf.fileNumber > smallest) rtf.fileNumber--; } } catch (SocketTimeoutException e) { throw new KettleValueException(e); // should never happen on local files } } } return retval; }
From source file:com.akretion.kettle.steps.terminatooor.ScriptValuesAddedFunctions.java
public static void deleteFile(ScriptEngine actualContext, Bindings actualObject, Object[] ArgList, Object FunctionContext) { try {//w w w.j a v a2 s .c o m if (ArgList.length == 1 && !isNull(ArgList[0]) && !isUndefined(ArgList[0])) { //Object act = actualObject.get("_step_", actualObject); //ScriptValuesMod act = (ScriptValuesMod)Context.toType(scm_delete, ScriptValuesMod.class); FileObject fileObject = null; try { fileObject = KettleVFS.getFileObject((String) ArgList[0]); if (fileObject.exists()) { if (fileObject.getType() == FileType.FILE) { if (!fileObject.delete()) new RuntimeException("We can not delete file [" + (String) ArgList[0] + "]!"); } } else { new RuntimeException("file [" + (String) ArgList[0] + "] can not be found!"); } } catch (IOException e) { throw new RuntimeException("The function call deleteFile is not valid."); } finally { if (fileObject != null) try { fileObject.close(); } catch (Exception e) { } } } else { throw new RuntimeException("The function call deleteFile is not valid."); } } catch (Exception e) { throw new RuntimeException(e.toString()); } }
From source file:com.panet.imeta.trans.steps.scriptvalues_mod.ScriptValuesAddedFunctions.java
public static void deleteFile(Context actualContext, Scriptable actualObject, Object[] ArgList, Function FunctionContext) { try {// w w w. java 2 s . c om if (ArgList.length == 1 && !isNull(ArgList[0]) && !isUndefined(ArgList[0])) { //Object act = actualObject.get("_step_", actualObject); //ScriptValuesMod act = (ScriptValuesMod)Context.toType(scm_delete, ScriptValuesMod.class); FileObject fileObject = null; try { fileObject = KettleVFS.getFileObject(Context.toString(ArgList[0])); if (fileObject.exists()) { if (fileObject.getType() == FileType.FILE) { if (!fileObject.delete()) Context.reportRuntimeError( "We can not delete file [" + Context.toString(ArgList[0]) + "]!"); } } else { Context.reportRuntimeError("file [" + Context.toString(ArgList[0]) + "] can not be found!"); } } catch (IOException e) { throw Context.reportRuntimeError("The function call deleteFile is not valid."); } finally { if (fileObject != null) try { fileObject.close(); } catch (Exception e) { } } } else { throw Context.reportRuntimeError("The function call deleteFile is not valid."); } } catch (Exception e) { throw Context.reportRuntimeError(e.toString()); } }
From source file:com.panet.imeta.job.entries.sftpput.JobEntrySFTPPUT.java
public Result execute(Result previousResult, int nr, Repository rep, Job parentJob) throws KettleException { LogWriter log = LogWriter.getInstance(); Result result = previousResult; List<RowMetaAndData> rows = result.getRows(); result.setResult(false);// ww w. ja v a 2 s. co m if (log.isDetailed()) log.logDetailed(toString(), Messages.getString("JobSFTPPUT.Log.StartJobEntry")); ArrayList<FileObject> myFileList = new ArrayList<FileObject>(); if (copyprevious) { if (rows.size() == 0) { if (log.isDetailed()) log.logDetailed(toString(), Messages.getString("JobSFTPPUT.ArgsFromPreviousNothing")); result.setResult(true); return result; } try { RowMetaAndData resultRow = null; // Copy the input row to the (command line) arguments for (int iteration = 0; iteration < rows.size(); iteration++) { resultRow = rows.get(iteration); // Get file names String file_previous = resultRow.getString(0, null); if (!Const.isEmpty(file_previous)) { FileObject file = KettleVFS.getFileObject(file_previous); if (!file.exists()) log.logError(toString(), Messages.getString("JobSFTPPUT.Log.FilefromPreviousNotFound", file_previous)); else { myFileList.add(file); if (log.isDebug()) log.logDebug(toString(), Messages.getString("JobSFTPPUT.Log.FilenameFromResult", file_previous)); } } } } catch (Exception e) { log.logError(toString(), Messages.getString("JobSFTPPUT.Error.ArgFromPrevious")); result.setNrErrors(1); return result; } } SFTPClient sftpclient = null; // String substitution.. String realServerName = environmentSubstitute(serverName); String realServerPort = environmentSubstitute(serverPort); String realUsername = environmentSubstitute(userName); String realPassword = environmentSubstitute(password); String realSftpDirString = environmentSubstitute(sftpDirectory); String realWildcard = environmentSubstitute(wildcard); String realLocalDirectory = environmentSubstitute(localDirectory); try { // Create sftp client to host ... sftpclient = new SFTPClient(InetAddress.getByName(realServerName), Const.toInt(realServerPort, 22), realUsername); if (log.isDetailed()) log.logDetailed(toString(), Messages.getString("JobSFTPPUT.Log.OpenedConnection", realServerName, "" + realServerPort, realUsername)); // login to ftp host ... sftpclient.login(realPassword); // Don't show the password in the logs, it's not good for security // audits // log.logDetailed(toString(), "logged in using password // "+realPassword); // Logging this seems a bad idea! Oh well. // move to spool dir ... if (!Const.isEmpty(realSftpDirString)) { sftpclient.chdir(realSftpDirString); if (log.isDetailed()) log.logDetailed(toString(), Messages.getString("JobSFTPPUT.Log.ChangedDirectory", realSftpDirString)); } // end if if (!copyprevious) { // Get all the files in the local directory... myFileList = new ArrayList<FileObject>(); FileObject localFiles = KettleVFS.getFileObject(realLocalDirectory); FileObject[] children = localFiles.getChildren(); if (children != null) { for (int i = 0; i < children.length; i++) { // Get filename of file or directory if (children[i].getType().equals(FileType.FILE)) { // myFileList.add(children[i].getAbsolutePath()); myFileList.add(children[i]); } } // end for } } if (myFileList == null || myFileList.size() == 0) { log.logError(toString(), Messages.getString("JobSFTPPUT.Error.NoFileToSend")); result.setNrErrors(1); return result; } if (log.isDetailed()) log.logDetailed(toString(), Messages.getString("JobSFTPPUT.Log.RowsFromPreviousResult", "" + myFileList.size())); Pattern pattern = null; if (!copyprevious) { if (!Const.isEmpty(realWildcard)) { pattern = Pattern.compile(realWildcard); } } // Get the files in the list and execute sftp.put() for each file for (int i = 0; i < myFileList.size() && !parentJob.isStopped(); i++) { FileObject myFile = myFileList.get(i); String localFilename = myFile.toString(); String destinationFilename = myFile.getName().getBaseName(); boolean getIt = true; // First see if the file matches the regular expression! if (pattern != null) { Matcher matcher = pattern.matcher(destinationFilename); getIt = matcher.matches(); } if (getIt) { if (log.isDebug()) log.logDebug(toString(), Messages.getString("JobSFTPPUT.Log.PuttingFile", localFilename, realSftpDirString)); sftpclient.put(myFile, destinationFilename); if (log.isDetailed()) log.logDetailed(toString(), Messages.getString("JobSFTPPUT.Log.TransferedFile", localFilename)); // Delete the file if this is needed! if (remove) { myFile.delete(); if (log.isDetailed()) log.logDetailed(toString(), Messages.getString("JobSFTPPUT.Log.DeletedFile", localFilename)); } else { if (addFilenameResut) { // Add to the result files... ResultFile resultFile = new ResultFile(ResultFile.FILE_TYPE_GENERAL, myFile, parentJob.getJobname(), toString()); result.getResultFiles().put(resultFile.getFile().toString(), resultFile); if (log.isDetailed()) log.logDetailed(toString(), Messages .getString("JobSFTPPUT.Log.FilenameAddedToResultFilenames", localFilename)); } } } } // end for result.setResult(true); // JKU: no idea if this is needed...! // result.setNrFilesRetrieved(filesRetrieved); } // end try catch (Exception e) { result.setNrErrors(1); log.logError(toString(), Messages.getString("JobSFTPPUT.Exception", e.getMessage())); log.logError(toString(), Const.getStackTracker(e)); } finally { // close connection, if possible try { if (sftpclient != null) sftpclient.disconnect(); } catch (Exception e) { // just ignore this, makes no big difference } // end catch } // end finallly return result; }
From source file:com.panet.imeta.job.entries.zipfile.JobEntryZipFile.java
public boolean processRowFile(Job parentJob, Result result, String realZipfilename, String realWildcard, String realWildcardExclude, String realTargetdirectory, String realMovetodirectory, boolean createparentfolder) { LogWriter log = LogWriter.getInstance(); boolean Fileexists = false; File tempFile = null;/*from w ww .ja va 2 s . c o m*/ File fileZip = null; boolean resultat = false; boolean renameOk = false; boolean orginexist = false; // Check if target file/folder exists! FileObject OriginFile = null; ZipInputStream zin = null; byte[] buffer = null; FileOutputStream dest = null; BufferedOutputStream buff = null; org.apache.tools.zip.ZipOutputStream out = null; org.apache.tools.zip.ZipEntry entry = null; try { OriginFile = KettleVFS.getFileObject(realTargetdirectory); orginexist = OriginFile.exists(); } catch (Exception e) { } finally { if (OriginFile != null) { try { OriginFile.close(); } catch (IOException ex) { } ; } } if (realZipfilename != null && orginexist) { FileObject fileObject = null; try { fileObject = KettleVFS.getFileObject(realZipfilename); // Check if Zip File exists if (fileObject.exists()) { Fileexists = true; if (log.isDebug()) log.logDebug(toString(), Messages.getString("JobZipFiles.Zip_FileExists1.Label") + realZipfilename + Messages.getString("JobZipFiles.Zip_FileExists2.Label")); } // Let's see if we need to create parent folder of destination // zip filename if (createparentfolder) { createParentFolder(realZipfilename); } // Let's start the process now if (ifzipfileexists == 3 && Fileexists) { // the zip file exists and user want to Fail resultat = 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, KettleVFS.getFileObject(realZipfilename), parentJob.getJobname(), toString()); result.getResultFiles().put(resultFile.getFile().toString(), resultFile); } resultat = true; } else if (afterzip == 2 && realMovetodirectory == null) { // After Zip, Move files..User must give a destination // Folder resultat = false; log.logError(toString(), Messages.getString("JobZipFiles.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 String[] filelist = null; File f = new File(realTargetdirectory); if (f.isDirectory()) { // Target is a directory // Get all the files in the directory... filelist = f.list(); } else { // Target is a file filelist = new String[1]; filelist[0] = f.getName(); } if (filelist.length == 0) { resultat = false; log.logError(toString(), Messages.getString("JobZipFiles.Log.FolderIsEmpty", realTargetdirectory)); } else if (!checkContainsFile(realTargetdirectory, filelist)) { resultat = false; log.logError(toString(), Messages.getString("JobZipFiles.Log.NoFilesInFolder", realTargetdirectory)); } 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 (realZipfilename.toLowerCase().endsWith(".zip")) { // strip this off realZipfilename = realZipfilename.substring(0, realZipfilename.length() - 4); } realZipfilename = realZipfilename + "_" + StringUtil.getFormattedDateTimeNow(true) + ".zip"; if (log.isDebug()) log.logDebug(toString(), Messages.getString("JobZipFiles.Zip_FileNameChange1.Label") + realZipfilename + Messages.getString("JobZipFiles.Zip_FileNameChange1.Label")); } else if (ifzipfileexists == 1 && Fileexists) { // the zip file exists and user want to append // get a temp file fileZip = new File(realZipfilename); tempFile = File.createTempFile(fileZip.getName(), null); // delete it, otherwise we cannot rename existing // zip to it. tempFile.delete(); renameOk = fileZip.renameTo(tempFile); if (!renameOk) { log.logError(toString(), Messages.getString("JobZipFiles.Cant_Rename_Temp1.Label") + fileZip.getAbsolutePath() + Messages.getString("JobZipFiles.Cant_Rename_Temp2.Label") + tempFile.getAbsolutePath() + Messages.getString("JobZipFiles.Cant_Rename_Temp3.Label")); } if (log.isDebug()) log.logDebug(toString(), Messages.getString("JobZipFiles.Zip_FileAppend1.Label") + realZipfilename + Messages.getString("JobZipFiles.Zip_FileAppend2.Label")); } if (log.isDetailed()) log.logDetailed(toString(), Messages.getString("JobZipFiles.Files_Found1.Label") + filelist.length + Messages.getString("JobZipFiles.Files_Found2.Label") + realTargetdirectory + Messages.getString("JobZipFiles.Files_Found3.Label")); Pattern pattern = null; Pattern patternexclude = null; // Let's prepare pattern..only if target is a folder ! if (f.isDirectory()) { if (!Const.isEmpty(realWildcard)) { pattern = Pattern.compile(realWildcard); } if (!Const.isEmpty(realWildcardExclude)) { patternexclude = Pattern.compile(realWildcardExclude); } } // Prepare Zip File buffer = new byte[18024]; dest = new FileOutputStream(realZipfilename); buff = new BufferedOutputStream(dest); out = new org.apache.tools.zip.ZipOutputStream(buff); HashSet<String> fileSet = new HashSet<String>(); 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. zin = new ZipInputStream(new FileInputStream(tempFile)); entry = (ZipEntry) zin.getNextEntry(); while (entry != null) { String name = entry.getName(); if (!fileSet.contains(name)) { // Add ZIP entry to output stream. out.putNextEntry(new ZipEntry(name)); // Transfer bytes from the ZIP file to the // output file int len; while ((len = zin.read(buffer)) > 0) { out.write(buffer, 0, len); } fileSet.add(name); } entry = (ZipEntry) zin.getNextEntry(); } // Close the streams zin.close(); } // Set the method out.setMethod(org.apache.tools.zip.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...) String[] ZippedFiles = new String[filelist.length]; int FileNum = 0; // Get the files in the list... for (int i = 0; i < filelist.length && !parentJob.isStopped(); i++) { boolean getIt = true; boolean getItexclude = false; // First see if the file matches the regular // expression! // ..only if target is a folder ! if (f.isDirectory()) { if (pattern != null) { Matcher matcher = pattern.matcher(filelist[i]); getIt = matcher.matches(); } if (patternexclude != null) { Matcher matcherexclude = patternexclude.matcher(filelist[i]); getItexclude = matcherexclude.matches(); } } // Get processing File String targetFilename = realTargetdirectory + Const.FILE_SEPARATOR + filelist[i]; if (f.isFile()) targetFilename = realTargetdirectory; File file = new File(targetFilename); if (getIt && !getItexclude && !file.isDirectory() && !fileSet.contains(filelist[i])) { // We can add the file to the Zip Archive if (log.isDebug()) log.logDebug(toString(), Messages.getString("JobZipFiles.Add_FilesToZip1.Label") + filelist[i] + Messages.getString("JobZipFiles.Add_FilesToZip2.Label") + realTargetdirectory + Messages.getString("JobZipFiles.Add_FilesToZip3.Label")); // Associate a file input stream for the current // file FileInputStream in = new FileInputStream(targetFilename); // Add ZIP entry to output stream. out.putNextEntry(new ZipEntry(filelist[i])); int len; while ((len = in.read(buffer)) > 0) { out.write(buffer, 0, len); } out.flush(); out.closeEntry(); // Close the current file input stream in.close(); // Get Zipped File ZippedFiles[FileNum] = filelist[i]; FileNum = FileNum + 1; } } // Close the ZipOutPutStream out.close(); buff.close(); dest.close(); if (log.isBasic()) log.logBasic(toString(), Messages.getString("JobZipFiles.Log.TotalZippedFiles", "" + ZippedFiles.length)); // Delete Temp File if (tempFile != null) { tempFile.delete(); } // -----Get the list of Zipped Files and Move or Delete // Them if (afterzip == 1 || afterzip == 2) { // iterate through the array of Zipped files for (int i = 0; i < ZippedFiles.length; i++) { if (ZippedFiles[i] != null) { // Delete File FileObject fileObjectd = KettleVFS.getFileObject( realTargetdirectory + Const.FILE_SEPARATOR + ZippedFiles[i]); if (f.isFile()) fileObjectd = KettleVFS.getFileObject(realTargetdirectory); // Here gc() is explicitly called if e.g. // createfile is used in the same // job for the same file. The problem is // that after creating the file the // file object is not properly garbaged // collected and thus the file cannot // be deleted anymore. This is a known // problem in the JVM. System.gc(); // Here we can move, delete files if (afterzip == 1) { // Delete File boolean deleted = fileObjectd.delete(); if (!deleted) { resultat = false; log.logError(toString(), Messages .getString("JobZipFiles.Cant_Delete_File1.Label") + realTargetdirectory + Const.FILE_SEPARATOR + ZippedFiles[i] + Messages.getString("JobZipFiles.Cant_Delete_File2.Label")); } // File deleted if (log.isDebug()) log.logDebug(toString(), Messages.getString("JobZipFiles.File_Deleted1.Label") + realTargetdirectory + Const.FILE_SEPARATOR + ZippedFiles[i] + Messages .getString("JobZipFiles.File_Deleted2.Label")); } else if (afterzip == 2) { // Move File try { FileObject fileObjectm = KettleVFS.getFileObject( realMovetodirectory + Const.FILE_SEPARATOR + ZippedFiles[i]); fileObjectd.moveTo(fileObjectm); } catch (IOException e) { log.logError(toString(), Messages.getString("JobZipFiles.Cant_Move_File1.Label") + ZippedFiles[i] + Messages .getString("JobZipFiles.Cant_Move_File2.Label") + e.getMessage()); resultat = false; } // File moved if (log.isDebug()) log.logDebug(toString(), Messages.getString("JobZipFiles.File_Moved1.Label") + ZippedFiles[i] + Messages.getString("JobZipFiles.File_Moved2.Label")); } } } } if (addfiletoresult) { // Add file to result files name ResultFile resultFile = new ResultFile(ResultFile.FILE_TYPE_GENERAL, KettleVFS.getFileObject(realZipfilename), parentJob.getJobname(), toString()); result.getResultFiles().put(resultFile.getFile().toString(), resultFile); } resultat = true; } } } catch (Exception e) { log.logError(toString(), Messages.getString("JobZipFiles.Cant_CreateZipFile1.Label") + realZipfilename + Messages.getString("JobZipFiles.Cant_CreateZipFile2.Label") + e.getMessage()); // result.setResult( false ); // result.setNrErrors(1); resultat = false; } finally { if (fileObject != null) { try { fileObject.close(); } catch (IOException ex) { } ; } // Close the ZipOutPutStream try { if (out != null) out.close(); if (buff != null) buff.close(); if (dest != null) dest.close(); if (zin != null) zin.close(); if (entry != null) entry = null; } catch (IOException ex) { } ; } } else { resultat = true; if (realZipfilename == null) log.logError(toString(), Messages.getString("JobZipFiles.No_ZipFile_Defined.Label")); if (!orginexist) log.logError(toString(), Messages.getString("JobZipFiles.No_FolderCible_Defined.Label", realTargetdirectory)); } // return a verifier return resultat; }
From source file:com.panet.imeta.job.entries.movefiles.JobEntryMoveFiles.java
private boolean MoveFile(String shortfilename, FileObject sourcefilename, FileObject destinationfilename, FileObject movetofolderfolder, LogWriter log, Job parentJob, Result result) { FileObject destinationfile = null; boolean retval = false; try {// www .j a v a 2 s .c o m if (!destinationfilename.exists()) { if (!simulate) sourcefilename.moveTo(destinationfilename); if (log.isDetailed()) log.logDetailed(toString(), Messages.getString("JobMoveFiles.Log.FileMoved", sourcefilename.getName().toString(), destinationfilename.getName().toString())); // add filename to result filename if (add_result_filesname && !iffileexists.equals("fail") && !iffileexists.equals("do_nothing")) addFileToResultFilenames(destinationfilename.toString(), log, result, parentJob); updateSuccess(); } else { if (log.isDetailed()) log.logDetailed(toString(), Messages.getString("JobMoveFiles.Log.FileExists", destinationfilename.toString())); if (iffileexists.equals("overwrite_file")) { if (!simulate) sourcefilename.moveTo(destinationfilename); if (log.isDetailed()) log.logDetailed(toString(), Messages.getString("JobMoveFiles.Log.FileOverwrite", destinationfilename.getName().toString())); // add filename to result filename if (add_result_filesname && !iffileexists.equals("fail") && !iffileexists.equals("do_nothing")) addFileToResultFilenames(destinationfilename.toString(), log, result, parentJob); updateSuccess(); } else if (iffileexists.equals("unique_name")) { String short_filename = shortfilename; // return destination short filename try { short_filename = getMoveDestinationFilename(short_filename, "ddMMyyyy_HHmmssSSS"); } catch (Exception e) { log.logError(toString(), Messages.getString( Messages.getString("JobMoveFiles.Error.GettingFilename", short_filename))); return retval; } String movetofilenamefull = destinationfilename.getParent().toString() + Const.FILE_SEPARATOR + short_filename; destinationfile = KettleVFS.getFileObject(movetofilenamefull); if (!simulate) sourcefilename.moveTo(destinationfile); if (log.isDetailed()) log.logDetailed(toString(), Messages.getString("JobMoveFiles.Log.FileMoved", sourcefilename.getName().toString(), destinationfile.getName().toString())); // add filename to result filename if (add_result_filesname && !iffileexists.equals("fail") && !iffileexists.equals("do_nothing")) addFileToResultFilenames(destinationfile.toString(), log, result, parentJob); updateSuccess(); } else if (iffileexists.equals("delete_file")) { if (!simulate) destinationfilename.delete(); if (log.isDetailed()) log.logDetailed(toString(), Messages.getString("JobMoveFiles.Log.FileDeleted", destinationfilename.getName().toString())); } else if (iffileexists.equals("move_file")) { String short_filename = shortfilename; // return destination short filename try { short_filename = getMoveDestinationFilename(short_filename, null); } catch (Exception e) { log.logError(toString(), Messages.getString( Messages.getString("JobMoveFiles.Error.GettingFilename", short_filename))); return retval; } String movetofilenamefull = movetofolderfolder.toString() + Const.FILE_SEPARATOR + short_filename; destinationfile = KettleVFS.getFileObject(movetofilenamefull); if (!destinationfile.exists()) { if (!simulate) sourcefilename.moveTo(destinationfile); if (log.isDetailed()) log.logDetailed(toString(), Messages.getString("JobMoveFiles.Log.FileMoved", sourcefilename.getName().toString(), destinationfile.getName().toString())); // add filename to result filename if (add_result_filesname && !iffileexists.equals("fail") && !iffileexists.equals("do_nothing")) addFileToResultFilenames(destinationfile.toString(), log, result, parentJob); } else { if (ifmovedfileexists.equals("overwrite_file")) { if (!simulate) sourcefilename.moveTo(destinationfile); if (log.isDetailed()) log.logDetailed(toString(), Messages.getString("JobMoveFiles.Log.FileOverwrite", destinationfile.getName().toString())); // add filename to result filename if (add_result_filesname && !iffileexists.equals("fail") && !iffileexists.equals("do_nothing")) addFileToResultFilenames(destinationfile.toString(), log, result, parentJob); updateSuccess(); } else if (ifmovedfileexists.equals("unique_name")) { SimpleDateFormat daf = new SimpleDateFormat(); Date now = new Date(); daf.applyPattern("ddMMyyyy_HHmmssSSS"); String dt = daf.format(now); short_filename += "_" + dt; String destinationfilenamefull = movetofolderfolder.toString() + Const.FILE_SEPARATOR + short_filename; destinationfile = KettleVFS.getFileObject(destinationfilenamefull); if (!simulate) sourcefilename.moveTo(destinationfile); if (log.isDetailed()) log.logDetailed(toString(), Messages.getString("JobMoveFiles.Log.FileMoved", destinationfile.getName().toString())); // add filename to result filename if (add_result_filesname && !iffileexists.equals("fail") && !iffileexists.equals("do_nothing")) addFileToResultFilenames(destinationfile.toString(), log, result, parentJob); updateSuccess(); } else if (ifmovedfileexists.equals("fail")) { // Update Errors updateErrors(); } } } else if (iffileexists.equals("fail")) { // Update Errors updateErrors(); } } } catch (Exception e) { log.logError(toString(), Messages.getString("JobMoveFiles.Error.Exception.MoveProcessError", sourcefilename.toString(), destinationfilename.toString(), e.getMessage())); } finally { if (destinationfile != null) { try { destinationfile.close(); } catch (IOException ex) { } ; } } return retval; }
From source file:com.panet.imeta.job.entries.unzip.JobEntryUnZip.java
private boolean unzipFile(LogWriter log, FileObject sourceFileObject, String realTargetdirectory, String realWildcard, String realWildcardExclude, Result result, Job parentJob, FileObject fileObject, FileObject movetodir, String realMovetodirectory) { boolean retval = false; try {/*from w ww .jav a2 s .co m*/ if (log.isDetailed()) log.logDetailed(toString(), Messages.getString("JobUnZip.Log.ProcessingFile", sourceFileObject.toString())); // Do you create a root folder? // if (rootzip) { String shortSourceFilename = sourceFileObject.getName().getBaseName(); int lenstring = shortSourceFilename.length(); int lastindexOfDot = shortSourceFilename.lastIndexOf('.'); if (lastindexOfDot == -1) lastindexOfDot = lenstring; String foldername = realTargetdirectory + "/" + shortSourceFilename.substring(0, lastindexOfDot); FileObject rootfolder = KettleVFS.getFileObject(foldername); if (!rootfolder.exists()) { try { rootfolder.createFolder(); if (log.isDetailed()) log.logDetailed(toString(), Messages.getString("JobUnZip.Log.RootFolderCreated", foldername)); } catch (Exception e) { throw new Exception(Messages.getString("JobUnZip.Error.CanNotCreateRootFolder", foldername), e); } } } // Try to read the entries from the VFS object... // String zipFilename = "zip:" + sourceFileObject.getName().getFriendlyURI(); FileObject zipFile = KettleVFS.getFileObject(zipFilename); FileObject[] items = zipFile.findFiles(new AllFileSelector() { public boolean traverseDescendents(FileSelectInfo info) { return true; } public boolean includeFile(FileSelectInfo info) { // Never return the parent directory of a file list. if (info.getDepth() == 0) { return false; } FileObject fileObject = info.getFile(); return fileObject != null; } }); Pattern pattern = null; if (!Const.isEmpty(realWildcard)) { pattern = Pattern.compile(realWildcard); } Pattern patternexclude = null; if (!Const.isEmpty(realWildcardExclude)) { patternexclude = Pattern.compile(realWildcardExclude); } for (FileObject item : items) { if (successConditionBroken) { if (!successConditionBrokenExit) { log.logError(toString(), Messages.getString("JobUnZip.Error.SuccessConditionbroken", "" + NrErrors)); successConditionBrokenExit = true; } return false; } try { if (log.isDetailed()) log.logDetailed(toString(), Messages.getString("JobUnZip.Log.ProcessingZipEntry", item.getName().getURI(), sourceFileObject.toString())); // get real destination filename // String newFileName = realTargetdirectory + Const.FILE_SEPARATOR + getTargetFilename(item.getName().getPath()); FileObject newFileObject = KettleVFS.getFileObject(newFileName); if (item.getType().equals(FileType.FOLDER)) { // Directory // if (log.isDetailed()) log.logDetailed(toString(), Messages.getString("JobUnZip.CreatingDirectory.Label", newFileName)); // Create Directory if necessary ... // if (!newFileObject.exists()) newFileObject.createFolder(); } else { // File // boolean getIt = true; boolean getItexclude = false; // First see if the file matches the regular expression! // if (pattern != null) { Matcher matcher = pattern.matcher(item.getName().getURI()); getIt = matcher.matches(); } if (patternexclude != null) { Matcher matcherexclude = patternexclude.matcher(item.getName().getURI()); getItexclude = matcherexclude.matches(); } boolean take = takeThisFile(log, item, newFileName); if (getIt && !getItexclude && take) { if (log.isDetailed()) log.logDetailed(toString(), Messages.getString("JobUnZip.ExtractingEntry.Label", item.getName().getURI(), newFileName)); if (iffileexist == IF_FILE_EXISTS_UNIQ) { // Create file with unique name int lenstring = newFileName.length(); int lastindexOfDot = newFileName.lastIndexOf('.'); if (lastindexOfDot == -1) lastindexOfDot = lenstring; newFileName = newFileName.substring(0, lastindexOfDot) + StringUtil.getFormattedDateTimeNow(true) + newFileName.substring(lastindexOfDot, lenstring); if (log.isDebug()) log.logDebug(toString(), Messages.getString("JobUnZip.Log.CreatingUniqFile", newFileName)); } // See if the folder to the target file exists... // if (!newFileObject.getParent().exists()) { newFileObject.getParent().createFolder(); // creates // the // whole // path. } InputStream is = null; OutputStream os = null; try { is = KettleVFS.getInputStream(item); os = KettleVFS.getOutputStream(newFileObject, false); if (is != null) { byte[] buff = new byte[2048]; int len; while ((len = is.read(buff)) > 0) { os.write(buff, 0, len); } // Add filename to result filenames addFilenameToResultFilenames(result, parentJob, newFileName); } } finally { if (is != null) is.close(); if (os != null) os.close(); } } // end if take } } catch (Exception e) { updateErrors(); log.logError(toString(), Messages.getString("JobUnZip.Error.CanNotProcessZipEntry", item.getName().getURI(), sourceFileObject.toString()), e); } } // End while // Here gc() is explicitly called if e.g. createfile is used in the // same // job for the same file. The problem is that after creating the // file the // file object is not properly garbaged collected and thus the file // cannot // be deleted anymore. This is a known problem in the JVM. // System.gc(); // Unzip done... if (afterunzip == 1) { // delete zip file boolean deleted = fileObject.delete(); if (!deleted) { updateErrors(); log.logError(toString(), Messages.getString("JobUnZip.Cant_Delete_File.Label", sourceFileObject.toString())); } // File deleted if (log.isDebug()) log.logDebug(toString(), Messages.getString("JobUnZip.File_Deleted.Label", sourceFileObject.toString())); } else if (afterunzip == 2) { FileObject destFile = null; // Move File try { String destinationFilename = movetodir + Const.FILE_SEPARATOR + fileObject.getName().getBaseName(); destFile = KettleVFS.getFileObject(destinationFilename); fileObject.moveTo(destFile); // File moved if (log.isDetailed()) log.logDetailed(toString(), Messages.getString("JobUnZip.Log.FileMovedTo", sourceFileObject.toString(), realMovetodirectory)); } catch (Exception e) { updateErrors(); log.logError(toString(), Messages.getString("JobUnZip.Cant_Move_File.Label", sourceFileObject.toString(), realMovetodirectory, e.getMessage())); } finally { if (destFile != null) { try { destFile.close(); } catch (IOException ex) { } ; } } } retval = true; } catch (Exception e) { updateErrors(); log.logError(Messages.getString("JobUnZip.Error.Label"), Messages.getString("JobUnZip.ErrorUnzip.Label", sourceFileObject.toString(), e.getMessage()), e); } return retval; }