List of usage examples for org.apache.commons.compress.archivers.zip ZipFile getInputStream
public InputStream getInputStream(ZipArchiveEntry ze) throws IOException, ZipException
From source file:org.itstechupnorth.walrus.text.GutenburgParser.java
private Reader open() throws IOException, ZipException, FileNotFoundException, UnsupportedEncodingException { final Reader reader; final InputStream input; if (FilenameUtils.isExtension(file.getName(), ZIP)) { try {//from ww w . j a va 2s . c o m final ZipFile zip = new ZipFile(file); input = zip.getInputStream((ZipArchiveEntry) zip.getEntries().nextElement()); } catch (IllegalArgumentException e) { throw new RuntimeWarningException(e.getMessage() + "(" + file + ")", e); } } else { input = new FileInputStream(file); } reader = new InputStreamReader(new BufferedInputStream(input), "UTF-8"); return reader; }
From source file:org.jboss.as.forge.util.Files.java
public static boolean extractAppServer(final String zipPath, final File target, final boolean overwrite) throws IOException { if (target.exists() && !overwrite) { throw new IllegalStateException(Messages.INSTANCE.getMessage("files.not.empty.directory")); }/*www .ja va2 s .c o m*/ // Create a temporary directory final File tmpDir = new File(getTempDirectory(), "jboss-as-" + zipPath.hashCode()); if (tmpDir.exists()) { deleteRecursively(tmpDir); } try { final byte buff[] = new byte[1024]; ZipFile file = null; try { file = new ZipFile(zipPath); final Enumeration<ZipArchiveEntry> entries = file.getEntries(); while (entries.hasMoreElements()) { final ZipArchiveEntry entry = entries.nextElement(); // Create the extraction target final File extractTarget = new File(tmpDir, entry.getName()); if (entry.isDirectory()) { extractTarget.mkdirs(); } else { final File parent = new File(extractTarget.getParent()); parent.mkdirs(); final BufferedInputStream in = new BufferedInputStream(file.getInputStream(entry)); try { final BufferedOutputStream out = new BufferedOutputStream( new FileOutputStream(extractTarget)); try { int read; while ((read = in.read(buff)) != -1) { out.write(buff, 0, read); } } finally { Streams.safeClose(out); } } finally { Streams.safeClose(in); } // Set the file permissions if (entry.getUnixMode() > 0) { setPermissions(extractTarget, FilePermissions.of(entry.getUnixMode())); } } } } catch (IOException e) { throw new IOException(Messages.INSTANCE.getMessage("files.extraction.error", file), e); } finally { ZipFile.closeQuietly(file); // Streams.safeClose(file); } // If the target exists, remove then rename if (target.exists()) { deleteRecursively(target); } // First child should be a directory and there should only be one child final File[] children = tmpDir.listFiles(); if (children != null && children.length == 1) { return moveDirectory(children[0], target); } return moveDirectory(tmpDir, target); } finally { deleteRecursively(tmpDir); } }
From source file:org.jboss.datavirt.commons.dev.server.util.ArchiveUtils.java
/** * Unpacks the given archive file into the output directory. * @param archiveFile an archive file// w w w. ja v a 2 s. c om * @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()); } } if (entry.isDirectory()) { if (!outFile.mkdir()) { throw new IOException("Failed to create directory: " + 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); } }
From source file:org.larz.dom4.editor.AbstractDetailsPage.java
protected Image getSpriteFromZip(final String sprite, String zipName) { try {/*from w w w . ja v a 2 s . c o m*/ Path path = new Path("$nl$/lib/" + zipName + ".zip"); URL url = FileLocator.find(Activator.getDefault().getBundle(), path, null); String dbPath = FileLocator.toFileURL(url).getPath(); ZipFile zipFile = new ZipFile(new File(dbPath)); InputStream imageStream = zipFile.getInputStream(zipFile.getEntry(sprite)); Image image = null; if (spriteMap.get(sprite) != null) { image = spriteMap.get(sprite); } else { if (imageStream != null) { image = new Image(null, new ImageData(imageStream)); spriteMap.put(sprite, image); } } return image; } catch (Exception e) { e.printStackTrace(); } return null; }
From source file:org.larz.dom4.editor.MonsterDetailsPage.java
private Image getSpriteFromZip(final String sprite) { ImageLoader loader1 = new ImageLoader() { @Override/*from www. ja v a 2s . c om*/ public InputStream getStream() throws IOException { Path path = new Path("$nl$/lib/sprites.zip"); URL url = FileLocator.find(Activator.getDefault().getBundle(), path, null); String dbPath = FileLocator.toFileURL(url).getPath(); ZipFile zipFile = new ZipFile(new File(dbPath)); return zipFile.getInputStream(zipFile.getEntry(sprite)); } }; Image image = null; try { if (spriteMap.get(sprite) != null) { image = spriteMap.get(sprite); } else { image = new Image(null, ImageConverter.convertToSWT(ImageConverter.cropImage(loader1.loadImage()))); spriteMap.put(sprite, image); } } catch (Exception e) { e.printStackTrace(); } return image; }
From source file:org.moe.cli.utils.ArchiveUtils.java
public static void unzipArchive(ZipFile zipFile, File destination) throws IOException { Enumeration<ZipArchiveEntry> e = zipFile.getEntries(); InputStream is = null;/*from www .j a v a 2 s. c o m*/ FileOutputStream fStream = null; try { while (e.hasMoreElements()) { ZipArchiveEntry entry = e.nextElement(); if (entry.isDirectory()) { String dest = entry.getName(); File destFolder = new File(destination, dest); if (!destFolder.exists()) { destFolder.mkdirs(); } } else { if (!entry.isUnixSymlink()) { String dest = entry.getName(); File destFile = new File(destination, dest); is = zipFile.getInputStream(entry); // get the input stream fStream = new FileOutputStream(destFile); copyFiles(is, fStream); } else { String link = zipFile.getUnixSymlink(entry); String entryName = entry.getName(); int parentIdx = entryName.lastIndexOf("/"); String newLink = entryName.substring(0, parentIdx) + "/" + link; File destFile = new File(destination, newLink); File linkFile = new File(destination, entryName); Files.createSymbolicLink(Paths.get(linkFile.getPath()), Paths.get(destFile.getPath())); } } } } finally { if (is != null) { is.close(); } if (fStream != null) { fStream.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"); }/*from w ww.j a v a2s .c o m*/ 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 Map<String, ApipItemMetadata> getApipItemMetadata(final ZipFile zip, final HashMap<String, ZipArchiveEntry> mappedEntries, final ApipManifest manifest) { final Map<String, ApipItemMetadata> itemMetadataMap = new HashMap<String, ApipItemMetadata>(); final String apipRoot = findApipRootDirectoy(mappedEntries); final Map<String, ApipManifestResource> resourceMap = manifest.getResourceMap(); for (final ApipManifestResource resource : manifest.getResources()) { if (isItemResource(resource.getResourceType())) { LOGGER.warn("import metadata for." + resource.getIdentifier()); for (final ApipManifestDependency itemDependency : resource.getDependencies()) { final ApipManifestResource metadataResource = resourceMap.get(itemDependency.getIdentifier()); if (metadataResource != null && ResourceTypePrefix.apipMetadata.isPrefixFor(metadataResource.getResourceType())) { try { final ZipArchiveEntry manifestZipEntry = mappedEntries .get(apipRoot + metadataResource.getFileReference().getHref()); if (manifestZipEntry == null) { itemMetadataMap.put(resource.getIdentifier(), null); } else { final InputStream inputstream = zip.getInputStream(manifestZipEntry); final ApipItemMetadata metadata = zipXMLService.extractMetadataFromXML(inputstream); itemMetadataMap.put(resource.getIdentifier(), metadata); }/*from www . ja v a 2s. co m*/ } catch (final Exception e) { // unable to get metadata insert null in map itemMetadataMap.put(resource.getIdentifier(), null); } } } } } return itemMetadataMap; }
From source file:org.opentestsystem.authoring.testitembank.service.impl.ApipZipInputFileExtractorService.java
private ApipManifest extractManifest(final ZipFile zip, final HashMap<String, ZipArchiveEntry> mappedEntries) { ApipManifest returnValue = null;/*ww w . ja v a 2 s . c om*/ try { final String apipRoot = findApipRootDirectoy(mappedEntries); final ZipArchiveEntry manifestZipEntry = mappedEntries.get(apipRoot + IMS_MANIFEST_NAME); final InputStream is = zip.getInputStream(manifestZipEntry); returnValue = zipXMLService.extractManifestFromXML(is); } catch (final Exception e) { throw new TestItemBankException("apip.zip.extractor.failure.mainfest", e); } return returnValue; }
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/* w ww. j a v a2 s . co 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); } }