List of usage examples for java.util.zip ZipOutputStream putNextEntry
public void putNextEntry(ZipEntry e) throws IOException
From source file:mpimp.assemblxweb.util.J5FileUtils.java
private static void zipDirectoryRecursively(String dirPath, BufferedInputStream origin, int BUFFER, ZipOutputStream out, byte[] data, String parentDirName) throws Exception { File directory = new File(dirPath); File content[] = directory.listFiles(); for (int i = 0; i < content.length; i++) { if (content[i].isDirectory()) { String parentPath = parentDirName + File.separator + content[i].getName(); zipDirectoryRecursively(content[i].getAbsolutePath(), origin, BUFFER, out, data, parentPath); } else {/*from ww w. j a v a 2 s .c o m*/ String filePathInDirectory = parentDirName + File.separator + content[i].getName(); FileInputStream in = new FileInputStream(content[i].getAbsolutePath()); origin = new BufferedInputStream(in, BUFFER); ZipEntry zipEntry = new ZipEntry(filePathInDirectory); out.putNextEntry(zipEntry); int count; while ((count = origin.read(data, 0, BUFFER)) != -1) { out.write(data, 0, count); } origin.close(); in.close(); } } }
From source file:net.firejack.platform.core.utils.ArchiveUtils.java
/** * @param zipFile//from w ww. j av a2 s. c o m * @param files * @throws java.io.IOException */ public static void addFilesToZip(File zipFile, Map<String, File> files) throws IOException { // get a temp file File tempFile = File.createTempFile(zipFile.getName(), null); // delete it, otherwise you cannot rename your existing zip to it. FileUtils.deleteQuietly(tempFile); boolean renameOk = zipFile.renameTo(tempFile); if (!renameOk) { throw new RuntimeException( "could not rename the file " + zipFile.getAbsolutePath() + " to " + tempFile.getAbsolutePath()); } ZipInputStream zin = new ZipInputStream(new FileInputStream(tempFile)); ZipOutputStream out = new ZipOutputStream(new FileOutputStream(zipFile)); ZipEntry entry = zin.getNextEntry(); while (entry != null) { String name = entry.getName(); boolean notInFiles = true; for (Map.Entry<String, File> e : files.entrySet()) { if (e.getKey().equals(name)) { notInFiles = false; break; } } if (notInFiles) { out.putNextEntry(new ZipEntry(name)); IOUtils.copy(zin, out); } entry = zin.getNextEntry(); } // Close the streams zin.close(); // Compress the files for (Map.Entry<String, File> e : files.entrySet()) { InputStream in = new FileInputStream(e.getValue()); // Add ZIP entry to output stream. out.putNextEntry(new ZipEntry(e.getKey())); // Transfer bytes from the file to the ZIP file IOUtils.copy(in, out); // Complete the entry out.closeEntry(); IOUtils.closeQuietly(in); } // Complete the ZIP file IOUtils.closeQuietly(out); FileUtils.deleteQuietly(tempFile); }
From source file:de.brendamour.jpasskit.signing.PKSigningUtil.java
private static final void zip(final File directory, final File base, final ZipOutputStream zipOutputStream) throws IOException { File[] files = directory.listFiles(); byte[] buffer = new byte[ZIP_BUFFER_SIZE]; int read = 0; for (int i = 0, n = files.length; i < n; i++) { if (files[i].isDirectory()) { zip(files[i], base, zipOutputStream); } else {//w w w . j ava 2s. com FileInputStream fileInputStream = new FileInputStream(files[i]); ZipEntry entry = new ZipEntry(getRelativePathOfZipEntry(files[i], base)); zipOutputStream.putNextEntry(entry); while (-1 != (read = fileInputStream.read(buffer))) { zipOutputStream.write(buffer, 0, read); } fileInputStream.close(); } } }
From source file:net.firejack.platform.core.utils.ArchiveUtils.java
/** * @param zipFile/* w w w .j av a 2 s .com*/ * @param files * @throws java.io.IOException */ public static void addStreamsToZip(File zipFile, Map<String, InputStream> files) throws IOException { // get a temp file File tempFile = File.createTempFile(zipFile.getName(), null); // delete it, otherwise you cannot rename your existing zip to it. FileUtils.deleteQuietly(tempFile); boolean renameOk = zipFile.renameTo(tempFile); if (!renameOk) { throw new RuntimeException( "could not rename the file " + zipFile.getAbsolutePath() + " to " + tempFile.getAbsolutePath()); } ZipInputStream zin = new ZipInputStream(new FileInputStream(tempFile)); ZipOutputStream out = new ZipOutputStream(new FileOutputStream(zipFile)); ZipEntry entry = zin.getNextEntry(); while (entry != null) { String name = entry.getName(); boolean notInFiles = true; for (Map.Entry<String, InputStream> e : files.entrySet()) { if (e.getKey().equals(name)) { notInFiles = false; break; } } if (notInFiles) { out.putNextEntry(new ZipEntry(name)); IOUtils.copy(zin, out); } entry = zin.getNextEntry(); } // Close the streams zin.close(); // Compress the files for (Map.Entry<String, InputStream> e : files.entrySet()) { InputStream in = e.getValue(); // Add ZIP entry to output stream. out.putNextEntry(new ZipEntry(e.getKey())); // Transfer bytes from the file to the ZIP file IOUtils.copy(in, out); // Complete the entry out.closeEntry(); IOUtils.closeQuietly(in); } // Complete the ZIP file IOUtils.closeQuietly(out); FileUtils.deleteQuietly(tempFile); }
From source file:com.meltmedia.cadmium.core.util.WarUtils.java
/** * Writes a xml document to a zip file with an entry specified by the jbossWeb parameter. * @param outZip The zip output stream to write to. * @param jbossWeb The zip entry to add to the zip file. * @param doc The xml DOM document to write to the zip file. * @throws IOException /*from w ww .j a v a2 s . co m*/ * @throws TransformerFactoryConfigurationError * @throws TransformerConfigurationException * @throws TransformerException */ public static void storeXmlDocument(ZipOutputStream outZip, ZipEntry jbossWeb, Document doc) throws IOException, TransformerFactoryConfigurationError, TransformerConfigurationException, TransformerException { jbossWeb = new ZipEntry(jbossWeb.getName()); outZip.putNextEntry(jbossWeb); TransformerFactory tFactory = TransformerFactory.newInstance(); Transformer transformer = tFactory.newTransformer(); transformer.setOutputProperty(OutputKeys.INDENT, "yes"); transformer.setOutputProperty("{http://xml.apache.org/xslt}indent-amount", "2"); DOMSource source = new DOMSource(doc); StreamResult result = new StreamResult(outZip); transformer.transform(source, result); outZip.closeEntry(); }
From source file:com.thejustdo.util.Utils.java
/** * Creates a ZIP file containing all the files inside a directory. * @param location Directory to read./*w ww . java 2 s. c om*/ * @param pathname Name and path where to store the file. * @throws FileNotFoundException If can't find the initial directory. * @throws IOException If can't read/write. */ public static void zipDirectory(File location, File pathname) throws FileNotFoundException, IOException { BufferedInputStream origin; FileOutputStream dest = new FileOutputStream(pathname); ZipOutputStream out = new ZipOutputStream(new BufferedOutputStream(dest)); byte data[] = new byte[2048]; // 1. Get a list of files from current directory String files[] = location.list(); File f; // 2. Adding each file to the zip-set. for (String s : files) { log.info(String.format("Adding: %s", s)); f = new File(location, s); FileInputStream fi = new FileInputStream(f); origin = new BufferedInputStream(fi, 2048); ZipEntry entry = new ZipEntry(location.getName() + File.separator + s); out.putNextEntry(entry); int count; while ((count = origin.read(data, 0, 2048)) != -1) { out.write(data, 0, count); } origin.close(); } out.close(); }
From source file:it.geosolutions.tools.compress.file.Compressor.java
/** * @param outputDir//from w ww .j ava 2 s.com * The directory where the zipfile will be created * @param zipFileBaseName * The basename of hte zip file (i.e.: a .zip will be appended) * @param folder * The folder that will be compressed * @return The created zipfile, or null if an error occurred. * @deprecated TODO UNTESTED */ public static File deflate(final File outputDir, final String zipFileBaseName, final File folder) { // Create a buffer for reading the files byte[] buf = new byte[4096]; // Create the ZIP file final File outZipFile = new File(outputDir, zipFileBaseName + ".zip"); if (outZipFile.exists()) { if (LOGGER.isInfoEnabled()) LOGGER.info("The output file already exists: " + outZipFile); return outZipFile; } ZipOutputStream out = null; BufferedOutputStream bos = null; FileOutputStream fos = null; try { fos = new FileOutputStream(outZipFile); bos = new BufferedOutputStream(fos); out = new ZipOutputStream(bos); Collector c = new Collector(null); List<File> files = c.collect(folder); // Compress the files for (File file : files) { FileInputStream in = null; try { in = new FileInputStream(file); if (file.isDirectory()) { out.putNextEntry(new ZipEntry(Path.toRelativeFile(folder, file).getPath())); } else { // Add ZIP entry to output stream. out.putNextEntry(new ZipEntry(FilenameUtils.getBaseName(file.getName()))); // Transfer bytes from the file to the ZIP file int len; while ((len = in.read(buf)) > 0) { out.write(buf, 0, len); } } } finally { try { // Complete the entry out.closeEntry(); } catch (IOException e) { } IOUtils.closeQuietly(in); } } } catch (IOException e) { LOGGER.error(e.getLocalizedMessage(), e); return null; } finally { // Complete the ZIP file IOUtils.closeQuietly(bos); IOUtils.closeQuietly(fos); IOUtils.closeQuietly(out); } return outZipFile; }
From source file:net.sourceforge.fenixedu.presentationTier.Action.externalServices.epfl.ExportPhdIndividualProgramProcessesInHtml.java
static byte[] createZip(final PhdProgramPublicCandidacyHashCode hashCode) { final ByteArrayOutputStream outputStream = new ByteArrayOutputStream(); ZipOutputStream zip = null; try {// www . j a va2s . co m zip = new ZipOutputStream(outputStream); int count = 1; for (final PhdProgramProcessDocument document : hashCode.getIndividualProgramProcess() .getCandidacyProcessDocuments()) { final ZipEntry zipEntry = new ZipEntry(count + "-" + document.getFilename()); zip.putNextEntry(zipEntry); // TODO: use in local context copy(new ByteArrayInputStream(new // byte[20]), zip); copy(document.getStream(), zip); zip.closeEntry(); count++; } } catch (FileNotFoundException e) { logger.error(e.getMessage(), e); } catch (IOException e) { logger.error(e.getMessage(), e); } finally { if (zip != null) { try { zip.flush(); zip.close(); } catch (IOException e) { logger.error(e.getMessage(), e); } } } return outputStream.toByteArray(); }
From source file:com.nridge.core.base.std.FilUtl.java
/** * Compresses the input path name into a ZIP output file stream. The * method will recursively process all files and sub folders within * the input path file.// w w w . j ava2 s. c o m * * @param aPathName Identifies the name of the folder to compress. * @param aZipOutStream A previously opened ZIP output file stream. * @throws IOException Related to opening the file streams and * related read/write operations. */ static public void zipPath(String aPathName, ZipOutputStream aZipOutStream) throws IOException { int byteCount; byte[] ioBuf; String[] fileList; File zipFile, inFile; FileInputStream fileIn; zipFile = new File(aPathName); if (!zipFile.isDirectory()) return; fileList = zipFile.list(); if (fileList == null) return; for (String fileName : fileList) { inFile = new File(aPathName, fileName); if (inFile.isDirectory()) zipPath(inFile.getPath(), aZipOutStream); else { ioBuf = new byte[FILE_IO_BUFFER_SIZE]; fileIn = new FileInputStream(inFile); aZipOutStream.putNextEntry(new ZipEntry(inFile.getName())); byteCount = fileIn.read(ioBuf); while (byteCount > 0) { aZipOutStream.write(ioBuf, 0, byteCount); byteCount = fileIn.read(ioBuf); } fileIn.close(); } } }
From source file:edu.kit.dama.util.ZipUtils.java
/** * Write a list of files into a ZipOutputStream. The provided base path is * used to keep the structure defined by pFileList within the zip file.<BR/> * Due to the fact, that we have only one single base path, all files within * 'pFileList' must have in the same base directory. Adding files from a * higher level will cause unexpected zip entries. * * @param pFileList The list of input files * @param pBasePath The base path the will be removed from all file paths * before creating a new zip entry/* w w w. ja v a 2 s . co m*/ * @param pZipOut The zip output stream * @throws IOException If something goes wrong, in most cases if there are * problems with reading the input files or writing into the output file */ private static void zip(File[] pFileList, String pBasePath, ZipOutputStream pZipOut) throws IOException { // Create a buffer for reading the files byte[] buf = new byte[1024]; try { // Compress the files LOGGER.debug("Adding {} files to archive", pFileList.length); for (File pFileList1 : pFileList) { String entryName = pFileList1.getPath().replaceAll(Pattern.quote(pBasePath), ""); if (entryName.startsWith(File.separator)) { entryName = entryName.substring(1); } if (pFileList1.isDirectory()) { LOGGER.debug("Adding directory entry"); //add empty folders, too pZipOut.putNextEntry(new ZipEntry((entryName + File.separator).replaceAll("\\\\", "/"))); pZipOut.closeEntry(); File[] fileList = pFileList1.listFiles(); if (fileList.length != 0) { LOGGER.debug("Adding directory content recursively"); zip(fileList, pBasePath, pZipOut); } else { LOGGER.debug("Skipping recursive call due to empty directory"); } //should we close the entry here?? //pZipOut.closeEntry(); } else { LOGGER.debug("Adding file entry"); try (final FileInputStream in = new FileInputStream(pFileList1)) { // Add ZIP entry to output stream. pZipOut.putNextEntry(new ZipEntry(entryName.replaceAll("\\\\", "/"))); // Transfer bytes from the file to the ZIP file int len; while ((len = in.read(buf)) > 0) { pZipOut.write(buf, 0, len); } // Complete the entry LOGGER.debug("Closing file entry"); pZipOut.closeEntry(); } } } } catch (IOException ioe) { LOGGER.error( "Aborting zip process due to an IOException caused by any zip stream (FileInput or ZipOutput)", ioe); throw ioe; } catch (RuntimeException e) { LOGGER.error("Aborting zip process due to an unexpected exception", e); throw new IOException("Unexpected exception during zip operation", e); } }