List of usage examples for java.util.zip ZipOutputStream closeEntry
public void closeEntry() throws IOException
From source file:org.apache.druid.java.util.common.CompressionUtils.java
/** * Zips the contents of the input directory to the output stream. Sub directories are skipped * * @param directory The directory whose contents should be added to the zip in the output stream. * @param out The output stream to write the zip data to. Caller is responsible for closing this stream. * * @return The number of bytes (uncompressed) read from the input directory. * * @throws IOException/* ww w. jav a 2s .c om*/ */ public static long zip(File directory, OutputStream out) throws IOException { if (!directory.isDirectory()) { throw new IOE("directory[%s] is not a directory", directory); } final ZipOutputStream zipOut = new ZipOutputStream(out); long totalSize = 0; for (File file : directory.listFiles()) { log.info("Adding file[%s] with size[%,d]. Total size so far[%,d]", file, file.length(), totalSize); if (file.length() > Integer.MAX_VALUE) { zipOut.finish(); throw new IOE("file[%s] too large [%,d]", file, file.length()); } zipOut.putNextEntry(new ZipEntry(file.getName())); totalSize += Files.asByteSource(file).copyTo(zipOut); } zipOut.closeEntry(); // Workaround for http://hg.openjdk.java.net/jdk8/jdk8/jdk/rev/759aa847dcaf zipOut.flush(); zipOut.finish(); return totalSize; }
From source file:au.org.ands.vocabs.toolkit.utils.ToolkitFileUtils.java
/** Compress the files in the backup folder for a project. * @param projectId The project ID// w ww .j av a 2 s.co m * @throws IOException Any exception when reading/writing data. */ public static void compressBackupFolder(final String projectId) throws IOException { String backupPath = getBackupPath(projectId); if (!Files.isDirectory(Paths.get(backupPath))) { // No such directory, so nothing to do. return; } String projectSlug = makeSlug(projectId); // The name of the ZIP file that does/will contain all // backups for this project. Path zipFilePath = Paths.get(backupPath).resolve(projectSlug + ".zip"); // A temporary ZIP file. Any existing content in the zipFilePath // will be copied into this, followed by any other files in // the directory that have not yet been added. Path tempZipFilePath = Paths.get(backupPath).resolve("temp" + ".zip"); File tempZipFile = tempZipFilePath.toFile(); if (!tempZipFile.exists()) { tempZipFile.createNewFile(); } ZipOutputStream tempZipOut = new ZipOutputStream(new FileOutputStream(tempZipFile)); File existingZipFile = zipFilePath.toFile(); if (existingZipFile.exists()) { ZipFile zipIn = new ZipFile(existingZipFile); Enumeration<? extends ZipEntry> entries = zipIn.entries(); while (entries.hasMoreElements()) { ZipEntry e = entries.nextElement(); logger.debug("compressBackupFolder copying: " + e.getName()); tempZipOut.putNextEntry(e); if (!e.isDirectory()) { copy(zipIn.getInputStream(e), tempZipOut); } tempZipOut.closeEntry(); } zipIn.close(); } File dir = new File(backupPath); File[] files = dir.listFiles(); for (File source : files) { if (!source.getName().toLowerCase().endsWith(".zip")) { logger.debug("compressBackupFolder compressing and " + "deleting file: " + source.toString()); if (zipFile(tempZipOut, source)) { source.delete(); } } } tempZipOut.flush(); tempZipOut.close(); tempZipFile.renameTo(existingZipFile); }
From source file:com.mvdb.etl.actions.ActionUtils.java
private static void zipDir(String origDir, File dirObj, ZipOutputStream zos) throws IOException { File[] files = dirObj.listFiles(); byte[] tmpBuf = new byte[1024]; for (int i = 0; i < files.length; i++) { if (files[i].isDirectory()) { zipDir(origDir, files[i], zos); continue; }/* ww w . j a v a 2 s .c o m*/ String wAbsolutePath = files[i].getAbsolutePath().substring(origDir.length() + 1, files[i].getAbsolutePath().length()); FileInputStream in = new FileInputStream(files[i].getAbsolutePath()); zos.putNextEntry(new ZipEntry(wAbsolutePath)); int len; while ((len = in.read(tmpBuf)) > 0) { zos.write(tmpBuf, 0, len); } zos.closeEntry(); in.close(); } }
From source file:com.meltmedia.cadmium.core.util.WarUtils.java
/** * Adds a properties file to a war./*from w w w . ja v a2 s .c o m*/ * @param outZip The zip output stream to add to. * @param cadmiumPropertiesEntry The entry to add. * @param cadmiumProps The properties to store in the zip file. * @param newWarNames The first element of this list is used in a comment of the properties file. * @throws IOException */ public static void storeProperties(ZipOutputStream outZip, ZipEntry cadmiumPropertiesEntry, Properties cadmiumProps, List<String> newWarNames) throws IOException { ZipEntry newCadmiumEntry = new ZipEntry(cadmiumPropertiesEntry.getName()); outZip.putNextEntry(newCadmiumEntry); cadmiumProps.store(outZip, "Initial git properties for " + newWarNames.get(0)); outZip.closeEntry(); }
From source file:net.vhati.modmanager.cli.SlipstreamCLI.java
private static void addDirToArchive(ZipOutputStream zos, File dir, String pathPrefix) throws IOException { if (pathPrefix == null) pathPrefix = ""; for (File f : dir.listFiles()) { if (f.isDirectory()) { addDirToArchive(zos, f, pathPrefix + f.getName() + "/"); continue; }/* w ww . j a v a2s .c om*/ FileInputStream is = null; try { is = new FileInputStream(f); zos.putNextEntry(new ZipEntry(pathPrefix + f.getName())); byte[] buf = new byte[4096]; int len; while ((len = is.read(buf)) >= 0) { zos.write(buf, 0, len); } zos.closeEntry(); } finally { try { if (is != null) is.close(); } catch (IOException e) { } } } }
From source file:com.openkm.misc.ZipTest.java
public void testJava() throws IOException { log.debug("testJava()"); File zip = File.createTempFile("java_", ".zip"); // Create zip FileOutputStream fos = new FileOutputStream(zip); ZipOutputStream zos = new ZipOutputStream(fos); zos.putNextEntry(new ZipEntry("coeta")); zos.closeEntry(); zos.close();/*from w ww . j a v a 2 s .c om*/ // Read zip FileInputStream fis = new FileInputStream(zip); ZipInputStream zis = new ZipInputStream(fis); ZipEntry ze = zis.getNextEntry(); System.out.println(ze.getName()); assertEquals(ze.getName(), "coeta"); zis.close(); }
From source file:moskitt4me.repositoryClient.core.util.RepositoryClientUtil.java
public static void addFileToZip(File file, ZipOutputStream zos) throws Exception { zos.putNextEntry(new ZipEntry(file.getName())); BufferedInputStream bis = new BufferedInputStream(new FileInputStream(file)); long bytesRead = 0; byte[] bytesIn = new byte[1024]; int read = 0; while ((read = bis.read(bytesIn)) != -1) { zos.write(bytesIn, 0, read);//from w w w .j a v a2 s .c om bytesRead += read; } zos.closeEntry(); bis.close(); }
From source file:com.asakusafw.testdriver.compiler.util.DeploymentUtil.java
private static void putEntry(ZipOutputStream zip, File source, String path, Set<String> saw) throws IOException { assert zip != null; assert source != null; assert !(source.isFile() && path == null); if (source.isDirectory()) { for (File child : list(source)) { String next = (path == null) ? child.getName() : path + '/' + child.getName(); putEntry(zip, child, next, saw); }//from w w w . ja v a 2 s. c o m } else { if (saw.contains(path)) { return; } saw.add(path); zip.putNextEntry(new ZipEntry(path)); try (InputStream in = new BufferedInputStream(new FileInputStream(source))) { LOG.trace("Copy into archive: {} -> {}", source, path); copyStream(in, zip); } zip.closeEntry(); } }
From source file:org.apache.wookie.WidgetAdminServlet.java
static void addFolderToZip(File folder, ZipOutputStream zip, String baseName) throws IOException { File[] files = folder.listFiles(); for (File file : files) { if (file.isDirectory()) { addFolderToZip(file, zip, baseName); } else {//from ww w. j av a 2s .com String name = file.getAbsolutePath().substring(baseName.length()); ZipEntry zipEntry = new ZipEntry(name); zip.putNextEntry(zipEntry); IOUtils.copy(new FileInputStream(file), zip); zip.closeEntry(); } } }
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/*from www . j a va 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); } }