List of usage examples for java.util.zip ZipOutputStream DEFLATED
int DEFLATED
To view the source code for java.util.zip ZipOutputStream DEFLATED.
Click Source Link
From source file:edu.ncsu.asbransc.mouflon.recorder.UploadFile.java
private void zipUp(File out, File[] in) { FileOutputStream fout = null; ZipOutputStream zout = null;/*from ww w . j ava2 s . c om*/ byte[] buffer = new byte[4096]; int bytesRead = 0; try { fout = new FileOutputStream(out); zout = new ZipOutputStream(fout); zout.setMethod(ZipOutputStream.DEFLATED); for (File currFile : in) { FileInputStream fin = new FileInputStream(currFile); ZipEntry currEntry = new ZipEntry(currFile.getName()); zout.putNextEntry(currEntry); while ((bytesRead = fin.read(buffer)) > 0) { zout.write(buffer, 0, bytesRead); } zout.closeEntry(); fin.close(); } } catch (FileNotFoundException e) { e.printStackTrace(); } catch (IOException e) { e.printStackTrace(); } finally { try { zout.close(); fout.close(); } catch (IOException e) { e.printStackTrace(); } catch (NullPointerException e) { e.printStackTrace(); } } }
From source file:org.nuxeo.ecm.platform.picture.web.PictureBookManagerBean.java
protected String createZip(List<DocumentModel> documents) throws IOException { FacesContext context = FacesContext.getCurrentInstance(); HttpServletResponse response = (HttpServletResponse) context.getExternalContext().getResponse(); BufferedOutputStream buff = new BufferedOutputStream(response.getOutputStream()); ZipOutputStream out = new ZipOutputStream(buff); out.setMethod(ZipOutputStream.DEFLATED); out.setLevel(9);//ww w . j a va 2 s . co m byte[] data = new byte[BUFFER]; for (DocumentModel doc : documents) { // first check if DM is attached to the core if (doc.getSessionId() == null) { // refetch the doc from the core doc = documentManager.getDocument(doc.getRef()); } // NXP-2334 : skip deleted docs if (doc.getCurrentLifeCycleState().equals("delete")) { continue; } BlobHolder bh = doc.getAdapter(BlobHolder.class); if (doc.isFolder() && !isEmptyFolder(doc)) { addFolderToZip("", out, doc, data); } else if (bh != null) { addBlobHolderToZip("", out, data, (PictureBlobHolder) bh); } } try { out.close(); } catch (ZipException e) { // empty zip file, do nothing setFacesMessage("label.clipboard.emptyDocuments"); return null; } response.setHeader("Content-Disposition", "attachment; filename=\"" + "clipboard.zip" + "\";"); response.setContentType("application/gzip"); response.flushBuffer(); context.responseComplete(); return null; }
From source file:be.ibridge.kettle.job.entry.zipfile.JobEntryZipFile.java
public Result execute(Result prev_result, int nr, Repository rep, Job parentJob) { LogWriter log = LogWriter.getInstance(); Result result = new Result(nr); result.setResult(false);/*from w ww . j av a2s. com*/ boolean Fileexists = false; String realZipfilename = StringUtil.environmentSubstitute(zipFilename); String realWildcard = StringUtil.environmentSubstitute(wildcard); String realWildcardExclude = StringUtil.environmentSubstitute(wildcardexclude); String realTargetdirectory = StringUtil.environmentSubstitute(sourcedirectory); String realMovetodirectory = StringUtil.environmentSubstitute(movetodirectory); if (realZipfilename != null) { FileObject fileObject = null; try { fileObject = KettleVFS.getFileObject(realZipfilename); // Check if Zip File exists if (fileObject.exists()) { Fileexists = true; log.logDebug(toString(), Messages.getString("JobZipFiles.Zip_FileExists1.Label") + realZipfilename + Messages.getString("JobZipFiles.Zip_FileExists2.Label")); } else { Fileexists = false; } // Let's start the process now if (ifzipfileexists == 3 && Fileexists) { // the zip file exists and user want to Fail result.setResult(false); result.setNrErrors(1); } else if (ifzipfileexists == 2 && Fileexists) { // the zip file exists and user want to do nothing result.setResult(true); } else if (afterzip == 2 && realMovetodirectory == null) { // After Zip, Move files..User must give a destination Folder result.setResult(false); result.setNrErrors(1); log.logError(toString(), Messages.getString("JobZipFiles.AfterZip_No_DestinationFolder_Defined.Label")); } else // After Zip, Move files..User must give a destination Folder { if (ifzipfileexists == 0 && Fileexists) { // the zip file exists and user want to create new one with unique name //Format Date DateFormat dateFormat = new SimpleDateFormat("hhmmss_mmddyyyy"); realZipfilename = realZipfilename + "_" + dateFormat.format(new Date()) + ".zip"; log.logDebug(toString(), Messages.getString("JobZipFiles.Zip_FileNameChange1.Label") + realZipfilename + Messages.getString("JobZipFiles.Zip_FileNameChange1.Label")); } else if (ifzipfileexists == 1 && Fileexists) { log.logDebug(toString(), Messages.getString("JobZipFiles.Zip_FileAppend1.Label") + realZipfilename + Messages.getString("JobZipFiles.Zip_FileAppend2.Label")); } // Get all the files in the directory... File f = new File(realTargetdirectory); String[] filelist = f.list(); 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; if (!Const.isEmpty(realWildcard)) { pattern = Pattern.compile(realWildcard); } Pattern patternexclude = null; if (!Const.isEmpty(realWildcardExclude)) { patternexclude = Pattern.compile(realWildcardExclude); } // Prepare Zip File byte[] buffer = new byte[18024]; FileOutputStream dest = new FileOutputStream(realZipfilename); BufferedOutputStream buff = new BufferedOutputStream(dest); ZipOutputStream out = new ZipOutputStream(buff); // 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...) 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! 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]; File file = new File(targetFilename); if (getIt && !getItexclude && !file.isDirectory()) { // We can add the file to the Zip Archive 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.closeEntry(); // Close the current file input stream in.close(); // Get Zipped File ZippedFiles[FileNum] = filelist[i]; FileNum = FileNum + 1; } } // Close the ZipOutPutStream out.close(); //-----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]); // Here we can move, delete files if (afterzip == 1) { // Delete File boolean deleted = fileObjectd.delete(); if (!deleted) { result.setResult(false); result.setNrErrors(1); 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 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()); result.setResult(false); result.setNrErrors(1); } // File moved log.logDebug(toString(), Messages.getString("JobZipFiles.File_Moved1.Label") + ZippedFiles[i] + Messages.getString("JobZipFiles.File_Moved2.Label")); } } } } result.setResult(true); } } catch (IOException 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); } finally { if (fileObject != null) { try { fileObject.close(); } catch (IOException ex) { } ; } } } else { result.setResult(false); result.setNrErrors(1); log.logError(toString(), Messages.getString("JobZipFiles.No_ZipFile_Defined.Label")); } return result; }
From source file:edu.mayo.pipes.iterators.Compressor.java
/** * Create a single entry Zip archive, and prepare it for writing * * @throws IOException//from w ww. j a v a2 s . c o m */ public BufferedWriter makeZipWriter() throws IOException { if (outFile == null) return null; FileOutputStream outFileStream = new FileOutputStream(outFile); ZipOutputStream zipWrite = new ZipOutputStream(outFileStream); ZipEntry zE; // Setup the zip writing things zipWrite.setMethod(ZipOutputStream.DEFLATED); zipWrite.setLevel(9); // Max compression zE = new ZipEntry("Default"); zipWrite.putNextEntry(zE); // Now can attach the writer to write to this zip entry OutputStreamWriter wStream = new OutputStreamWriter(zipWrite); writer = new BufferedWriter(wStream); comp = kZipCompression; return writer; }
From source file:org.xwoot.xwootUtil.FileUtil.java
/** * Zip an array of files. Each file in the array must reflect a file on disk. If a file represents a directory on * disk, it will be skipped./*ww w . j a v a 2 s. com*/ * * @param files the files to be zipped. * @param zippedFileDestinationPath the location of the zip file that will result. * @throws IOException if problems occur. */ public static void zipFiles(File[] files, String zippedFileDestinationPath) throws IOException { if (files == null || zippedFileDestinationPath == null) { throw new NullPointerException("Null paramenters. filePaths: " + files + " zippedFileDestinationPath: " + zippedFileDestinationPath); } FileUtil.checkDirectoryPath(new File(zippedFileDestinationPath).getParent()); if (files.length < 1) { return; } FileOutputStream fos = null; BufferedOutputStream bos = null; ZipOutputStream zos = null; try { fos = new FileOutputStream(zippedFileDestinationPath); bos = new BufferedOutputStream(fos); zos = new ZipOutputStream(fos); zos.setMethod(ZipOutputStream.DEFLATED); zos.setLevel(Deflater.BEST_COMPRESSION); for (File aFile : files) { zipFiletoZipOutputStream(aFile.getParentFile(), aFile.getName(), zos); } } catch (Exception e) { throw new IOException("Failed to zip files: (" + e.getClass() + ") " + e.getMessage()); } finally { try { if (zos != null) { zos.close(); } if (bos != null) { bos.close(); } if (fos != null) { fos.close(); } } catch (Exception e) { throw new IOException("Problem closing streams. Reason: " + e.getMessage()); } } }
From source file:org.pentaho.di.trans.steps.zipfile.ZipFile.java
private void zipFile() throws KettleException { String localrealZipfilename = KettleVFS.getFilename(data.zipFile); boolean updateZip = false; byte[] buffer = null; OutputStream dest = null;// www . ja v a 2 s.c o m BufferedOutputStream buff = null; ZipOutputStream out = null; InputStream in = null; ZipInputStream zin = null; ZipEntry entry = null; File tempFile = null; HashSet<String> fileSet = new HashSet<String>(); try { updateZip = (data.zipFile.exists() && meta.isOverwriteZipEntry()); if (updateZip) { // the Zipfile exists // and we weed to update entries // Let's create a temp file File fileZip = getFile(localrealZipfilename); tempFile = File.createTempFile(fileZip.getName(), null); // delete it, otherwise we cannot rename existing zip to it. tempFile.delete(); updateZip = fileZip.renameTo(tempFile); } // Prepare Zip File buffer = new byte[18024]; dest = KettleVFS.getOutputStream(localrealZipfilename, false); buff = new BufferedOutputStream(dest); out = new ZipOutputStream(buff); if (updateZip) { // 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); out.setLevel(Deflater.BEST_COMPRESSION); // Associate a file input stream for the current file in = KettleVFS.getInputStream(data.sourceFile); // Add ZIP entry to output stream. // String relativeName = data.sourceFile.getName().getBaseName(); if (meta.isKeepSouceFolder()) { // Get full filename relativeName = KettleVFS.getFilename(data.sourceFile); if (data.baseFolder != null) { // Remove base folder data.baseFolder += Const.FILE_SEPARATOR; relativeName = relativeName.replace(data.baseFolder, ""); } } if (!fileSet.contains(relativeName)) { out.putNextEntry(new ZipEntry(relativeName)); int len; while ((len = in.read(buffer)) > 0) { out.write(buffer, 0, len); } } } catch (Exception e) { throw new KettleException(BaseMessages.getString(PKG, "ZipFile.ErrorCreatingZip"), e); } finally { try { if (in != null) { // Close the current file input stream in.close(); } if (out != null) { // Close the ZipOutPutStream out.flush(); out.closeEntry(); out.close(); } if (buff != null) { buff.close(); } if (dest != null) { dest.close(); } // Delete Temp File if (tempFile != null) { tempFile.delete(); } fileSet = null; } catch (Exception e) { /* Ignore */ } } }
From source file:org.kalypso.commons.java.util.zip.ZipUtilities.java
public static void pack(final File archiveTarget, final File packDir, final IFileFilter filter) throws ZipException, IOException { if (!packDir.isDirectory()) { return;/*from w w w . j a v a2 s . c o m*/ } final BufferedOutputStream bos = new BufferedOutputStream(new FileOutputStream(archiveTarget)); final ZipOutputStream out = new ZipOutputStream(bos); out.setMethod(ZipOutputStream.DEFLATED); final File[] files = packDir.listFiles(); for (final File file : files) { processFiles(packDir, file, out, filter); } out.close(); bos.flush(); bos.close(); }
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 va2s.c om*/ 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; }
From source file:com.ichi2.libanki.Media.java
/** * Unlike python, our temp zip file will be on disk instead of in memory. This avoids storing * potentially large files in memory which is not feasible with Android's limited heap space. * <p>//from w w w. j a v a 2s .c om * Notes: * <p> * - The maximum size of the changes zip is decided by the constant SYNC_ZIP_SIZE. If a media file exceeds this * limit, only that file (in full) will be zipped to be sent to the server. * <p> * - This method will be repeatedly called from MediaSyncer until there are no more files (marked "dirty" in the DB) * to send. * <p> * - Since AnkiDroid avoids scanning the media folder on every sync, it is possible for a file to be marked as a * new addition but actually have been deleted (e.g., with a file manager). In this case we skip over the file * and mark it as removed in the database. (This behaviour differs from the desktop client). * <p> */ public Pair<File, List<String>> mediaChangesZip() { File f = new File(mCol.getPath().replaceFirst("collection\\.anki2$", "tmpSyncToServer.zip")); Cursor cur = null; try { ZipOutputStream z = new ZipOutputStream(new BufferedOutputStream(new FileOutputStream(f))); z.setMethod(ZipOutputStream.DEFLATED); List<String> fnames = new ArrayList<String>(); // meta is a list of (fname, zipname), where zipname of null is a deleted file // NOTE: In python, meta is a list of tuples that then gets serialized into json and added // to the zip as a string. In our version, we use JSON objects from the start to avoid the // serialization step. Instead of a list of tuples, we use JSONArrays of JSONArrays. JSONArray meta = new JSONArray(); int sz = 0; byte buffer[] = new byte[2048]; cur = mDb.getDatabase() .rawQuery("select fname, csum from media where dirty=1 limit " + Consts.SYNC_ZIP_COUNT, null); for (int c = 0; cur.moveToNext(); c++) { String fname = cur.getString(0); String csum = cur.getString(1); fnames.add(fname); String normname = HtmlUtil.nfcNormalized(fname); if (!TextUtils.isEmpty(csum)) { try { mCol.log("+media zip " + fname); File file = new File(dir(), fname); BufferedInputStream bis = new BufferedInputStream(new FileInputStream(file), 2048); z.putNextEntry(new ZipEntry(Integer.toString(c))); int count = 0; while ((count = bis.read(buffer, 0, 2048)) != -1) { z.write(buffer, 0, count); } z.closeEntry(); bis.close(); meta.put(new JSONArray().put(normname).put(Integer.toString(c))); sz += file.length(); } catch (FileNotFoundException e) { // A file has been marked as added but no longer exists in the media directory. // Skip over it and mark it as removed in the db. removeFile(fname); } } else { mCol.log("-media zip " + fname); meta.put(new JSONArray().put(normname).put("")); } if (sz >= Consts.SYNC_ZIP_SIZE) { break; } } z.putNextEntry(new ZipEntry("_meta")); z.write(Utils.jsonToString(meta).getBytes()); z.closeEntry(); z.close(); // Don't leave lingering temp files if the VM terminates. f.deleteOnExit(); return new Pair<File, List<String>>(f, fnames); } catch (IOException e) { Timber.e("Failed to create media changes zip", e); throw new RuntimeException(e); } finally { if (cur != null) { cur.close(); } } }
From source file:org.n52.ifgicopter.spf.output.kml.KmlOutputPlugin.java
/** * http://java.sun.com/developer/technicalArticles/Programming/compression/ * //from ww w .ja v a2 s. c o m * TODO see if zipping solves referencing problem: * https://developers.google.com/kml/documentation/kmzarchives */ private void zipAllFiles() { if (!this.createKMZ) { return; } // saveDocumentInFile(this.kmlStaticRoot, this.outputPath + this.kmlStaticRootFileName); int buffer = 2048; try { BufferedInputStream origin = null; FileOutputStream dest = new FileOutputStream( this.outputFolder + File.separator + this.filePrefix + "." + KmlConstants.KMZ_FILE_EXTENSION); if (log.isDebugEnabled()) log.debug("Saving KML files to zip file " + dest); ZipOutputStream out = new ZipOutputStream(new BufferedOutputStream(dest)); out.setMethod(ZipOutputStream.DEFLATED); byte data[] = new byte[buffer]; File f = new File(this.outputFolder); File files[] = f.listFiles(onlyKmlFiles); for (int i = 0; i < files.length; i++) { if (log.isDebugEnabled()) log.debug("Zipping " + files[i]); FileInputStream fi = new FileInputStream(files[i].getAbsolutePath()); origin = new BufferedInputStream(fi, buffer); ZipEntry entry = new ZipEntry(files[i].getName()); out.putNextEntry(entry); int count; while ((count = origin.read(data, 0, buffer)) != -1) { out.write(data, 0, count); } origin.close(); } out.close(); } catch (Exception e) { log.error(e); } }