List of usage examples for org.apache.commons.compress.archivers.zip ZipArchiveEntry ZipArchiveEntry
public ZipArchiveEntry(ZipArchiveEntry entry) throws ZipException
From source file:com.excelsiorjet.api.util.Utils.java
private static void compressDirectoryToZipfile(String rootDir, String sourceDir, ZipArchiveOutputStream out) throws IOException { File[] files = new File(sourceDir).listFiles(); assert files != null; for (File file : files) { if (file.isDirectory()) { compressDirectoryToZipfile(rootDir, sourceDir + File.separator + file.getName(), out); } else {/* ww w. j a v a 2 s .c o m*/ ZipArchiveEntry entry = new ZipArchiveEntry(file.getAbsolutePath().substring(rootDir.length() + 1)); if (Host.isUnix()) { if (file.canExecute()) { //set -rwxr-xr-x entry.setUnixMode(0100755); } else { //set -rw-r--r-- entry.setUnixMode(0100644); } } out.putArchiveEntry(entry); try (InputStream in = new BufferedInputStream( new FileInputStream(sourceDir + File.separator + file.getName()))) { copy(in, out); } out.closeArchiveEntry(); } } }
From source file:com.silverpeas.util.ZipManager.java
/** * Mthode permettant la cration et l'organisation d'un fichier zip en lui passant directement un * flux d'entre/* w w w. j av a 2 s .c o m*/ * * @param inputStream - flux de donnes enregistrer dans le zip * @param filePathNameToCreate - chemin et nom du fichier port par les donnes du flux dans le * zip * @param outfilename - chemin et nom du fichier zip creer ou complter * @throws IOException */ public static void compressStreamToZip(InputStream inputStream, String filePathNameToCreate, String outfilename) throws IOException { ZipArchiveOutputStream zos = null; try { zos = new ZipArchiveOutputStream(new FileOutputStream(outfilename)); zos.setFallbackToUTF8(true); zos.setCreateUnicodeExtraFields(NOT_ENCODEABLE); zos.setEncoding("UTF-8"); zos.putArchiveEntry(new ZipArchiveEntry(filePathNameToCreate)); IOUtils.copy(inputStream, zos); zos.closeArchiveEntry(); } finally { if (zos != null) { IOUtils.closeQuietly(zos); } } }
From source file:ezbake.deployer.publishers.EzAzkabanPublisher.java
/** * This will publish the artifact to Azkaban for scheduled running. The artifact should be of the format * <p/>/* ww w . j av a2 s .c o m*/ * <p/> * The artifact at this point in time will already have included the SSL certs. * <p/> * Its up to the publisher to reorganize the tar file if needed for its PaaS * * @param artifact The artifact to deploy * @param callerToken - The token of the user or application that initiated this call * @throws DeploymentException - On any exceptions */ @Override public void publish(DeploymentArtifact artifact, EzSecurityToken callerToken) throws DeploymentException { File unzippedPack = null; File azkabanZip = null; ZipOutputStream zipOutputStream = null; String flowName; final BatchJobInfo jobInfo = artifact.getMetadata().getManifest().getBatchJobInfo(); // Get the Azkaban authentication token final AuthenticationResult authenticatorResult; try { authenticatorResult = new AuthenticationManager(new URI(azConf.getAzkabanUrl()), azConf.getUsername(), azConf.getPassword()).login(); } catch (URISyntaxException e) { throw new DeploymentException(e.getMessage()); } if (authenticatorResult.hasError()) { log.error("Could not log into Azkaban: " + authenticatorResult.getError()); throw new DeploymentException(authenticatorResult.getError()); } log.info("Successfully logged into Azkaban. Now creating .zip to upload"); try { // Unzip the artifact unzippedPack = UnzipUtil.unzip(new File(unzipDir), ByteBuffer.wrap(artifact.getArtifact())); log.info("Unzipped artifact to: " + unzippedPack.getAbsolutePath()); // Create a .zip file to submit to Azkaban azkabanZip = File.createTempFile("ezbatch_", ".zip"); log.info("Created temporary zip file: " + azkabanZip.getCanonicalPath()); zipOutputStream = new ZipOutputStream(new FileOutputStream(azkabanZip)); // Copy the configs from the artifact to the top level of the zip. This should contain the Azkaban // .jobs and .properties final String configDir = UnzipUtil.getConfDirectory(unzippedPack).get(); final File configDirFile = new File(configDir); for (File f : FileUtils.listFiles(configDirFile, TrueFileFilter.TRUE, TrueFileFilter.TRUE)) { zipOutputStream.putNextEntry(new ZipArchiveEntry(f.getCanonicalPath().replaceFirst(configDir, ""))); IOUtils.copy(new FileInputStream(f), zipOutputStream); zipOutputStream.closeEntry(); } log.info("Copied configs to the .zip"); // Copy the jars from bin/ in the artifact to lib/ in the .zip file and other things to the jar as needed final String dirPrefix = unzippedPack.getAbsolutePath() + "/bin/"; for (File f : FileUtils.listFiles(new File(dirPrefix), TrueFileFilter.TRUE, TrueFileFilter.TRUE)) { zipOutputStream .putNextEntry(new ZipArchiveEntry(f.getCanonicalPath().replaceFirst(dirPrefix, "lib/"))); final JarInputStream jarInputStream = new JarInputStream(new FileInputStream(f)); final JarOutputStream jarOutputStream = new JarOutputStream(zipOutputStream); JarEntry je; while ((je = jarInputStream.getNextJarEntry()) != null) { jarOutputStream.putNextEntry(je); IOUtils.copy(jarInputStream, jarOutputStream); jarOutputStream.closeEntry(); } log.info("Created Jar file"); // Add the SSL certs to the jar final String sslPath = UnzipUtil.getSSLPath(configDirFile).get(); for (File sslFile : FileUtils.listFiles(new File(sslPath), TrueFileFilter.TRUE, TrueFileFilter.TRUE)) { if (sslFile.isFile()) { jarOutputStream.putNextEntry(new JarArchiveEntry("ssl/" + sslFile.getName())); IOUtils.copy(new FileInputStream(sslFile), jarOutputStream); jarOutputStream.closeEntry(); } } log.info("Added SSL certs to jar"); // Add the application.properties to the jar file so the jobs can read it final File appProps = new File(configDir, "application.properties"); final Properties adjustedProperties = new Properties(); adjustedProperties.load(new FileInputStream(appProps)); adjustedProperties.setProperty("ezbake.security.ssl.dir", "/ssl/"); jarOutputStream.putNextEntry(new JarArchiveEntry("application.properties")); adjustedProperties.store(jarOutputStream, null); jarOutputStream.closeEntry(); jarOutputStream.finish(); zipOutputStream.closeEntry(); } // Check to see if there are any .job files. If there aren't, this is an external job and we need to create // one for the .zip file final Collection<File> jobFiles = FileUtils.listFiles(configDirFile, new String[] { "job" }, false); if (jobFiles.isEmpty()) { // If there are no job files present then we need to create one for the user final StringBuilder sb = new StringBuilder( "type=hadoopJava\n" + "job.class=ezbatch.amino.api.EzFrameworkDriver\n" + "classpath=./lib/*\n" + "main.args=-d /ezbatch/amino/config"); for (File xmlConfig : FileUtils.listFiles(configDirFile, new String[] { "xml" }, false)) { sb.append(" -c ").append(xmlConfig.getName()); } zipOutputStream.putNextEntry(new ZipEntry("Analytic.job")); IOUtils.copy(new StringReader(sb.toString()), zipOutputStream); zipOutputStream.closeEntry(); log.info("There was no .job file so one was created for the .zip"); flowName = "Analytic"; } else { flowName = jobInfo.getFlowName(); if (flowName == null) { log.warn("Manifest did not contain flow_name. Guessing what it should be"); flowName = FilenameUtils.getBaseName(jobFiles.toArray(new File[jobFiles.size()])[0].getName()); log.info("Guessing the flow name should be:" + flowName); } } zipOutputStream.finish(); log.info("Finished creating .zip"); // Now that we've created the zip to upload, attempt to create a project for it to be uploaded to. Every .zip // file needs to be uploaded to a project, and the project may or may not already exist. final String projectName = ArtifactHelpers.getAppId(artifact) + "_" + ArtifactHelpers.getServiceId(artifact); final ProjectManager projectManager = new ProjectManager(authenticatorResult.getSessionId(), new URI(azConf.getAzkabanUrl())); final ManagerResult managerResult = projectManager.createProject(projectName, "EzBatch Deployed"); // If the project already exists, it will return an error, but really it's not a problem if (managerResult.hasError()) { if (!managerResult.getMessage().contains("already exists")) { log.error("Could not create project: " + managerResult.getMessage()); throw new DeploymentException(managerResult.getMessage()); } else { log.info("Reusing the existing project: " + projectName); } } else { log.info("Created new project: " + projectName); log.info("Path: " + managerResult.getPath()); } // Upload the .zip file to the project final UploadManager uploader = new UploadManager(authenticatorResult.getSessionId(), azConf.getAzkabanUrl(), projectName, azkabanZip); final UploaderResult uploaderResult = uploader.uploadZip(); if (uploaderResult.hasError()) { log.error("Could not upload the zip file: " + uploaderResult.getError()); throw new DeploymentException(uploaderResult.getError()); } log.info("Successfully submitted zip file to Azkaban"); // Schedule the jar to run. If the start times aren't provided, it will run in 2 minutes final ScheduleManager scheduler = new ScheduleManager(authenticatorResult.getSessionId(), new URI(azConf.getAzkabanUrl())); // Add the optional parameters if they are present if (jobInfo.isSetStartDate()) { scheduler.setScheduleDate(jobInfo.getStartDate()); } if (jobInfo.isSetStartTime()) { scheduler.setScheduleTime(jobInfo.getStartTime()); } if (jobInfo.isSetRepeat()) { scheduler.setPeriod(jobInfo.getRepeat()); } final SchedulerResult schedulerResult = scheduler.scheduleFlow(projectName, flowName, uploaderResult.getProjectId()); if (schedulerResult.hasError()) { log.error("Failure to schedule job: " + schedulerResult.getError()); throw new DeploymentException(schedulerResult.getError()); } log.info("Successfully scheduled flow: " + flowName); } catch (Exception ex) { log.error("No Nos!", ex); throw new DeploymentException(ex.getMessage()); } finally { IOUtils.closeQuietly(zipOutputStream); FileUtils.deleteQuietly(azkabanZip); FileUtils.deleteQuietly(unzippedPack); } }
From source file:at.spardat.xma.xdelta.test.JarDeltaJarPatcherTest.java
/** * Writes a modified version of zip_Source into target. * * @author S3460//from ww w. j a v a2 s .c o m * @param zipSource the zip source * @param target the target * @return the zip file * @throws Exception the exception */ private ZipFile makeTargetZipFile(ZipFile zipSource, File target) throws Exception { ZipArchiveOutputStream out = new ZipArchiveOutputStream(new FileOutputStream(target)); for (Enumeration<ZipArchiveEntry> enumer = zipSource.getEntries(); enumer.hasMoreElements();) { ZipArchiveEntry sourceEntry = enumer.nextElement(); out.putArchiveEntry(new ZipArchiveEntry(sourceEntry.getName())); byte[] oldBytes = toBytes(zipSource, sourceEntry); byte[] newBytes = getRandomBytes(); byte[] mixedBytes = mixBytes(oldBytes, newBytes); out.write(mixedBytes, 0, mixedBytes.length); out.flush(); out.closeArchiveEntry(); } out.putArchiveEntry(new ZipArchiveEntry("zipentry" + entryMaxSize + 1)); byte[] bytes = getRandomBytes(); out.write(bytes, 0, bytes.length); out.flush(); out.closeArchiveEntry(); out.putArchiveEntry(new ZipArchiveEntry("zipentry" + (entryMaxSize + 2))); out.closeArchiveEntry(); out.flush(); out.finish(); out.close(); return new ZipFile(targetFile); }
From source file:com.facebook.buck.util.unarchive.UnzipTest.java
@Test public void testExtractSymlink() throws InterruptedException, IOException { assumeThat(Platform.detect(), Matchers.is(Matchers.not(Platform.WINDOWS))); // Create a simple zip archive using apache's commons-compress to store executable info. try (ZipArchiveOutputStream zip = new ZipArchiveOutputStream(zipFile.toFile())) { ZipArchiveEntry entry = new ZipArchiveEntry("link.txt"); entry.setUnixMode((int) MostFiles.S_IFLNK); String target = "target.txt"; entry.setSize(target.getBytes(Charsets.UTF_8).length); entry.setMethod(ZipEntry.STORED); zip.putArchiveEntry(entry);// w w w .ja v a 2 s .co m zip.write(target.getBytes(Charsets.UTF_8)); zip.closeArchiveEntry(); } Path extractFolder = tmpFolder.newFolder(); ArchiveFormat.ZIP.getUnarchiver().extractArchive(new DefaultProjectFilesystemFactory(), zipFile.toAbsolutePath(), extractFolder.toAbsolutePath(), ExistingFileMode.OVERWRITE); Path link = extractFolder.toAbsolutePath().resolve("link.txt"); assertTrue(Files.isSymbolicLink(link)); assertThat(Files.readSymbolicLink(link).toString(), Matchers.equalTo("target.txt")); }
From source file:com.amazon.aws.samplecode.travellog.util.DataExtractor.java
public void run() { try {/* w ww. jav a 2 s . c om*/ //Create temporary directory File tmpDir = File.createTempFile("travellog", ""); tmpDir.delete(); //Wipe out temporary file to replace with a directory tmpDir.mkdirs(); logger.log(Level.INFO, "Extract temp dir: " + tmpDir); //Store journal to props file Journal journal = dao.getJournal(); Properties journalProps = buildProps(journal); File journalFile = new File(tmpDir, "journal"); journalProps.store(new FileOutputStream(journalFile), ""); //Iterate through entries and grab related photos List<Entry> entries = dao.getEntries(journal); int entryIndex = 1; int imageFileIndex = 1; for (Entry entry : entries) { Properties entryProps = buildProps(entry); File entryFile = new File(tmpDir, "entry." + (entryIndex++)); entryProps.store(new FileOutputStream(entryFile), ""); List<Photo> photos = dao.getPhotos(entry); int photoIndex = 1; for (Photo photo : photos) { Properties photoProps = buildProps(photo); InputStream photoData = S3PhotoUtil.loadOriginalPhoto(photo); String imageFileName = "imgdata." + (imageFileIndex++); File imageFile = new File(tmpDir, imageFileName); FileOutputStream outputStream = new FileOutputStream(imageFile); IOUtils.copy(photoData, outputStream); photoProps.setProperty("file", imageFileName); outputStream.close(); photoData.close(); File photoFile = new File(tmpDir, "photo." + (entryIndex - 1) + "." + (photoIndex++)); photoProps.store(new FileOutputStream(photoFile), ""); } List<Comment> comments = dao.getComments(entry); int commentIndex = 1; for (Comment comment : comments) { Properties commentProps = buildProps(comment); File commentFile = new File(tmpDir, "comment." + (entryIndex - 1) + "." + commentIndex++); commentProps.store(new FileOutputStream(commentFile), ""); } } //Bundle up the folder as a zip final File zipOut; //If we have an output path store locally if (outputPath != null) { zipOut = new File(outputPath); } else { //storing to S3 zipOut = File.createTempFile("export", ".zip"); } zipOut.getParentFile().mkdirs(); //make sure directory structure is in place ZipArchiveOutputStream zaos = new ZipArchiveOutputStream(zipOut); //Create the zip file File[] files = tmpDir.listFiles(); for (File file : files) { ZipArchiveEntry archiveEntry = new ZipArchiveEntry(file.getName()); byte[] fileData = FileUtils.readFileToByteArray(file); archiveEntry.setSize(fileData.length); zaos.putArchiveEntry(archiveEntry); zaos.write(fileData); zaos.flush(); zaos.closeArchiveEntry(); } zaos.close(); //If outputpath if (outputPath == null) { TravelLogStorageObject obj = new TravelLogStorageObject(); obj.setBucketName(bucketName); obj.setStoragePath(storagePath); obj.setData(FileUtils.readFileToByteArray(zipOut)); obj.setMimeType("application/zip"); S3StorageManager mgr = new S3StorageManager(); mgr.store(obj, false, null); //Store with full redundancy and default permissions } } catch (Exception e) { logger.log(Level.SEVERE, e.getMessage(), e); } }
From source file:fr.acxio.tools.agia.tasks.ZipFilesTasklet.java
protected void zipResource(Resource sSourceResource, ZipArchiveOutputStream sZipArchiveOutputStream, StepContribution sContribution, ChunkContext sChunkContext) throws IOException, ZipFilesException { // TODO : use a queue to reduce the callstack overhead if (sSourceResource.exists()) { File aSourceFile = sSourceResource.getFile(); String aSourcePath = aSourceFile.getCanonicalPath(); if (!aSourcePath.startsWith(sourceBaseDirectoryPath)) { throw new ZipFilesException( "Source file " + aSourcePath + " does not match base directory " + sourceBaseDirectoryPath); }/*from w w w.ja v a 2 s . c om*/ if (sContribution != null) { sContribution.incrementReadCount(); } String aZipEntryName = aSourcePath.substring(sourceBaseDirectoryPath.length() + 1); sZipArchiveOutputStream.putArchiveEntry(new ZipArchiveEntry(aZipEntryName)); if (LOGGER.isInfoEnabled()) { LOGGER.info("Zipping {} to {}", sSourceResource.getFile().getCanonicalPath(), aZipEntryName); } if (aSourceFile.isFile()) { InputStream aInputStream = sSourceResource.getInputStream(); IOUtils.copy(aInputStream, sZipArchiveOutputStream); aInputStream.close(); sZipArchiveOutputStream.closeArchiveEntry(); } else { sZipArchiveOutputStream.closeArchiveEntry(); for (File aFile : aSourceFile .listFiles((FileFilter) (recursive ? TrueFileFilter.TRUE : FileFileFilter.FILE))) { zipResource(new FileSystemResource(aFile), sZipArchiveOutputStream, sContribution, sChunkContext); } } if (sContribution != null) { sContribution.incrementWriteCount(1); } } else if (LOGGER.isInfoEnabled()) { LOGGER.info("{} does not exist", sSourceResource.getFilename()); } }
From source file:io.magentys.maven.DonutMojo.java
private void zipDonutReport() throws IOException, ArchiveException { Optional<File> file = FileUtils .listFiles(outputDirectory, new RegexFileFilter("^(.*)donut-report.html$"), TrueFileFilter.INSTANCE) .stream().findFirst();/* www. j a v a 2 s . c o m*/ if (!file.isPresent()) throw new FileNotFoundException( String.format("Cannot find a donut report in folder: %s", outputDirectory.getAbsolutePath())); File zipFile = new File(outputDirectory, FilenameUtils.removeExtension(file.get().getName()) + ".zip"); try (OutputStream os = new FileOutputStream(zipFile); ArchiveOutputStream aos = new ArchiveStreamFactory() .createArchiveOutputStream(ArchiveStreamFactory.ZIP, os); BufferedInputStream is = new BufferedInputStream(new FileInputStream(file.get()))) { aos.putArchiveEntry(new ZipArchiveEntry(file.get().getName())); IOUtils.copy(is, aos); aos.closeArchiveEntry(); aos.finish(); } }
From source file:com.facebook.buck.util.unarchive.Unzip.java
private static void fillIntermediatePaths(Path path, SortedMap<Path, ZipArchiveEntry> pathMap) { for (Path p = path.getParent(); p != null; p = p.getParent()) { if (pathMap.containsKey(p)) { break; }// w ww . ja va 2 s . c o m pathMap.put(p, new ZipArchiveEntry(p + "/")); } }
From source file:es.ucm.fdi.util.archive.ZipFormat.java
public void create(ArrayList<File> sources, File destFile, File baseDir) throws IOException { // to avoid modifying input argument ArrayList<File> toAdd = new ArrayList<>(sources); ZipArchiveOutputStream zos = null;//from www .j a v a2s . com try { zos = new ZipArchiveOutputStream(new FileOutputStream(destFile)); zos.setMethod(ZipArchiveOutputStream.DEFLATED); byte[] b = new byte[1024]; //log.debug("Creating zip file: "+ficheroZip.getName()); for (int i = 0; i < toAdd.size(); i++) { // note: cannot use foreach because sources gets modified File file = toAdd.get(i); // zip standard uses fw slashes instead of backslashes, always String baseName = baseDir.getAbsolutePath() + '/'; String fileName = file.getAbsolutePath().substring(baseName.length()); if (file.isDirectory()) { fileName += '/'; } ZipArchiveEntry entry = new ZipArchiveEntry(fileName); // skip directories - after assuring that their children *will* be included. if (file.isDirectory()) { //log.debug("\tAdding dir "+fileName); for (File child : file.listFiles()) { toAdd.add(child); } zos.putArchiveEntry(entry); continue; } //log.debug("\tAdding file "+fileName); // Add the zip entry and associated data. zos.putArchiveEntry(entry); int n; try (FileInputStream fis = new FileInputStream(file)) { while ((n = fis.read(b)) > -1) { zos.write(b, 0, n); } zos.closeArchiveEntry(); } } } finally { if (zos != null) { zos.finish(); zos.close(); } } }