List of usage examples for java.util.zip ZipFile getName
public String getName()
From source file:org.ambraproject.admin.action.AdminTopActionTest.java
@Test(dataProviderClass = SampleArticleData.class, dataProvider = "sampleArticle") public void testIngestDuplicateArticle(ZipFile archive, Article article) throws Exception { String zipFileName = new File(archive.getName()).getName(); Article articleToStore = new Article(); articleToStore.setDoi(article.getDoi()); dummyDataStore.store(articleToStore); File destinationFile = new File(ingestedDir, zipFileName); File crossrefFile = new File(ingestedDir, crossrefFileName(article.getDoi())); try {//from w w w . j ava 2 s.c om dummyDataStore.store(article); action.setFilesToIngest(new String[] { zipFileName }); action.ingest(); assertNotNull(action.getActionErrors(), "nul list of errors"); assertTrue(action.getActionErrors().size() > 0, "didn't add an error for ingest"); assertFalse(destinationFile.exists(), "Zip file got moved to ingested dir"); assertFalse(crossrefFile.exists(), "Crossref file got created in ingest dir"); } finally { if (destinationFile.exists()) { try { FileUtils.moveFile(destinationFile, new File(ingestDir, zipFileName)); } catch (IOException e) { //ignore } } documentManagementService.delete(article.getDoi(), DEFAULT_ADMIN_AUTHID); } }
From source file:io.apiman.common.plugin.PluginClassLoader.java
/** * @see java.lang.ClassLoader#findResource(java.lang.String) *//*from w w w .j a va 2 s .co m*/ @Override protected URL findResource(String name) { Enumeration<? extends ZipEntry> entries = this.pluginArtifactZip.entries(); while (entries.hasMoreElements()) { ZipEntry zipEntry = entries.nextElement(); if (zipEntry.getName().equalsIgnoreCase(name) || zipEntry.getName().equalsIgnoreCase("WEB-INF/classes/" + name)) { try { return extractResource(zipEntry); } catch (IOException e) { throw new RuntimeException(e); } } } ZipEntry entry; File file; for (ZipFile zipFile : this.dependencyZips) { entry = zipFile.getEntry(name); if (entry != null) { try { file = new File(zipFile.getName()); URL zipUrl = file.toURI().toURL(); URL entryUrl = new URL("jar:" + zipUrl + "!/" + name); return entryUrl; } catch (MalformedURLException e) { e.printStackTrace(); } } } return super.findResource(name); }
From source file:io.apiman.common.plugin.PluginClassLoader.java
/** * @see java.lang.ClassLoader#findResources(java.lang.String) *///from w ww .j ava 2s.c o m @Override protected Enumeration<URL> findResources(String name) throws IOException { List<URL> resources = new ArrayList<>(); // Search for the artifact in the plugin WAR itself Enumeration<? extends ZipEntry> entries = this.pluginArtifactZip.entries(); while (entries.hasMoreElements()) { ZipEntry zipEntry = entries.nextElement(); if (zipEntry.getName().equalsIgnoreCase(name)) { try { resources.add(extractResource(zipEntry)); } catch (IOException e) { throw new RuntimeException(e); } } } // Now also add any resources found in dependencies ZipEntry entry; File file; for (ZipFile zipFile : this.dependencyZips) { entry = zipFile.getEntry(name); if (entry != null) { try { file = new File(zipFile.getName()); URL zipUrl = file.toURI().toURL(); URL entryUrl = new URL("jar:" + zipUrl + "!/" + name); resources.add(entryUrl); } catch (MalformedURLException e) { e.printStackTrace(); } } } // Return the discovered resources as an Enumeration final Iterator<URL> iterator = resources.iterator(); return new Enumeration<URL>() { @Override public boolean hasMoreElements() { return iterator.hasNext(); } @Override public URL nextElement() { return iterator.next(); } }; }
From source file:org.ambraproject.admin.action.AdminTopActionTest.java
@Test(dataProviderClass = SampleArticleData.class, dataProvider = "sampleArticle") public void testIngest(ZipFile archive, Article article) throws Exception { String zipFileName = new File(archive.getName()).getName(); File crossref_file = new File(ingestedDir, crossrefFileName(article.getDoi())); File articleDir = new File(filestoreDir, article.getDoi().replaceAll("info:doi/10.1371/journal.", "10.1371/")); //delete the article in case it's still in the database from the ingester test try {/* w w w . j av a 2 s .c o m*/ documentManagementService.delete(article.getDoi(), DEFAULT_ADMIN_AUTHID); } catch (NoSuchArticleIdException e) { //ignore } try { action.setFilesToIngest(new String[] { zipFileName }); String result = action.ingest(); assertEquals(result, Action.SUCCESS, "Action didn't return success"); assertTrue(new File(ingestedDir, zipFileName).exists(), "Zip file didn't get moved to ingested directory"); assertFalse(new File(ingestDir, zipFileName).exists(), "Zip file didn't get moved to ingested directory"); assertTrue(crossref_file.exists(), "crossref file didn't get created"); assertTrue(articleDir.exists(), "Article didn't get written to the file store"); try { articleService.getArticle(article.getDoi(), DEFAULT_ADMIN_AUTHID); } catch (NoSuchArticleIdException e) { fail("Article didn't get written to the database"); } //cleanup } finally { FileUtils.deleteQuietly(crossref_file); try { //move the archive back FileUtils.moveFile(new File(ingestedDir, zipFileName), new File(ingestDir, zipFileName)); } catch (IOException e) { //suppress } try { //delete the files that got written to the filestore FileUtils.deleteDirectory(articleDir); } catch (IOException e) { //suppress } //delete the article documentManagementService.delete(article.getDoi(), DEFAULT_ADMIN_AUTHID); } }
From source file:org.apache.maven.archetype.common.DefaultArchetypeArtifactManager.java
private ZipEntry searchEntry(ZipFile zipFile, String searchString) { getLogger().debug("Searching for " + searchString + " inside " + zipFile.getName()); Enumeration<? extends ZipEntry> enu = zipFile.entries(); while (enu.hasMoreElements()) { ZipEntry entryfound = (ZipEntry) enu.nextElement(); getLogger().debug(" - " + entryfound.getName()); if (searchString.equals(entryfound.getName())) { getLogger().debug("Entry found"); return entryfound; }/*from ww w. j a v a 2 s. c o m*/ } return null; }
From source file:sebastian.hohns.imagezoom.converter.JImagePyramideProcessor.java
/** * Transforms a list of images and store them at targetPath. * @param imageZip ZipFile with images/* ww w . j av a 2 s . c o m*/ * @param targetPath target directory * @return true if successful, else false */ public boolean process(ZipFile imageZip, String targetPath) { List<String> images = new ArrayList<String>(imageZip.size()); //Unzip file to tmp dir String tmpPath = System.getProperty("java.io.tmpdir") + File.separator + FilenameUtils.getBaseName(imageZip.getName()); if (!new File(tmpPath).exists()) { new File(tmpPath).mkdirs(); } Enumeration<ZipEntry> pics = (Enumeration<ZipEntry>) imageZip.entries(); ZipEntry entry; try { while (pics.hasMoreElements()) { entry = pics.nextElement(); //Ignore directory and extract all files to tmpdir if (!entry.isDirectory()) { copyInputStream(imageZip.getInputStream(entry), new BufferedOutputStream( new FileOutputStream(FilenameUtils.concat(tmpPath, entry.getName())))); images.add(FilenameUtils.concat(tmpPath, FilenameUtils.getBaseName(entry.getName()))); } } //Call List<String> process boolean result = process(images, targetPath); //Cleanup extracted files cleanup(tmpPath); return result; } catch (IOException ioe) { ioe.printStackTrace(); return false; } }
From source file:org.apache.maven.archetype.common.DefaultArchetypeArtifactManager.java
private void closeZipFile(ZipFile zipFile) { try {//from w ww . j a v a 2s . c om zipFile.close(); } catch (Exception e) { getLogger().error("Failed to close " + zipFile.getName() + " zipFile."); } }
From source file:io.fabric8.vertx.maven.plugin.utils.WebJars.java
public static void extract(final AbstractVertxMojo mojo, File in, File out, boolean stripVersion) throws IOException { ZipFile file = new ZipFile(in); try {//from w w w. ja v a 2 s . c om Enumeration<? extends ZipEntry> entries = file.entries(); while (entries.hasMoreElements()) { ZipEntry entry = entries.nextElement(); if (entry.getName().startsWith(WEBJAR_LOCATION) && !entry.isDirectory()) { // Compute destination. File output = new File(out, entry.getName().substring(WEBJAR_LOCATION.length())); if (stripVersion) { String path = entry.getName().substring(WEBJAR_LOCATION.length()); Matcher matcher = WEBJAR_INTERNAL_PATH_REGEX.matcher(path); if (matcher.matches()) { output = new File(out, matcher.group(1) + "/" + matcher.group(3)); } else { mojo.getLog().warn(path + " does not match the regex - did not strip the version for this" + " file"); } } InputStream stream = null; try { stream = file.getInputStream(entry); output.getParentFile().mkdirs(); org.apache.commons.io.FileUtils.copyInputStreamToFile(stream, output); } catch (IOException e) { mojo.getLog().error("Cannot unpack " + entry.getName() + " from " + file.getName(), e); throw e; } finally { IOUtils.closeQuietly(stream); } } } } finally { IOUtils.closeQuietly(file); } }
From source file:net.amigocraft.mpt.util.MiscUtil.java
public static boolean unzip(ZipFile zip, File dest, List<String> files) throws MPTException { boolean returnValue = true; try {/*from w w w.j av a 2 s. co m*/ List<String> existingDirs = new ArrayList<>(); Enumeration<? extends ZipEntry> en = zip.entries(); entryLoop: while (en.hasMoreElements()) { ZipEntry entry = en.nextElement(); String name = entry.getName().startsWith("./") ? entry.getName().substring(2, entry.getName().length()) : entry.getName(); File file = new File(dest, name); if (entry.isDirectory()) { if (file.exists()) { if (DISALLOW_MERGE) { existingDirs.add(name); if (VERBOSE) Main.log.warning("Refusing to extract directory " + name + ": already exists"); } } } else { files.add(name); for (String dir : DISALLOWED_DIRECTORIES) { if (file.getPath().startsWith(dir)) { if (VERBOSE) Main.log.warning("Refusing to extract " + name + " from " + zip.getName() + ": parent directory \"" + dir + "\" is not allowed"); continue entryLoop; } } if (DISALLOW_MERGE) { for (String dir : existingDirs) { if (file.getPath().substring(2, file.getPath().length()).replace(File.separator, "/") .startsWith(dir)) { continue entryLoop; } } } if (!DISALLOW_OVERWRITE || !file.exists()) { file.getParentFile().mkdirs(); for (String ext : DISALLOWED_EXTENSIONS) { if (file.getName().endsWith(ext)) { if (VERBOSE) Main.log.warning("Refusing to extract " + name + " from " + zip.getName() + ": extension \"" + ext + "\" is not allowed"); returnValue = false; continue entryLoop; } } BufferedInputStream bIs = new BufferedInputStream(zip.getInputStream(entry)); int b; byte[] buffer = new byte[1024]; FileOutputStream fOs = new FileOutputStream(file); BufferedOutputStream bOs = new BufferedOutputStream(fOs, 1024); while ((b = bIs.read(buffer, 0, 1024)) != -1) bOs.write(buffer, 0, b); bOs.flush(); bOs.close(); bIs.close(); } else { if (VERBOSE) Main.log.warning( "Refusing to extract " + name + " from " + zip.getName() + ": already exists"); returnValue = false; } } } } catch (Exception ex) { ex.printStackTrace(); //TODO throw new MPTException(ERROR_COLOR + "Failed to extract archive!"); } return returnValue; }
From source file:org.wisdom.maven.utils.WebJars.java
private static void extract(final AbstractWisdomMojo mojo, File in, File out, boolean stripVersion) throws IOException { ZipFile file = new ZipFile(in); try {//from ww w. j a v a2 s. c om Enumeration<? extends ZipEntry> entries = file.entries(); while (entries.hasMoreElements()) { ZipEntry entry = entries.nextElement(); if (entry.getName().startsWith(WEBJAR_LOCATION) && !entry.isDirectory()) { // Compute destination. File output = new File(out, entry.getName().substring(WEBJAR_LOCATION.length())); if (stripVersion) { String path = entry.getName().substring(WEBJAR_LOCATION.length()); Matcher matcher = WEBJAR_INTERNAL_PATH_REGEX.matcher(path); if (matcher.matches()) { output = new File(out, matcher.group(1) + "/" + matcher.group(3)); } else { mojo.getLog().warn(path + " does not match the regex - did not strip the version for this" + " file"); } } InputStream stream = null; try { stream = file.getInputStream(entry); output.getParentFile().mkdirs(); FileUtils.copyInputStreamToFile(stream, output); } catch (IOException e) { mojo.getLog().error("Cannot unpack " + entry.getName() + " from " + file.getName(), e); throw e; } finally { IOUtils.closeQuietly(stream); } } } } finally { IOUtils.closeQuietly(file); } }