List of usage examples for org.apache.commons.compress.archivers.zip ZipFile getEntry
public ZipArchiveEntry getEntry(String name)
null
if no entry by that name exists. From source file:org.codehaus.plexus.archiver.zip.ZipArchiverTest.java
public void testCreateArchiveWithDetectedModes() throws Exception { String[] executablePaths = { "path/to/executable", "path/to/executable.bat" }; String[] confPaths = { "path/to/etc/file", "path/to/etc/file2" }; String[] logPaths = { "path/to/logs/log.txt" }; int exeMode = 0777; int confMode = 0600; int logMode = 0640; if (Os.isFamily(Os.FAMILY_WINDOWS)) { StackTraceElement e = new Throwable().getStackTrace()[0]; System.out// w w w . j av a 2 s .com .println("Cannot execute test: " + e.getMethodName() + " on " + System.getProperty("os.name")); return; } File tmpDir = null; try { tmpDir = File.createTempFile("zip-with-chmod.", ".dir"); tmpDir.delete(); tmpDir.mkdirs(); for (String executablePath : executablePaths) { writeFile(tmpDir, executablePath, exeMode); } for (String confPath : confPaths) { writeFile(tmpDir, confPath, confMode); } for (String logPath : logPaths) { writeFile(tmpDir, logPath, logMode); } { Map<String, PlexusIoResourceAttributes> attributesByPath = PlexusIoResourceAttributeUtils .getFileAttributesByPath(tmpDir); for (String path : executablePaths) { PlexusIoResourceAttributes attrs = attributesByPath.get(path); if (attrs == null) { attrs = attributesByPath.get(new File(tmpDir, path).getAbsolutePath()); } assertNotNull(attrs); assertEquals("Wrong mode for: " + path + "; expected: " + exeMode, exeMode, attrs.getOctalMode()); } for (String path : confPaths) { PlexusIoResourceAttributes attrs = attributesByPath.get(path); if (attrs == null) { attrs = attributesByPath.get(new File(tmpDir, path).getAbsolutePath()); } assertNotNull(attrs); assertEquals("Wrong mode for: " + path + "; expected: " + confMode, confMode, attrs.getOctalMode()); } for (String path : logPaths) { PlexusIoResourceAttributes attrs = attributesByPath.get(path); if (attrs == null) { attrs = attributesByPath.get(new File(tmpDir, path).getAbsolutePath()); } assertNotNull(attrs); assertEquals("Wrong mode for: " + path + "; expected: " + logMode, logMode, attrs.getOctalMode()); } } File zipFile = getTestFile("target/output/zip-with-modes.zip"); ZipArchiver archiver = getZipArchiver(zipFile); archiver.addDirectory(tmpDir); archiver.createArchive(); assertTrue(zipFile.exists()); File zipFile2 = getTestFile("target/output/zip-with-modes-L2.zip"); archiver = getZipArchiver(); archiver.setDestFile(zipFile2); archiver.addArchivedFileSet(zipFile); archiver.createArchive(); ZipFile zf = new ZipFile(zipFile2); for (String path : executablePaths) { ZipArchiveEntry ze = zf.getEntry(path); int mode = ze.getUnixMode() & UnixStat.PERM_MASK; assertEquals("Wrong mode for: " + path + "; expected: " + exeMode, exeMode, mode); } for (String path : confPaths) { ZipArchiveEntry ze = zf.getEntry(path); int mode = ze.getUnixMode() & UnixStat.PERM_MASK; assertEquals("Wrong mode for: " + path + "; expected: " + confMode, confMode, mode); } for (String path : logPaths) { ZipArchiveEntry ze = zf.getEntry(path); int mode = ze.getUnixMode() & UnixStat.PERM_MASK; assertEquals("Wrong mode for: " + path + "; expected: " + logMode, logMode, mode); } } finally { if (tmpDir != null && tmpDir.exists()) { try { FileUtils.forceDelete(tmpDir); } catch (IOException e) { e.printStackTrace(); } } } }
From source file:org.codehaus.plexus.archiver.zip.ZipArchiverTest.java
public void testForcedFileModes() throws IOException { File step1file = new File("target/output/forced-file-mode.zip"); {//from ww w. j a va 2 s. c om final ZipArchiver zipArchiver = getZipArchiver(step1file); zipArchiver.setFileMode(0077); zipArchiver.setDirectoryMode(0007); PlexusIoResourceAttributes attrs = new SimpleResourceAttributes(123, "fred", 22, "filntstones", 0111); PlexusIoResource resource = ResourceFactory .createResource(new File("src/test/resources/folders/File.txt"), "Test.txt", null, attrs); zipArchiver.addResource(resource, "Test2.txt", 0707); PlexusIoFileResourceCollection files = new PlexusIoFileResourceCollection(); files.setBaseDir(new File("src/test/resources/folders")); files.setPrefix("sixsixsix/"); zipArchiver.addResources(files); zipArchiver.createArchive(); ZipFile zf = new ZipFile(step1file); fileModeAssert(040007, zf.getEntry("sixsixsix/a/").getUnixMode()); fileModeAssert(0100077, zf.getEntry("sixsixsix/b/FileInB.txt").getUnixMode()); fileModeAssert(0100707, zf.getEntry("Test2.txt").getUnixMode()); zf.close(); } File Step2file = new File("target/output/forced-file-mode-from-zip.zip"); { final ZipArchiver za2 = getZipArchiver(Step2file); za2.setFileMode(0666); za2.setDirectoryMode(0676); PlexusIoZipFileResourceCollection zipSrc = new PlexusIoZipFileResourceCollection(); zipSrc.setFile(step1file); zipSrc.setPrefix("zz/"); za2.addResources(zipSrc); za2.createArchive(); ZipFile zf = new ZipFile(Step2file); fileModeAssert(040676, zf.getEntry("zz/sixsixsix/a/").getUnixMode()); fileModeAssert(0100666, zf.getEntry("zz/Test2.txt").getUnixMode()); zf.close(); } File step3file = new File("target/output/forced-file-mode-from-zip2.zip"); { final ZipArchiver za2 = getZipArchiver(step3file); za2.setFileMode(0666); za2.setDirectoryMode(0676); PlexusArchiverZipFileResourceCollection zipSrc = new PlexusArchiverZipFileResourceCollection(); zipSrc.setFile(step1file); zipSrc.setPrefix("zz/"); za2.addResources(zipSrc); za2.createArchive(); ZipFile zf = new ZipFile(Step2file); fileModeAssert(040676, zf.getEntry("zz/sixsixsix/a/").getUnixMode()); fileModeAssert(0100666, zf.getEntry("zz/Test2.txt").getUnixMode()); zf.close(); } }
From source file:org.cytobank.acs.core.ACS.java
/** * Writes out a file contained within this <code>ACS</code> instance to the * specified <code>OutputStream</code>. * //from w w w .ja v a 2 s . co m * @param filePath * the <code>String</code> path representing a file within this * <code>ACS</code> instance. * @param outputStream * to write the requested file to * @throws IOException * If an input or output exception occurred */ public void extractFile(String filePath, OutputStream outputStream) throws IOException { ZipFile zipFile = new ZipFile(acsFile); if (filePath.startsWith("/")) filePath = filePath.substring(1); InputStream inputStream = null; ZipArchiveEntry entry = zipFile.getEntry(filePath); try { if (entry == null) throw new IOException("Could not find zip file entry " + filePath); inputStream = zipFile.getInputStream(entry); IOUtils.copy(inputStream, outputStream); } finally { try { if (inputStream != null) { inputStream.close(); } } catch (IOException ignore) { } try { zipFile.close(); } catch (IOException ignore) { } } }
From source file:org.larz.dom4.editor.AbstractDetailsPage.java
protected Image getSpriteFromZip(final String sprite, String zipName) { try {/*from ww w. ja v a 2 s . c om*/ 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 w w w . j av a2 s . com 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.sead.nds.repository.BagGenerator.java
public void validateBag(String bagId) { log.info("Validating Bag"); ZipFile zf = null; InputStream is = null;//from ww w . j a va2 s . com try { zf = new ZipFile(getBagFile(bagId)); ZipArchiveEntry entry = zf.getEntry(getValidName(bagId) + "/manifest-sha1.txt"); if (entry != null) { log.info("SHA1 hashes used"); hashtype = "SHA1 Hash"; is = zf.getInputStream(entry); BufferedReader br = new BufferedReader(new InputStreamReader(is)); String line = br.readLine(); while (line != null) { log.debug("Hash entry: " + line); int breakIndex = line.indexOf(' '); String hash = line.substring(0, breakIndex); String path = line.substring(breakIndex + 1); log.debug("Adding: " + path + " with hash: " + hash); sha1Map.put(path, hash); line = br.readLine(); } IOUtils.closeQuietly(is); } else { entry = zf.getEntry(getValidName(bagId) + "/manifest-sha512.txt"); if (entry != null) { log.info("SHA512 hashes used"); hashtype = "SHA512 Hash"; is = zf.getInputStream(entry); BufferedReader br = new BufferedReader(new InputStreamReader(is)); String line = br.readLine(); while (line != null) { int breakIndex = line.indexOf(' '); String hash = line.substring(0, breakIndex); String path = line.substring(breakIndex + 1); sha1Map.put(path, hash); line = br.readLine(); } IOUtils.closeQuietly(is); } } log.info("HashMap Map contains: " + sha1Map.size() + " etries"); checkFiles(sha1Map, zf); } catch (IOException io) { log.error("Could not validate Hashes", io); } catch (Exception e) { log.error("Could not validate Hashes", e); } return; }
From source file:org.sead.nds.repository.util.ValidationJob.java
private String generateFileHash(String name, ZipFile zf) { ZipArchiveEntry archiveEntry1 = zf.getEntry(name); // Error check - add file sizes to compare against supplied stats long start = System.currentTimeMillis(); InputStream inputStream = null; String realHash = null;/*from w w w. j ava 2 s . c om*/ try { inputStream = zf.getInputStream(archiveEntry1); String hashtype = bagGenerator.getHashtype(); if (hashtype != null) { if (hashtype.equals("SHA1 Hash")) { realHash = DigestUtils.sha1Hex(inputStream); } else if (hashtype.equals("SHA512 Hash")) { realHash = DigestUtils.sha512Hex(inputStream); } } } catch (ZipException e) { // TODO Auto-generated catch block e.printStackTrace(); } catch (IOException e) { // TODO Auto-generated catch block e.printStackTrace(); } finally { IOUtils.closeQuietly(inputStream); } log.debug("Retrieve/compute time = " + (System.currentTimeMillis() - start) + " ms"); // Error check - add file sizes to compare against supplied stats bagGenerator.incrementTotalDataSize(archiveEntry1.getSize()); return realHash; }
From source file:org.silverpeas.core.util.ZipUtilTest.java
/** * Test of compressPathToZip method, of class ZipManager. * * @throws Exception//from ww w .ja v a 2s. co m */ @Test public void testCompressPathToZip(MavenTestEnv mavenTestEnv) throws Exception { File path = new File(mavenTestEnv.getResourceTestDirFile(), "ZipSample"); File outfile = new File(tempDir, "testCompressPathToZip.zip"); ZipUtil.compressPathToZip(path, outfile); ZipFile zipFile = new ZipFile(outfile, CharEncoding.UTF_8); try { Enumeration<? extends ZipEntry> entries = zipFile.getEntries(); assertThat(zipFile.getEncoding(), is(CharEncoding.UTF_8)); int nbEntries = 0; while (entries.hasMoreElements()) { ZipEntry entry = entries.nextElement(); nbEntries++; } assertThat(nbEntries, is(5)); assertThat(zipFile.getEntry("ZipSample/simple.txt"), is(notNullValue())); assertThat(zipFile.getEntry("ZipSample/level1/simple.txt"), is(notNullValue())); assertThat(zipFile.getEntry("ZipSample/level1/level2b/simple.txt"), is(notNullValue())); assertThat(zipFile.getEntry("ZipSample/level1/level2a/simple.txt"), is(notNullValue())); ZipEntry accentuatedEntry = zipFile.getEntry("ZipSample/level1/level2a/s\u00efmplifi\u00e9.txt"); if (accentuatedEntry == null) { accentuatedEntry = zipFile.getEntry("ZipSample/level1/level2a/" + new String("smplifi.txt".getBytes("UTF-8"), Charset.defaultCharset())); } assertThat(accentuatedEntry, is(notNullValue())); assertThat(zipFile.getEntry("ZipSample/level1/level2c/"), is(nullValue())); } finally { zipFile.close(); } }
From source file:org.silverpeas.core.util.ZipUtilTest.java
/** * Test of compressStreamToZip method, of class ZipManager. * * @throws Exception/*from w w w . ja v a 2s .c o m*/ */ @Test public void testCompressStreamToZip() throws Exception { InputStream inputStream = this.getClass().getClassLoader().getResourceAsStream("FrenchScrum.odp"); String filePathNameToCreate = separatorChar + "dir1" + separatorChar + "dir2" + separatorChar + "FrenchScrum.odp"; File outfile = new File(tempDir, "testCompressStreamToZip.zip"); ZipUtil.compressStreamToZip(inputStream, filePathNameToCreate, outfile.getPath()); inputStream.close(); assertThat(outfile, is(notNullValue())); assertThat(outfile.exists(), is(true)); assertThat(outfile.isFile(), is(true)); int result = ZipUtil.getNbFiles(outfile); assertThat(result, is(1)); ZipFile zipFile = new ZipFile(outfile); assertThat(zipFile.getEntry("/dir1/dir2/FrenchScrum.odp"), is(notNullValue())); zipFile.close(); }
From source file:org.slc.sli.ingestion.landingzone.ZipFileUtil.java
/** * Returns a stream for a file stored in the ZIP archive. * * @param zipFile ZIP archive/* w ww . j a v a2 s. c o m*/ * @param fileName Name of the file to get stream for * @return Input Stream for the requested file entry * @throws IOException IO Exception * @throws FileNotFoundException */ public static InputStream getInputStreamForFile(File zipFile, String fileName) throws IOException { ZipFile zf = null; InputStream fileInputStream = null; try { zf = new ZipFile(zipFile); ZipArchiveEntry entry = zf.getEntry(fileName); if (entry == null || entry.isDirectory()) { String msg = MessageFormat.format("No file entry is found for {0} withing the {0} archive", fileName, zipFile); throw new FileNotFoundException(msg); } final ZipFile fzf = zf; fileInputStream = new BufferedInputStream(zf.getInputStream(entry)) { @Override public void close() throws IOException { super.close(); ZipFile.closeQuietly(fzf); } }; } finally { if (fileInputStream == null) { ZipFile.closeQuietly(zf); } } return fileInputStream; }