List of usage examples for org.apache.commons.compress.archivers.zip ZipArchiveEntry getName
public String getName()
From source file:org.ng200.openolympus.controller.task.TaskUploader.java
protected void uploadTaskData(final Task task, final UploadableTask taskDto) throws IOException, IllegalStateException, FileNotFoundException, ArchiveException { final String descriptionPath = MessageFormat.format(TaskUploader.TASK_DESCRIPTION_PATH_TEMPLATE, StorageSpace.STORAGE_PREFIX, task.getTaskLocation()); if (taskDto.getDescriptionFile() != null) { final File descriptionFile = new File(descriptionPath); if (descriptionFile.exists()) { descriptionFile.delete();//from www.j a v a2 s.co m } descriptionFile.getParentFile().mkdirs(); descriptionFile.createNewFile(); taskDto.getDescriptionFile().transferTo(descriptionFile); } if (taskDto.getTaskZip() != null) { final String checkingChainPath = MessageFormat.format(TaskUploader.TASK_CHECKING_CHAIN_PATH_TEMPLATE, StorageSpace.STORAGE_PREFIX, task.getTaskLocation()); final File checkingChainFile = new File(checkingChainPath); if (checkingChainFile.exists()) { FileUtils.deleteDirectory(checkingChainFile); } checkingChainFile.mkdirs(); try (ArchiveInputStream in = new ArchiveStreamFactory().createArchiveInputStream("zip", taskDto.getTaskZip().getInputStream())) { ZipArchiveEntry entry; while ((entry = (ZipArchiveEntry) in.getNextEntry()) != null) { final File file = new File(checkingChainFile, entry.getName()); if (entry.isDirectory()) { file.mkdirs(); } else { try (OutputStream out = new FileOutputStream(file)) { IOUtils.copy(in, out); } } } } } }
From source file:org.ngrinder.common.util.CompressionUtil.java
/** * Unzip the given input stream into destination directory with the given character set. * // w ww . j av a 2 s .c o m * @param is * input stream * @param destDir * destination directory * @param charsetName * character set name */ public static void unzip(InputStream is, File destDir, String charsetName) { ZipArchiveInputStream zis = null; try { ZipArchiveEntry entry; String name; File target; int nWritten = 0; BufferedOutputStream bos; byte[] buf = new byte[1024 * 8]; zis = new ZipArchiveInputStream(is, charsetName, false); while ((entry = zis.getNextZipEntry()) != null) { name = entry.getName(); target = new File(destDir, name); if (entry.isDirectory()) { target.mkdirs(); /* does it always work? */ } else { target.createNewFile(); bos = new BufferedOutputStream(new FileOutputStream(target)); while ((nWritten = zis.read(buf)) >= 0) { bos.write(buf, 0, nWritten); } bos.close(); } } } catch (Exception e) { throw new NGrinderRuntimeException(e.getMessage(), e); } finally { IOUtils.closeQuietly(zis); } }
From source file:org.ngrinder.script.util.CompressionUtil.java
public void unzip(InputStream is, File destDir, String charsetName) throws IOException { ZipArchiveInputStream zis;/* w w w . j av a 2s . com*/ ZipArchiveEntry entry; String name; File target; int nWritten = 0; BufferedOutputStream bos; byte[] buf = new byte[1024 * 8]; zis = new ZipArchiveInputStream(is, charsetName, false); while ((entry = zis.getNextZipEntry()) != null) { name = entry.getName(); target = new File(destDir, name); if (entry.isDirectory()) { target.mkdirs(); /* does it always work? */ } else { target.createNewFile(); bos = new BufferedOutputStream(new FileOutputStream(target)); while ((nWritten = zis.read(buf)) >= 0) { bos.write(buf, 0, nWritten); } bos.close(); } } zis.close(); }
From source file:org.opencastproject.ingest.impl.IngestServiceImpl.java
/** * {@inheritDoc}//from w ww.j a va 2 s. co m * * @see org.opencastproject.ingest.api.IngestService#addZippedMediaPackage(java.io.InputStream, java.lang.String, * java.util.Map, java.lang.Long) */ @Override public WorkflowInstance addZippedMediaPackage(InputStream zipStream, String workflowDefinitionId, Map<String, String> workflowConfig, Long workflowInstanceId) throws MediaPackageException, IOException, IngestException, NotFoundException, UnauthorizedException { // Start a job synchronously. We can't keep the open input stream waiting around. Job job = null; // Get hold of the workflow instance if specified WorkflowInstance workflowInstance = null; if (workflowInstanceId != null) { logger.info("Ingesting zipped mediapackage for workflow {}", workflowInstanceId); try { workflowInstance = workflowService.getWorkflowById(workflowInstanceId); } catch (NotFoundException e) { logger.debug("Ingest target workflow not found, starting a new one"); } catch (WorkflowDatabaseException e) { throw new IngestException(e); } } else { logger.info("Ingesting zipped mediapackage"); } ZipArchiveInputStream zis = null; Set<String> collectionFilenames = new HashSet<String>(); try { // We don't need anybody to do the dispatching for us. Therefore we need to make sure that the job is never in // QUEUED state but set it to INSTANTIATED in the beginning and then manually switch it to RUNNING. job = serviceRegistry.createJob(JOB_TYPE, INGEST_STREAM, null, null, false); job.setStatus(Status.RUNNING); serviceRegistry.updateJob(job); // Create the working file target collection for this ingest operation String wfrCollectionId = Long.toString(job.getId()); zis = new ZipArchiveInputStream(zipStream); ZipArchiveEntry entry; MediaPackage mp = null; Map<String, URI> uris = new HashMap<String, URI>(); // Sequential number to append to file names so that, if two files have the same // name, one does not overwrite the other int seq = 1; // While there are entries write them to a collection while ((entry = zis.getNextZipEntry()) != null) { try { if (entry.isDirectory() || entry.getName().contains("__MACOSX")) continue; if (entry.getName().endsWith("manifest.xml") || entry.getName().endsWith("index.xml")) { // Build the mediapackage mp = loadMediaPackageFromManifest(new ZipEntryInputStream(zis, entry.getSize())); } else { logger.info("Storing zip entry {} in working file repository collection '{}'", job.getId() + entry.getName(), wfrCollectionId); // Since the directory structure is not being mirrored, makes sure the file // name is different than the previous one(s) by adding a sequential number String fileName = FilenameUtils.getBaseName(entry.getName()) + "_" + seq++ + "." + FilenameUtils.getExtension(entry.getName()); URI contentUri = workingFileRepository.putInCollection(wfrCollectionId, fileName, new ZipEntryInputStream(zis, entry.getSize())); collectionFilenames.add(fileName); // Each entry name starts with zip file name; discard it so that the map key will match the media package // element uri String key = entry.getName().substring(entry.getName().indexOf('/') + 1); uris.put(key, contentUri); ingestStatistics.add(entry.getSize()); logger.info("Zip entry {} stored at {}", job.getId() + entry.getName(), contentUri); } } catch (IOException e) { logger.warn("Unable to process zip entry {}: {}", entry.getName(), e.getMessage()); throw e; } } if (mp == null) throw new MediaPackageException("No manifest found in this zip"); // Determine the mediapackage identifier String mediaPackageId = null; if (workflowInstance != null) { mediaPackageId = workflowInstance.getMediaPackage().getIdentifier().toString(); mp.setIdentifier(workflowInstance.getMediaPackage().getIdentifier()); } else { if (mp.getIdentifier() == null || StringUtils.isBlank(mp.getIdentifier().toString())) mp.setIdentifier(new UUIDIdBuilderImpl().createNew()); mediaPackageId = mp.getIdentifier().toString(); } logger.info("Ingesting mediapackage {} is named '{}'", mediaPackageId, mp.getTitle()); // Make sure there are tracks in the mediapackage if (mp.getTracks().length == 0) { logger.warn("Mediapackage {} has no media tracks", mediaPackageId); } // Update the element uris to point to their working file repository location for (MediaPackageElement element : mp.elements()) { URI uri = uris.get(element.getURI().toString()); if (uri == null) throw new MediaPackageException( "Unable to map element name '" + element.getURI() + "' to workspace uri"); logger.info("Ingested mediapackage element {}/{} is located at {}", new Object[] { mediaPackageId, element.getIdentifier(), uri }); URI dest = workingFileRepository.moveTo(wfrCollectionId, uri.toString(), mediaPackageId, element.getIdentifier(), FilenameUtils.getName(element.getURI().toString())); element.setURI(dest); // TODO: This should be triggered somehow instead of being handled here if (MediaPackageElements.SERIES.equals(element.getFlavor())) { logger.info("Ingested mediapackage {} contains updated series information", mediaPackageId); updateSeries(element.getURI()); } } // Now that all elements are in place, start with ingest logger.info("Initiating processing of ingested mediapackage {}", mediaPackageId); workflowInstance = ingest(mp, workflowDefinitionId, workflowConfig, workflowInstanceId); logger.info("Ingest of mediapackage {} done", mediaPackageId); job.setStatus(Job.Status.FINISHED); return workflowInstance; } catch (ServiceRegistryException e) { throw new IngestException(e); } catch (MediaPackageException e) { job.setStatus(Job.Status.FAILED, Job.FailureReason.DATA); if (workflowInstance != null) { workflowInstance.getCurrentOperation().setState(OperationState.FAILED); workflowInstance.setState(WorkflowState.FAILED); try { logger.info("Marking related workflow {} as failed", workflowInstance); workflowService.update(workflowInstance); } catch (WorkflowException e1) { logger.error("Error updating workflow instance {} with ingest failure: {}", workflowInstance, e1.getMessage()); } } throw e; } catch (Exception e) { job.setStatus(Job.Status.FAILED); if (workflowInstance != null) { workflowInstance.getCurrentOperation().setState(OperationState.FAILED); workflowInstance.setState(WorkflowState.FAILED); try { logger.info("Marking related workflow {} as failed", workflowInstance); workflowService.update(workflowInstance); } catch (WorkflowException e1) { logger.error("Error updating workflow instance {} with ingest failure: {}", workflowInstance, e1.getMessage()); } } if (e instanceof IngestException) throw (IngestException) e; throw new IngestException(e); } finally { IOUtils.closeQuietly(zis); for (String filename : collectionFilenames) { workingFileRepository.deleteFromCollection(Long.toString(job.getId()), filename); } try { serviceRegistry.updateJob(job); } catch (Exception e) { throw new IngestException("Unable to update job", e); } } }
From source file:org.opengion.fukurou.util.ZipArchive.java
/** * ???ZIP??????/*w w w. java 2s.c om*/ * ?(targetPath)???ZIP(zipFile)???? * ????????????????? * <p> * ??????????????? * * @param targetPath ?? * @param zipFile ??ZIP * @param encording ?(Windows???"Windows-31J" ???) * @return ???ZIP? * @og.rev 4.1.0.2 (2008/02/01) ? * @og.rev 4.3.1.1 (2008/08/23) mkdirs ? * @og.rev 4.3.3.3 (2008/10/22) mkdirs???? * @og.rev 5.1.9.0 (2010/08/01) ? * @og.rev 5.7.1.2 (2013/12/20) org.apache.commons.compress ?(??) */ public static List<File> unCompress(final File targetPath, final File zipFile, final String encording) { List<File> list = new ArrayList<File>(); // ???'/'??'\'???? // String tmpPrefix = targetPath; // if( File.separatorChar != targetPath.charAt( targetPath.length() - 1 ) ) { // tmpPrefix = tmpPrefix + File.separator; // } ZipArchiveInputStream zis = null; File tmpFile = null; // String fileName = null; try { zis = new ZipArchiveInputStream(new BufferedInputStream(new FileInputStream(zipFile)), encording); ZipArchiveEntry entry = null; while ((entry = zis.getNextZipEntry()) != null) { // fileName = tmpPrefix + entry.getName().replace( '/', File.separatorChar ); tmpFile = new File(targetPath, entry.getName()); list.add(tmpFile); // ?????? if (entry.isDirectory()) { if (!tmpFile.exists() && !tmpFile.mkdirs()) { String errMsg = "??????[??=" + tmpFile + "]"; System.err.println(errMsg); continue; } } // ???????? else { // 4.3.3.3 (2008/10/22) ????? if (!tmpFile.getParentFile().exists() && !tmpFile.getParentFile().mkdirs()) { String errMsg = "??????[??=" + tmpFile + "]"; System.err.println(errMsg); continue; } BufferedOutputStream out = new BufferedOutputStream(new FileOutputStream(tmpFile)); try { IOUtils.copy(zis, out); } catch (IOException zex) { String errMsg = "ZIP??(copy)?????[??=" + tmpFile + "]"; System.err.println(errMsg); continue; } finally { Closer.ioClose(out); } } // 5.1.9.0 (2010/08/01) ? long lastTime = entry.getTime(); if (lastTime >= 0 && !tmpFile.setLastModified(lastTime)) { String errMsg = "ZIP??????[??=" + tmpFile + "]"; System.err.println(errMsg); } } } catch (FileNotFoundException ex) { String errMsg = "????????[??=" + tmpFile + "]"; throw new RuntimeException(errMsg, ex); } catch (IOException ex) { String errMsg = "ZIP???????[??=" + tmpFile + "]"; throw new RuntimeException(errMsg, ex); } finally { Closer.ioClose(zis); } return list; }
From source file:org.openjump.core.ui.plugin.file.open.OpenFileWizardState.java
public void setupFileLoaders(File[] files, FileLayerLoader fileLayerLoader) { Set<File> fileSet = new TreeSet<File>(Arrays.asList(files)); multiLoaderFiles.clear();/* www. ja va 2 s . c o m*/ // explicit loader chosen if (fileLayerLoader != null) { fileLoaderMap.clear(); for (File file : fileSet) { setFileLoader(file.toURI(), fileLayerLoader); } } else { // Remove old entries in fileloadermap fileLoaderMap.clear(); // for (Iterator<Entry<URI, FileLayerLoader>> iterator = fileLoaderMap.entrySet() // .iterator(); iterator.hasNext();) { // Entry<URI, FileLayerLoader> entry = iterator.next(); // URI fileUri = entry.getKey(); // File file; // if (fileUri.getScheme().equals("zip")) { // file = UriUtil.getZipFile(fileUri); // } else { // file = new File(fileUri); // } // // if (!fileSet.contains(file)) { // FileLayerLoader loader = entry.getValue(); // fileLoaderFiles.get(loader); // Set<URI> loaderFiles = fileLoaderFiles.get(loader); // if (loaderFiles != null) { // loaderFiles.remove(fileUri); // } // iterator.remove(); // } // } // manually add compressed files here for (File file : files) { // zip files if (CompressedFile.isZip(file.getName())) { try { ZipFile zipFile = new ZipFile(file); URI fileUri = file.toURI(); Enumeration entries = zipFile.getEntries(); while (entries.hasMoreElements()) { ZipArchiveEntry entry = (ZipArchiveEntry) entries.nextElement(); if (!entry.isDirectory()) { URI entryUri = UriUtil.createZipUri(file, entry.getName()); String entryExt = UriUtil.getFileExtension(entryUri); //System.out.println(entryUri+"<->"+entryExt); addFile(entryExt, entryUri); } } } catch (Exception e) { errorHandler.handleThrowable(e); } } // tar[.gz,.bz...] (un)compressed archive files else if (CompressedFile.isTar(file.getName())) { try { InputStream is = CompressedFile.openFile(file.getAbsolutePath(), null); TarArchiveEntry entry; TarArchiveInputStream tis = new TarArchiveInputStream(is); while ((entry = tis.getNextTarEntry()) != null) { if (!entry.isDirectory()) { URI entryUri = UriUtil.createZipUri(file, entry.getName()); String entryExt = UriUtil.getFileExtension(entryUri); addFile(entryExt, entryUri); } } tis.close(); } catch (Exception e) { errorHandler.handleThrowable(e); } } // 7zip compressed files else if (CompressedFile.isSevenZ(file.getName())) { try { //System.out.println(file.getName()); SevenZFile sevenZFile = new SevenZFile(file); SevenZArchiveEntry entry; while ((entry = sevenZFile.getNextEntry()) != null) { if (!entry.isDirectory()) { URI entryUri = UriUtil.createZipUri(file, entry.getName()); String entryExt = UriUtil.getFileExtension(entryUri); addFile(entryExt, entryUri); } } sevenZFile.close(); } catch (IOException e) { errorHandler.handleThrowable(e); } } // compressed files else if (CompressedFile.hasCompressedFileExtension(file.getName())) { String[] parts = file.getName().split("\\."); if (parts.length > 2) addFile(parts[parts.length - 2], file.toURI()); } // anything else is a plain data file else { URI fileUri = file.toURI(); addFile(FileUtil.getExtension(file), fileUri); } } } }
From source file:org.opensextant.xtext.collectors.ArchiveNavigator.java
public File unzip(File zipFile) throws IOException, ConfigException { // String _working = FilenameUtils.concat(getWorkingDir(), // FilenameUtils.getBaseName(zipFile.getPath())); // if (_working == null){ // throw new IOException("Invalid archive path for "+zipFile.getPath()); // }//from www . j a va 2 s.c o m // File workingDir = new File(_working); // workingDir.mkdir(); File workingDir = saveDir; InputStream input = new BufferedInputStream(new FileInputStream(zipFile)); ZipArchiveInputStream in = null; try { in = (ZipArchiveInputStream) (new ArchiveStreamFactory().createArchiveInputStream("zip", input)); ZipArchiveEntry zipEntry; while ((zipEntry = (ZipArchiveEntry) in.getNextEntry()) != null) { if (filterEntry(zipEntry)) { continue; } try { File tmpFile = saveArchiveEntry(zipEntry, in, workingDir); converter.convert(tmpFile); } catch (IOException err) { log.error("Unable to save item, FILE=" + zipEntry.getName() + "!" + zipEntry.getName(), err); } } return workingDir; } catch (ArchiveException ae) { throw new IOException(ae); } finally { in.close(); } }
From source file:org.opentestsystem.authoring.testitembank.service.impl.ApipZipInputFileExtractorService.java
private void copyZipEntry(final ZipArchiveEntry entryToCopy, final ZipFile originalZip, final ZipOutputStream zipOut) throws Exception { if (entryToCopy == null) { throw new Exception("no entry found"); }/* w w w .ja v a 2 s . com*/ final InputStream in = originalZip.getInputStream(entryToCopy); final ZipEntry entry = new ZipEntry(entryToCopy.getName()); zipOut.putNextEntry(entry); IOUtils.copy(in, zipOut); zipOut.closeEntry(); in.close(); }
From source file:org.opentestsystem.authoring.testitembank.service.impl.ApipZipInputFileExtractorService.java
private HashMap<String, ZipArchiveEntry> getMappedZipFileEntries(final ZipFile zippedFile) { final HashMap<String, ZipArchiveEntry> mappedEntries = new HashMap<String, ZipArchiveEntry>(); final Enumeration<ZipArchiveEntry> entries = zippedFile.getEntries(); while (entries.hasMoreElements()) { final ZipArchiveEntry e = entries.nextElement(); mappedEntries.put(e.getName(), e); }/*from w w w . j a v a2s. c o m*/ return mappedEntries; }
From source file:org.overlord.commons.dev.server.util.ArchiveUtils.java
/** * Unpacks the given archive file into the output directory. * @param archiveFile an archive file//from w w w . j ava 2 s .c o m * @param toDir where to unpack the archive to * @throws IOException */ public static void unpackToWorkDir(File archiveFile, File toDir) throws IOException { ZipFile zipFile = null; try { zipFile = new ZipFile(archiveFile); Enumeration<ZipArchiveEntry> zipEntries = zipFile.getEntries(); while (zipEntries.hasMoreElements()) { ZipArchiveEntry entry = zipEntries.nextElement(); String entryName = entry.getName(); File outFile = new File(toDir, entryName); if (!outFile.getParentFile().exists()) { if (!outFile.getParentFile().mkdirs()) { throw new IOException( "Failed to create parent directory: " + outFile.getParentFile().getCanonicalPath()); //$NON-NLS-1$ } } if (entry.isDirectory()) { if (!outFile.exists() && !outFile.mkdir()) { throw new IOException("Failed to create directory: " + outFile.getCanonicalPath()); //$NON-NLS-1$ } else if (outFile.exists() && !outFile.isDirectory()) { throw new IOException("Failed to create directory (already exists but is a file): " //$NON-NLS-1$ + outFile.getCanonicalPath()); } } else { InputStream zipStream = null; OutputStream outFileStream = null; zipStream = zipFile.getInputStream(entry); outFileStream = new FileOutputStream(outFile); try { IOUtils.copy(zipStream, outFileStream); } finally { IOUtils.closeQuietly(zipStream); IOUtils.closeQuietly(outFileStream); } } } } finally { ZipFile.closeQuietly(zipFile); } }