List of usage examples for java.util.zip ZipInputStream read
public int read(byte b[]) throws IOException
b.length
bytes of data from this input stream into an array of bytes. From source file:com.ephesoft.dcma.webservice.util.WebServiceHelper.java
public void processCreateHOCRXML(final HttpServletRequest req, final HttpServletResponse resp) throws InternalServerException, ValidationException { LOGGER.info("Start processing web service for create HOCR-XML for Batch Class"); String respStr = WebServiceUtil.EMPTY_STRING; String workingDir = WebServiceUtil.EMPTY_STRING; File zipInFolder = null;//from www . j a v a2 s . c o m File zipOutFolder = null; int responseCode = 0; WebServiceParams webServiceParams = null; if (req instanceof DefaultMultipartHttpServletRequest) { try { final String webServiceFolderPath = batchSchemaService.getWebServicesFolderPath(); workingDir = WebServiceUtil.createWebServiceWorkingDir(webServiceFolderPath); final String outputDir = WebServiceUtil.createWebServiceOutputDir(workingDir); final DefaultMultipartHttpServletRequest multiPartRequest = (DefaultMultipartHttpServletRequest) req; final MultiValueMap<String, MultipartFile> fileMap = multiPartRequest.getMultiFileMap(); if (fileMap.size() != 2) { LOGGER.error("Invalid Number of files sent to the server"); respStr = WebServiceConstants.INVALID_ARGUMENTS_FOR_CREATE_HOCR; responseCode = WebServiceConstants.INVALID_PARAMETERS_CODE; } else { String xmlFileName = WebServiceUtil.EMPTY_STRING; String zipOutputLocation = WebServiceUtil.EMPTY_STRING; String zipFileNameWithOutExt = WebServiceUtil.EMPTY_STRING; for (final String fileName : fileMap.keySet()) { InputStream instream = null; OutputStream outStream = null; ZipInputStream zipstream = null; try { if (!(fileName.endsWith(FileType.ZIP.getExtensionWithDot()) || fileName.endsWith(FileType.XML.getExtensionWithDot()) || fileName.endsWith(FileType.TIF.getExtensionWithDot()) || fileName.endsWith(FileType.TIFF.getExtensionWithDot()))) { respStr = WebServiceConstants.INVALID_ARGUMENTS_FOR_CREATE_OCR_BATCH_CLASS; responseCode = WebServiceConstants.INVALID_PARAMETERS_CODE; } final MultipartFile multiPartFile = multiPartRequest.getFile(fileName); instream = multiPartFile.getInputStream(); if (fileName.endsWith(FileType.XML.getExtensionWithDot())) { xmlFileName = fileName; final File file = new File(workingDir + File.separator + fileName); outStream = new FileOutputStream(file); final byte[] buf = new byte[WebServiceUtil.bufferSize]; int len; while ((len = instream.read(buf)) > 0) { outStream.write(buf, 0, len); } } if (fileName.endsWith(FileType.ZIP.getExtensionWithDot())) { zipFileNameWithOutExt = FilenameUtils.removeExtension(fileName); zipInFolder = new File(workingDir + File.separator + zipFileNameWithOutExt); if (zipInFolder != null) { zipInFolder.mkdirs(); } zipstream = new ZipInputStream(instream); ZipEntry ze = zipstream.getNextEntry(); if (ze == null) { respStr = WebServiceConstants.NO_FILES_IN_ZIP_DIR; responseCode = WebServiceConstants.NO_FILES_IN_ZIP_DIR_CODE; LOGGER.error(respStr + " No files in the zip directory "); } while (ze != null) { String upzipFileName = ze.getName(); LOGGER.info("Unzipping " + upzipFileName); if (!(upzipFileName.endsWith(FileType.TIF.getExtensionWithDot()) || upzipFileName.endsWith(FileType.TIFF.getExtensionWithDot()) || upzipFileName.endsWith(FileType.PDF.getExtensionWithDot()))) { respStr = WebServiceConstants.UNSUPPORTED_FILE_TYPE_EXCEPTION_MESSAGE; responseCode = WebServiceConstants.UNSUPPORTED_FILE_TYPE_EXCEPTION_CODE; LOGGER.error("File name should be a valid tif, tiff or pdf file name only"); } final File filePath = new File(zipInFolder + File.separator + upzipFileName); outStream = new FileOutputStream(filePath); final byte[] buf = new byte[WebServiceUtil.bufferSize]; int len; while ((len = zipstream.read(buf)) > 0) { outStream.write(buf, 0, len); } final int pageCount = TIFFUtil .getTIFFPageCount(zipInFolder + File.separator + upzipFileName); if (pageCount > 1 || upzipFileName.endsWith(FileType.PDF.getExtensionWithDot())) { final BatchInstanceThread threadList = new BatchInstanceThread( new File(zipInFolder.toString()).getName() + Math.random()); LOGGER.info( "Start spliting multipage tiff/pdf file into tiffs using image magick"); imService.convertPdfOrMultiPageTiffToTiffUsingIM("", filePath, "", new File(EphesoftStringUtil.concatenate(zipInFolder.toString(), File.separator, upzipFileName)), threadList); threadList.execute(); } ze = zipstream.getNextEntry(); } } if (fileName.endsWith(FileType.TIFF.getExtensionWithDot()) || fileName.endsWith(FileType.TIF.getExtensionWithDot())) { zipFileNameWithOutExt = WebServiceUtil.EMPTY_STRING; final File file = new File(workingDir + File.separator + fileName); zipInFolder = new File(workingDir); outStream = new FileOutputStream(file); final byte[] buf = new byte[WebServiceUtil.bufferSize]; int len; while ((len = instream.read(buf)) > 0) { outStream.write(buf, 0, len); } final int pageCount = TIFFUtil .getTIFFPageCount(workingDir + File.separator + fileName); if (pageCount > 1) { final BatchInstanceThread threadList = new BatchInstanceThread( zipInFolder.getName() + Math.random()); LOGGER.info( "Start spliting multipage tiff/pdf file into tiffs using image magick"); imService.convertPdfOrMultiPageTiffToTiffUsingIM(WebServiceUtil.EMPTY_STRING, file, WebServiceUtil.EMPTY_STRING, new File(EphesoftStringUtil .concatenate(workingDir.toString(), File.separator, fileName)), threadList); threadList.execute(); } } } finally { IOUtils.closeQuietly(instream); IOUtils.closeQuietly(outStream); IOUtils.closeQuietly(zipstream); } } final File xmlFile = new File(workingDir + File.separator + xmlFileName); if (StringUtils.isNotEmpty(xmlFileName) && xmlFile.exists()) { final FileInputStream inputStream = new FileInputStream(xmlFile); final Source source = XMLUtil.createSourceFromStream(inputStream); final Object unmarshelledObject = batchSchemaDao.getJAXB2Template().getJaxb2Marshaller() .unmarshal(source); if (!(unmarshelledObject instanceof WebServiceParams)) { respStr = WebServiceConstants.INVALID_ARGUMENTS_IN_XML_INPUT_MESSAGE; responseCode = WebServiceConstants.INVALID_ARGUMENTS_IN_XML_INPUT_CODE; } else { webServiceParams = (WebServiceParams) unmarshelledObject; if (null != webServiceParams.getParams()) { final List<Param> paramList = webServiceParams.getParams().getParam(); if (paramList == null || paramList.isEmpty()) { final HttpStatus status = HttpStatus.INTERNAL_SERVER_ERROR; respStr = WebServiceConstants.INVALID_ARGUMENTS_IN_XML_INPUT_MESSAGE; responseCode = WebServiceConstants.INVALID_ARGUMENTS_IN_XML_INPUT_CODE; LOGGER.error(respStr + "\n No Parameters in the request" + status); } else { String batchClassId = WebServiceUtil.EMPTY_STRING; for (final Param param : paramList) { if (WebServiceConstants.BATCH_CLASS_IDENTIFIER .equalsIgnoreCase(param.getName())) { batchClassId = param.getValue(); continue; } if (WebServiceUtil.ZIP_OUTPUT_LOCATION.equalsIgnoreCase(param.getName())) { zipOutputLocation = param.getValue(); if (StringUtils.isBlank(zipOutputLocation) || !(new File(zipOutputLocation).isDirectory())) { respStr = WebServiceConstants.ZIP_OUTPUT_LOCATION_INVALID_MESSAGE; responseCode = WebServiceConstants.ZIP_OUTPUT_LOCATION_INVALID_CODE; LOGGER.error( "Zip output location is blank or invalid in xml input file"); } continue; } if (WebServiceUtil.ZIP_NAME.equalsIgnoreCase(param.getName())) { if (!((zipFileNameWithOutExt + FileType.ZIP.getExtensionWithDot()) .equals(param.getValue()))) { respStr = WebServiceConstants.INPUT_ZIP_NOT_FOUND_MESSAGE; responseCode = WebServiceConstants.INVALID_ARGUMENTS_IN_XML_INPUT_CODE; LOGGER.error( "Zip file name doesn't match zip file name in xml input file"); } else continue; } } if (respStr == null || respStr.isEmpty()) { if (batchClassId != null && !batchClassId.isEmpty()) { final BatchClass batchClass = batchClassService .getBatchClassByIdentifier(batchClassId); if (batchClass != null) { final String ocrEngine = getDefaultHOCRPlugin(batchClassId); String colorSwitch = WebServiceUtil.EMPTY_STRING; String cmdLanguage = WebServiceUtil.EMPTY_STRING; LOGGER.info(EphesoftStringUtil.concatenate("Ocr engine used is : ", ocrEngine)); if (WebServiceConstants.TESSERACT_HOCR_PLUGIN .equalsIgnoreCase(ocrEngine)) { final File dir = new File(zipInFolder + ""); File[] directoryListing = dir.listFiles(); if (directoryListing != null) { zipOutFolder = new File( outputDir + File.separator + zipFileNameWithOutExt); if (zipOutFolder != null) { zipOutFolder.mkdirs(); } for (File inputFile : directoryListing) { String inputFileName = inputFile.getName(); if (inputFileName != null && !inputFileName.isEmpty()) { if (inputFileName.endsWith( FileType.TIFF.getExtensionWithDot()) || inputFileName.endsWith(FileType.TIF .getExtensionWithDot())) { final int pageCountTiff = TIFFUtil .getTIFFPageCount(inputFile.toString()); if (pageCountTiff > 1) { continue; } if (WebServiceConstants.TESSERACT_HOCR_PLUGIN .equalsIgnoreCase(ocrEngine)) { final BatchPlugin pluginProperties = classPluginPropertiesService .getPluginProperties(batchClassId, WebServiceConstants.TESSERACT_HOCR_PLUGIN); if (pluginProperties != null) { if (pluginProperties .getPluginConfigurations( TesseractProperties.TESSERACT_COLOR_SWITCH) != null) { colorSwitch = classPluginPropertiesService .getPropertyValue( batchClassId, WebServiceConstants.TESSERACT_HOCR_PLUGIN, TesseractProperties.TESSERACT_COLOR_SWITCH); if (pluginProperties .getPluginConfigurations( TesseractProperties.TESSERACT_LANGUAGE) != null) { cmdLanguage = classPluginPropertiesService .getPropertyValue( batchClassId, WebServiceConstants.TESSERACT_HOCR_PLUGIN, TesseractProperties.TESSERACT_LANGUAGE); tesseractService.createOCR( zipInFolder.toString(), colorSwitch, inputFileName, zipOutFolder.toString(), cmdLanguage, WebServiceConstants.TESSERACT_CURRENT_VERSION); } else { respStr = WebServiceConstants.NO_TESSERACT_LANGUAGE_SUPPORT; responseCode = WebServiceConstants.INTERNAL_SERVER_ERROR_CODE; LOGGER.error( "No Language Support"); } } else { respStr = WebServiceConstants.NO_TESSERACT_COLOR_SWITCH; responseCode = WebServiceConstants.INTERNAL_SERVER_ERROR_CODE; LOGGER.error( "Colour Switch Not Found"); } } else { respStr = WebServiceConstants.NO_PROPERTY_FOR_TESSERACT_HOCR; responseCode = WebServiceConstants.INTERNAL_SERVER_ERROR_CODE; } } } } else { respStr = WebServiceConstants.INVALID_FILE_NAME; responseCode = WebServiceConstants.INVALID_PARAMETERS_CODE; LOGGER.error( "File Name should not be NULL or empty "); } } // End of for loop } else { respStr = WebServiceConstants.NO_FILES_IN_ZIP_DIR; responseCode = WebServiceConstants.NO_FILES_IN_ZIP_DIR_CODE; LOGGER.error(respStr + " No files in the zip directory "); } } if (respStr.isEmpty()) { ServletOutputStream out = null; ZipOutputStream zout = null; final String zipFileName = WebServiceUtil.serverOutputFolderName; resp.setContentType(WebServiceUtil.APPLICATION_X_ZIP); resp.setHeader(WebServiceUtil.CONTENT_DISPOSITION, WebServiceUtil.ATTACHMENT_FILENAME + zipFileName + FileType.ZIP.getExtensionWithDot() + "\"\r\n"); resp.setStatus(HttpServletResponse.SC_OK); try { out = resp.getOutputStream(); zout = new ZipOutputStream(out); FileUtils.zipDirectory(zipOutFolder.toString(), zout, zipFileName); } catch (final FileNotFoundException fileNotFoundException) { String messageString = fileNotFoundException.getMessage(); messageString = messageString.substring( messageString.lastIndexOf(File.separator)); respStr = WebServiceConstants.FILE_NOT_FOUND + messageString; responseCode = WebServiceConstants.INTERNAL_SERVER_ERROR_CODE; LOGGER.error("Could Not Copy the File " + fileNotFoundException.getMessage()); } catch (final IOException ioExcpetion) { respStr = WebServiceConstants.ERROR_WHILE_CREATING_ZIPPED_FILE + ioExcpetion; responseCode = WebServiceConstants.INTERNAL_SERVER_ERROR_CODE; LOGGER.error(respStr); } finally { IOUtils.closeQuietly(zout); IOUtils.closeQuietly(out); } } if (respStr.isEmpty()) { FileOutputStream fos = null; ZipOutputStream zos = null; final File out = new File(zipOutputLocation + File.separator + zipFileNameWithOutExt + FileType.ZIP.getExtensionWithDot()); try { fos = new FileOutputStream(out); zos = new ZipOutputStream(fos); FileUtils.zipDirectory(zipOutFolder.toString(), zos, zipFileNameWithOutExt); } catch (final FileNotFoundException fileNotFoundException) { String messageString = fileNotFoundException.getMessage(); messageString = messageString.substring( messageString.lastIndexOf(File.separator)); respStr = WebServiceConstants.FILE_NOT_FOUND + messageString; responseCode = WebServiceConstants.INTERNAL_SERVER_ERROR_CODE; LOGGER.error("Could Not Copy the File " + fileNotFoundException.getMessage()); } catch (final IOException ioExcpetion) { respStr = WebServiceConstants.ERROR_WHILE_CREATING_ZIPPED_FILE + ioExcpetion; responseCode = WebServiceConstants.INTERNAL_SERVER_ERROR_CODE; LOGGER.error(respStr); } finally { IOUtils.closeQuietly(zos); IOUtils.closeQuietly(fos); } } } else { respStr = WebServiceConstants.INVALID_BATCH_CLASS_ID_MESSAGE; responseCode = WebServiceConstants.INVALID_ARGUMENTS_IN_XML_INPUT_CODE; LOGGER.error(respStr + " Batch Class ID doesnot exist "); } } else { respStr = WebServiceConstants.INVALID_BATCH_CLASS_ID_MESSAGE; responseCode = WebServiceConstants.INVALID_ARGUMENTS_IN_XML_INPUT_CODE; LOGGER.error(respStr + " No input of Batch Class ID "); } } } } } } else { respStr = WebServiceConstants.INPUT_FILES_NOT_FOUND_MESSAGE; responseCode = WebServiceConstants.INVALID_PARAMETERS_CODE; } } } catch (final FileNotFoundException fileNotFoundException) { String message = fileNotFoundException.getMessage(); message = message.substring(message.lastIndexOf(File.separator)); respStr = WebServiceConstants.FILE_NOT_FOUND + message; } catch (final ValidationException validationException) { throw validationException; } catch (final InternalServerException internalServerError) { throw internalServerError; } catch (final Exception exception) { respStr = WebServiceConstants.INTERNAL_SERVER_ERROR_MESSAGE + exception; responseCode = WebServiceConstants.INTERNAL_SERVER_ERROR_CODE; exception.printStackTrace(); } finally { FileUtils.deleteDirectoryAndContentsRecursive(new File(workingDir)); } } else { respStr = WebServiceConstants.INVALID_MULTIPART_REQUEST; responseCode = WebServiceConstants.INVALID_PARAMETERS_CODE; } validateResponse(responseCode, respStr); }
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 w w. j a va 2s .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.portfolio.data.provider.MysqlDataProvider.java
public static String unzip(String zipFile, String destinationFolder) throws FileNotFoundException, IOException { String folder = ""; File zipfile = new File(zipFile); ZipInputStream zis = new ZipInputStream(new BufferedInputStream(new FileInputStream(zipfile))); ZipEntry ze = null;/*from w w w .jav a2s. co m*/ try { while ((ze = zis.getNextEntry()) != null) { //folder = zipfile.getCanonicalPath().substring(0, zipfile.getCanonicalPath().length()-4)+"/"; folder = destinationFolder; File f = new File(folder, ze.getName()); if (ze.isDirectory()) { f.mkdirs(); continue; } f.getParentFile().mkdirs(); OutputStream fos = new BufferedOutputStream(new FileOutputStream(f)); try { try { final byte[] buf = new byte[8192]; int bytesRead; while (-1 != (bytesRead = zis.read(buf))) fos.write(buf, 0, bytesRead); } finally { fos.close(); } } catch (final IOException ioe) { f.delete(); throw ioe; } } } finally { zis.close(); } return folder; }
From source file:org.pentaho.di.job.entries.zipfile.JobEntryZipFile.java
public boolean processRowFile(Job parentJob, Result result, String realZipfilename, String realWildcard, String realWildcardExclude, String realSourceDirectoryOrFile, String realMovetodirectory, boolean createparentfolder) { boolean Fileexists = false; File tempFile = null;/*from ww w . j a 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; OutputStream dest = null; BufferedOutputStream buff = null; ZipOutputStream out = null; ZipEntry entry = null; String localSourceFilename = realSourceDirectoryOrFile; try { originFile = KettleVFS.getFileObject(realSourceDirectoryOrFile, this); localSourceFilename = KettleVFS.getFilename(originFile); orginExist = originFile.exists(); } catch (Exception e) { // Ignore errors } finally { if (originFile != null) { try { originFile.close(); } catch (IOException ex) { logError("Error closing file '" + originFile.toString() + "'", ex); } } } String localrealZipfilename = realZipfilename; if (realZipfilename != null && orginExist) { FileObject fileObject = null; try { fileObject = KettleVFS.getFileObject(localrealZipfilename, this); localrealZipfilename = KettleVFS.getFilename(fileObject); // Check if Zip File exists if (fileObject.exists()) { Fileexists = true; if (log.isDebug()) { logDebug(BaseMessages.getString(PKG, "JobZipFiles.Zip_FileExists1.Label") + localrealZipfilename + BaseMessages.getString(PKG, "JobZipFiles.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 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, fileObject, 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; logError( BaseMessages.getString(PKG, "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 FileObject[] fileList = null; FileObject sourceFileOrFolder = KettleVFS.getFileObject(localSourceFilename); boolean isSourceDirectory = sourceFileOrFolder.getType().equals(FileType.FOLDER); final Pattern pattern; final Pattern patternexclude; if (isSourceDirectory) { // Let's prepare the pattern matcher for performance reasons. // We only do this if the target is a folder ! // if (!Const.isEmpty(realWildcard)) { pattern = Pattern.compile(realWildcard); } else { pattern = null; } if (!Const.isEmpty(realWildcardExclude)) { patternexclude = Pattern.compile(realWildcardExclude); } else { patternexclude = null; } // Target is a directory // Get all the files in the directory... // if (includingSubFolders) { fileList = sourceFileOrFolder.findFiles(new FileSelector() { public boolean traverseDescendents(FileSelectInfo fileInfo) throws Exception { return true; } public boolean includeFile(FileSelectInfo fileInfo) throws Exception { boolean include; // Only include files in the sub-folders... // When we include sub-folders we match the whole filename, not just the base-name // if (fileInfo.getFile().getType().equals(FileType.FILE)) { include = true; if (pattern != null) { String name = fileInfo.getFile().getName().getPath(); include = pattern.matcher(name).matches(); } if (include && patternexclude != null) { String name = fileInfo.getFile().getName().getPath(); include = !pattern.matcher(name).matches(); } } else { include = false; } return include; } }); } else { fileList = sourceFileOrFolder.getChildren(); } } else { pattern = null; patternexclude = null; // Target is a file fileList = new FileObject[] { sourceFileOrFolder }; } if (fileList.length == 0) { resultat = false; logError(BaseMessages.getString(PKG, "JobZipFiles.Log.FolderIsEmpty", localSourceFilename)); } else if (!checkContainsFile(localSourceFilename, fileList, isSourceDirectory)) { resultat = false; logError(BaseMessages.getString(PKG, "JobZipFiles.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 (log.isDebug()) { logDebug(BaseMessages.getString(PKG, "JobZipFiles.Zip_FileNameChange1.Label") + localrealZipfilename + BaseMessages.getString(PKG, "JobZipFiles.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. tempFile.delete(); renameOk = fileZip.renameTo(tempFile); if (!renameOk) { logError(BaseMessages.getString(PKG, "JobZipFiles.Cant_Rename_Temp1.Label") + fileZip.getAbsolutePath() + BaseMessages.getString(PKG, "JobZipFiles.Cant_Rename_Temp2.Label") + tempFile.getAbsolutePath() + BaseMessages.getString(PKG, "JobZipFiles.Cant_Rename_Temp3.Label")); } if (log.isDebug()) { logDebug(BaseMessages.getString(PKG, "JobZipFiles.Zip_FileAppend1.Label") + localrealZipfilename + BaseMessages.getString(PKG, "JobZipFiles.Zip_FileAppend2.Label")); } } if (log.isDetailed()) { logDetailed( BaseMessages.getString(PKG, "JobZipFiles.Files_Found1.Label") + fileList.length + BaseMessages.getString(PKG, "JobZipFiles.Files_Found2.Label") + localSourceFilename + BaseMessages.getString(PKG, "JobZipFiles.Files_Found3.Label")); } // Prepare Zip File buffer = new byte[18024]; dest = KettleVFS.getOutputStream(localrealZipfilename, false); buff = new BufferedOutputStream(dest); out = new 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 = 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 = zin.getNextEntry(); } // Close the streams zin.close(); } // 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...) FileObject[] zippedFiles = new FileObject[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 (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 (patternexclude != null) { Matcher matcherexclude = patternexclude.matcher(filename); getItexclude = matcherexclude.matches(); } } // Get processing File String targetFilename = KettleVFS.getFilename(fileList[i]); if (sourceFileOrFolder.getType().equals(FileType.FILE)) { targetFilename = localSourceFilename; } FileObject file = KettleVFS.getFileObject(targetFilename); 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 (log.isDebug()) { logDebug(BaseMessages.getString(PKG, "JobZipFiles.Add_FilesToZip1.Label") + fileList[i] + BaseMessages.getString(PKG, "JobZipFiles.Add_FilesToZip2.Label") + localSourceFilename + BaseMessages.getString(PKG, "JobZipFiles.Add_FilesToZip3.Label")); } // Associate a file input stream for the current file InputStream in = KettleVFS.getInputStream(file); // Add ZIP entry to output stream. // String relativeName; String fullName = fileList[i].getName().getPath(); String basePath = sourceFileOrFolder.getName().getPath(); if (isSourceDirectory) { if (fullName.startsWith(basePath)) { relativeName = fullName.substring(basePath.length() + 1); } else { relativeName = fullName; } } else if (isFromPrevious) { int depth = determineDepth(environmentSubstitute(storedSourcePathDepth)); relativeName = determineZipfilenameForDepth(fullName, depth); } else { relativeName = fileList[i].getName().getBaseName(); } out.putNextEntry(new ZipEntry(relativeName)); 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()) { logBasic(BaseMessages.getString(PKG, "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, Move File FileObject fileObjectd = zippedFiles[i]; if (!isSourceDirectory) { fileObjectd = KettleVFS.getFileObject(localSourceFilename); } // Here we can move, delete files if (afterZip == 1) { // Delete File boolean deleted = fileObjectd.delete(); if (!deleted) { resultat = false; logError(BaseMessages.getString(PKG, "JobZipFiles.Cant_Delete_File1.Label") + localSourceFilename + Const.FILE_SEPARATOR + zippedFiles[i] + BaseMessages .getString(PKG, "JobZipFiles.Cant_Delete_File2.Label")); } // File deleted if (log.isDebug()) { logDebug(BaseMessages.getString(PKG, "JobZipFiles.File_Deleted1.Label") + localSourceFilename + Const.FILE_SEPARATOR + zippedFiles[i] + BaseMessages.getString(PKG, "JobZipFiles.File_Deleted2.Label")); } } else if (afterZip == 2) { // Move File FileObject fileObjectm = null; try { fileObjectm = KettleVFS.getFileObject(realMovetodirectory + Const.FILE_SEPARATOR + fileObjectd.getName().getBaseName()); fileObjectd.moveTo(fileObjectm); } catch (IOException e) { logError( BaseMessages.getString(PKG, "JobZipFiles.Cant_Move_File1.Label") + zippedFiles[i] + BaseMessages.getString(PKG, "JobZipFiles.Cant_Move_File2.Label") + e.getMessage()); resultat = false; } finally { try { if (fileObjectm != null) { fileObjectm.close(); } } catch (Exception e) { if (fileObjectm != null) { logError("Error closing file '" + fileObjectm.toString() + "'", e); } } } // File moved if (log.isDebug()) { logDebug(BaseMessages.getString(PKG, "JobZipFiles.File_Moved1.Label") + zippedFiles[i] + BaseMessages.getString(PKG, "JobZipFiles.File_Moved2.Label")); } } } } } if (addFileToResult) { // Add file to result files name ResultFile resultFile = new ResultFile(ResultFile.FILE_TYPE_GENERAL, fileObject, parentJob.getJobname(), toString()); result.getResultFiles().put(resultFile.getFile().toString(), resultFile); } resultat = true; } } } catch (Exception e) { logError(BaseMessages.getString(PKG, "JobZipFiles.Cant_CreateZipFile1.Label") + localrealZipfilename + BaseMessages.getString(PKG, "JobZipFiles.Cant_CreateZipFile2.Label"), e); resultat = false; } finally { if (fileObject != null) { try { fileObject.close(); fileObject = null; } catch (IOException ex) { logError("Error closing file '" + fileObject.toString() + "'", ex); } } 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) { logError("Error closing zip file entry for file '" + originFile.toString() + "'", ex); } } } else { resultat = true; if (localrealZipfilename == null) { logError(BaseMessages.getString(PKG, "JobZipFiles.No_ZipFile_Defined.Label")); } if (!orginExist) { logError(BaseMessages.getString(PKG, "JobZipFiles.No_FolderCible_Defined.Label", localSourceFilename)); } } // return a verifier return resultat; }