List of usage examples for org.apache.commons.compress.archivers.zip ZipArchiveOutputStream putArchiveEntry
public void putArchiveEntry(ArchiveEntry archiveEntry) throws IOException
From source file:org.apache.james.mailbox.backup.Zipper.java
private void storeInArchive(MailboxMessage message, ZipArchiveOutputStream archiveOutputStream) throws IOException { String entryId = message.getMessageId().serialize(); ZipArchiveEntry archiveEntry = (ZipArchiveEntry) archiveOutputStream.createArchiveEntry(new File(entryId), entryId);/*from ww w .j ava2s . c o m*/ archiveEntry.addExtraField(new SizeExtraField(message.getFullContentOctets())); archiveEntry.addExtraField(new UidExtraField(message.getUid().asLong())); archiveEntry.addExtraField(new MessageIdExtraField(message.getMessageId().serialize())); archiveEntry.addExtraField(new MailboxIdExtraField(message.getMailboxId().serialize())); archiveEntry.addExtraField(new InternalDateExtraField(message.getInternalDate())); archiveEntry.addExtraField(new FlagsExtraField(message.createFlags())); archiveOutputStream.putArchiveEntry(archiveEntry); IOUtils.copy(message.getFullContent(), archiveOutputStream); archiveOutputStream.closeArchiveEntry(); }
From source file:org.apache.karaf.tooling.ArchiveMojo.java
private void addFileToZip(ZipArchiveOutputStream tOut, Path f, String base) throws IOException { if (Files.isDirectory(f)) { String entryName = base + f.getFileName().toString() + "/"; ZipArchiveEntry zipEntry = new ZipArchiveEntry(entryName); tOut.putArchiveEntry(zipEntry); tOut.closeArchiveEntry();// w w w .j a v a 2 s. c o m try (DirectoryStream<Path> children = Files.newDirectoryStream(f)) { for (Path child : children) { addFileToZip(tOut, child, entryName); } } } else if (useSymLinks && Files.isSymbolicLink(f)) { String entryName = base + f.getFileName().toString(); ZipArchiveEntry zipEntry = new ZipArchiveEntry(entryName); zipEntry.setUnixMode(UnixStat.LINK_FLAG | UnixStat.DEFAULT_FILE_PERM); tOut.putArchiveEntry(zipEntry); tOut.write(Files.readSymbolicLink(f).toString().getBytes()); tOut.closeArchiveEntry(); } else { String entryName = base + f.getFileName().toString(); ZipArchiveEntry zipEntry = new ZipArchiveEntry(entryName); zipEntry.setSize(Files.size(f)); if (entryName.contains("/bin/") || (!usePathPrefix && entryName.startsWith("bin"))) { if (!entryName.endsWith(".bat")) { zipEntry.setUnixMode(0755); } else { zipEntry.setUnixMode(0644); } } tOut.putArchiveEntry(zipEntry); Files.copy(f, tOut); tOut.closeArchiveEntry(); } }
From source file:org.apache.openejb.maven.plugin.BuildTomEEMojo.java
private void zip(final ZipArchiveOutputStream zip, final File f, final String prefix) throws IOException { final String path = f.getPath().replace(prefix, "").replace(File.separator, "/"); final ZipArchiveEntry archiveEntry = new ZipArchiveEntry(f, path); if (isSh(path)) { archiveEntry.setUnixMode(0755);// ww w . j a va2s . c o m } zip.putArchiveEntry(archiveEntry); if (f.isDirectory()) { zip.closeArchiveEntry(); final File[] files = f.listFiles(); if (files != null) { for (final File child : files) { zip(zip, child, prefix); } } } else { IO.copy(f, zip); zip.closeArchiveEntry(); } }
From source file:org.apache.sis.internal.maven.Assembler.java
/** * Creates the distribution file.//w w w .j ava2 s .c o m * * @throws MojoExecutionException if the plugin execution failed. */ @Override public void execute() throws MojoExecutionException { final File sourceDirectory = new File(rootDirectory, ARTIFACT_PATH); if (!sourceDirectory.isDirectory()) { throw new MojoExecutionException("Directory not found: " + sourceDirectory); } final File targetDirectory = new File(rootDirectory, TARGET_DIRECTORY); final String version = project.getVersion(); final String artifactBase = FINALNAME_PREFIX + version; try { final File targetFile = new File(distributionDirectory(targetDirectory), artifactBase + ".zip"); final ZipArchiveOutputStream zip = new ZipArchiveOutputStream(targetFile); try { zip.setLevel(9); appendRecursively(sourceDirectory, artifactBase, zip, new byte[8192]); /* * At this point, all the "application/sis-console/src/main/artifact" and sub-directories * have been zipped. Now generate the Pack200 file and zip it directly (without creating * a temporary "sis.pack.gz" file). */ final Packer packer = new Packer(project.getName(), project.getUrl(), version, targetDirectory); final ZipArchiveEntry entry = new ZipArchiveEntry( artifactBase + '/' + LIB_DIRECTORY + '/' + FATJAR_FILE + PACK_EXTENSION); entry.setMethod(ZipArchiveEntry.STORED); zip.putArchiveEntry(entry); packer.preparePack200(FATJAR_FILE + ".jar").pack(new FilterOutputStream(zip) { /* * Closes the archive entry, not the ZIP file. */ @Override public void close() throws IOException { zip.closeArchiveEntry(); } }); } finally { zip.close(); } } catch (IOException e) { throw new MojoExecutionException(e.getLocalizedMessage(), e); } }
From source file:org.apache.sis.internal.maven.Assembler.java
/** * Adds the given file in the ZIP file. If the given file is a directory, then this method * recursively adds all files contained in this directory. This method is invoked for zipping * the "application/sis-console/src/main/artifact" directory and sub-directories before to zip * the Pack200 file./*from ww w . j a va2s . c o m*/ */ private void appendRecursively(final File file, String relativeFile, final ZipArchiveOutputStream out, final byte[] buffer) throws IOException { if (file.isDirectory()) { relativeFile += '/'; } final ZipArchiveEntry entry = new ZipArchiveEntry(file, relativeFile); if (file.canExecute()) { entry.setUnixMode(0744); } out.putArchiveEntry(entry); if (!entry.isDirectory()) { final FileInputStream in = new FileInputStream(file); try { int n; while ((n = in.read(buffer)) >= 0) { out.write(buffer, 0, n); } } finally { in.close(); } } out.closeArchiveEntry(); if (entry.isDirectory()) { for (final String filename : file.list(this)) { appendRecursively(new File(file, filename), relativeFile.concat(filename), out, buffer); } } }
From source file:org.apache.slider.tools.TestUtility.java
public static void addDir(File dirObj, ZipArchiveOutputStream zipFile, String prefix) throws IOException { for (File file : dirObj.listFiles()) { if (file.isDirectory()) { addDir(file, zipFile, prefix + file.getName() + File.separator); } else {//from w w w . jav a 2s . c o m log.info("Adding to zip - " + prefix + file.getName()); zipFile.putArchiveEntry(new ZipArchiveEntry(prefix + file.getName())); IOUtils.copy(new FileInputStream(file), zipFile); zipFile.closeArchiveEntry(); } } }
From source file:org.apache.tika.server.writer.ZipWriter.java
private static void zipStoreBuffer(ZipArchiveOutputStream zip, String name, byte[] dataBuffer) throws IOException { ZipEntry zipEntry = new ZipEntry(name != null ? name : UUID.randomUUID().toString()); zipEntry.setMethod(ZipOutputStream.STORED); zipEntry.setSize(dataBuffer.length); CRC32 crc32 = new CRC32(); crc32.update(dataBuffer);/*from www . ja v a 2 s.c o m*/ zipEntry.setCrc(crc32.getValue()); try { zip.putArchiveEntry(new ZipArchiveEntry(zipEntry)); } catch (ZipException ex) { if (name != null) { zipStoreBuffer(zip, "x-" + name, dataBuffer); return; } } zip.write(dataBuffer); zip.closeArchiveEntry(); }
From source file:org.beangle.commons.archiver.ZipUtils.java
public static File zip(List<String> fileNames, String zipPath, String encoding) { try {//from ww w .j a va 2s . c o m FileOutputStream f = new FileOutputStream(zipPath); ZipArchiveOutputStream zos = (ZipArchiveOutputStream) new ArchiveStreamFactory() .createArchiveOutputStream(ArchiveStreamFactory.ZIP, f); if (null != encoding) { zos.setEncoding(encoding); } for (int i = 0; i < fileNames.size(); i++) { String fileName = fileNames.get(i); String entryName = StringUtils.substringAfterLast(fileName, File.separator); ZipArchiveEntry entry = new ZipArchiveEntry(entryName); zos.putArchiveEntry(entry); FileInputStream fis = new FileInputStream(fileName); IOUtils.copy(fis, zos); fis.close(); zos.closeArchiveEntry(); } zos.close(); return new File(zipPath); } catch (Exception e) { throw new RuntimeException(e); } }
From source file:org.beangle.ems.util.ZipUtils.java
/** * <p>/*from w ww . ja va2 s. com*/ * zip. * </p> * * @param fileNames a {@link java.util.List} object. * @param zipPath a {@link java.lang.String} object. * @param encoding a {@link java.lang.String} object. * @return a {@link java.io.File} object. */ public static File zip(List<String> fileNames, String zipPath, String encoding) { try { FileOutputStream f = new FileOutputStream(zipPath); ZipArchiveOutputStream zos = (ZipArchiveOutputStream) new ArchiveStreamFactory() .createArchiveOutputStream(ArchiveStreamFactory.ZIP, f); if (null != encoding) { zos.setEncoding(encoding); } for (int i = 0; i < fileNames.size(); i++) { String fileName = fileNames.get(i); String entryName = Strings.substringAfterLast(fileName, File.separator); ZipArchiveEntry entry = new ZipArchiveEntry(entryName); zos.putArchiveEntry(entry); FileInputStream fis = new FileInputStream(fileName); IOs.copy(fis, zos); fis.close(); zos.closeArchiveEntry(); } zos.close(); return new File(zipPath); } catch (Exception e) { throw new RuntimeException(e); } }
From source file:org.betaconceptframework.astroboa.engine.jcr.io.SerializationBean.java
private void addContentToZip(ZipArchiveOutputStream os, SerializationReport serializationReport, Session session, SerializationConfiguration serializationConfiguration, String filename, ContentObjectCriteria contentObjectCriteria) throws IOException, Exception, SAXException, RepositoryException, PathNotFoundException { long start = System.currentTimeMillis(); Serializer serializer = null;//from w w w. ja v a 2 s. c o m try { //Create entry os.putArchiveEntry(new ZipArchiveEntry(filename + ".xml")); serializer = new Serializer(os, cmsRepositoryEntityUtils, session, serializationConfiguration); serializer.setSerializationReport(serializationReport); //Create document serializer.start(); //Create root element createRootElement(serializer, CmsConstants.REPOSITORY_PREFIXED_NAME, ResourceRepresentationType.XML, entityTypeToSerialize != null && (entityTypeToSerialize == CmsEntityType.OBJECT || entityTypeToSerialize == CmsEntityType.REPOSITORY)); switch (entityTypeToSerialize) { case OBJECT: if (contentObjectCriteria != null) { serializeObjectsAndTheirDependencies(contentObjectCriteria, serializer, session, serializationReport); } else { serializeAllNodesRepresentingObjects(serializer, session, serializationReport); } break; case REPOSITORY_USER: serializeAllNodesRepresentingRepositoryUsers(serializer, session, serializationReport); break; case TAXONOMY: serializeAllNodesRepresentingTaxonomies(serializer, session, serializationReport); break; case ORGANIZATION_SPACE: serializeNodeRepresentingOrganizationSpace(serializer, session, serializationReport); break; case REPOSITORY: serializeAllNodesRepresentingRepositoryUsers(serializer, session, serializationReport); serializeAllNodesRepresentingTaxonomies(serializer, session, serializationReport); serializeAllNodesRepresentingObjects(serializer, session, serializationReport); serializeNodeRepresentingOrganizationSpace(serializer, session, serializationReport); break; default: break; } serializer.closeEntity(CmsConstants.REPOSITORY_PREFIXED_NAME); //End document serializer.end(); //Close archive entry os.closeArchiveEntry(); logger.debug("Added content toy zip in {}", DurationFormatUtils.formatDuration(System.currentTimeMillis() - start, "HH:mm:ss.SSSSSS")); } finally { serializer = null; } }