private static boolean isZipFileCanBeOpen(File file) { try (ZipFile zipFile = new ZipFile(file)) { return zipFile.entries().hasMoreElements(); } catch (Exception e) { return false; } }
private InputStream getFromJar (String jarFile, String sharedLibrary) throws IOException { ZipFile file = new ZipFile(nativesJar); ZipEntry entry = file.getEntry(sharedLibrary); return file.getInputStream(entry); }
private static List<String> getZipFileList(File file) { List<String> filesList = new ArrayList<>(); try (ZipFile zipFile = new ZipFile(file)) { Enumeration<? extends ZipEntry> entries = zipFile.entries(); while (entries.hasMoreElements()) { ZipEntry entry = entries.nextElement(); filesList.add(entry.getName()); } } catch (Exception e) { LOG.error("Error read zip file '{}'", file.getAbsolutePath(), e); } return filesList; }
/** * @return null if jarFile doesn't contain a manifest, the * manifest otherwise. * @since Ant 1.5.2 */ private Manifest getManifestFromJar(File jarFile) throws IOException { try (ZipFile zf = new ZipFile(jarFile)) { // must not use getEntry as "well behaving" applications // must accept the manifest in any capitalization ZipEntry ze = StreamUtils.enumerationAsStream(zf.entries()) .filter(entry -> MANIFEST_NAME.equalsIgnoreCase(entry.getName())) .findFirst().orElse(null); if (ze == null) { return null; } try (InputStreamReader isr = new InputStreamReader(zf.getInputStream(ze), "UTF-8")) { return getManifest(isr); } } }
/** * Determines if a zip archive contains a particular directory. * * @param zipfile path to the zipped file * @param target directory being looked for in the zip. * @return boolean whether or not the directory exists in the zip. */ public static boolean zipDoesContainDir(String zipfile, String target) throws IOException { List<ZipEntry> entries = (List<ZipEntry>) Collections.list(new ZipFile(zipfile).entries()); String targetDir = target + "/"; for (ZipEntry entry : entries) { String name = entry.getName(); if (name.startsWith(targetDir)) { return true; } } return false; }
private List<String> getJarContent(File file) { final List<String> result = new ArrayList<>(); try { if (file.getName().endsWith(".jar")) { ZipFile zip = new ZipFile(file); Collections.list(zip.entries()).stream().map(ZipEntry::getName).forEach(result::add); } } catch (IOException e) { throw new RuntimeException(e); } return result; } }
/** * Return the files' path in ZIP file. * * @param zipFile The ZIP file. * @return the files' path in ZIP file * @throws IOException if an I/O error has occurred */ public static List<String> getFilesPath(final File zipFile) throws IOException { if (zipFile == null) return null; List<String> paths = new ArrayList<>(); ZipFile zip = new ZipFile(zipFile); Enumeration<?> entries = zip.entries(); while (entries.hasMoreElements()) { String entryName = ((ZipEntry) entries.nextElement()).getName(); if (entryName.contains("../")) { System.out.println("entryName: " + entryName + " is dangerous!"); paths.add(entryName); } else { paths.add(entryName); } } zip.close(); return paths; }
private InputStream getFromJar (String jarFile, String sharedLibrary) throws IOException { ZipFile file = new ZipFile(nativesJar); ZipEntry entry = file.getEntry(sharedLibrary); return file.getInputStream(entry); }
public static void main(String[] args) throws IOException { ZipFile zipFile = new ZipFile("C:/test.zip"); Enumeration<? extends ZipEntry> entries = zipFile.entries(); while(entries.hasMoreElements()){ ZipEntry entry = entries.nextElement(); InputStream stream = zipFile.getInputStream(entry); } }
ZipFile zipFile = new ZipFile("file.zip"); ZipEntry zipEntry = zipFile.getEntry("fileName.txt"); InputStream inputStream = zipFile.getInputStream(zipEntry);
@Test public void zip_directory() throws IOException { File foo = FileUtils.toFile(getClass().getResource("/org/sonar/api/utils/ZipUtilsTest/shouldZipDirectory/foo.txt")); File dir = foo.getParentFile(); File zip = temp.newFile(); ZipUtils.zipDir(dir, zip); assertThat(zip).exists().isFile(); assertThat(zip.length()).isGreaterThan(1L); Iterator<? extends ZipEntry> zipEntries = Iterators.forEnumeration(new ZipFile(zip).entries()); assertThat(zipEntries).hasSize(4); File unzipDir = temp.newFolder(); ZipUtils.unzip(zip, unzipDir); assertThat(new File(unzipDir, "bar.txt")).exists().isFile(); assertThat(new File(unzipDir, "foo.txt")).exists().isFile(); assertThat(new File(unzipDir, "dir1/hello.properties")).exists().isFile(); }
/** * Return the files' comment in ZIP file. * * @param zipFile The ZIP file. * @return the files' comment in ZIP file * @throws IOException if an I/O error has occurred */ public static List<String> getComments(final File zipFile) throws IOException { if (zipFile == null) return null; List<String> comments = new ArrayList<>(); ZipFile zip = new ZipFile(zipFile); Enumeration<?> entries = zip.entries(); while (entries.hasMoreElements()) { ZipEntry entry = ((ZipEntry) entries.nextElement()); comments.add(entry.getComment()); } zip.close(); return comments; }
public static String getZipEntryMd5(File file, String entryName) { ZipFile zipFile = null; try { zipFile = new ZipFile(file); ZipEntry entry = zipFile.getEntry(entryName); if (entry == null) { return null; } return MD5.getMD5(zipFile.getInputStream(entry), 1024 * 100); } catch (IOException e) { e.printStackTrace(); } finally { if (zipFile != null) { try { zipFile.close(); } catch (IOException e) { e.printStackTrace(); } } } return null; }
// ITS PSEUDOCODE!! private InputStream extractOnlyFile(String path) { ZipFile zf = new ZipFile(path); Enumeration e = zf.entries(); ZipEntry entry = (ZipEntry) e.nextElement(); // your only file return zf.getInputStream(entry); }
private InputStream readFile (String path) { if (nativesJar == null) { InputStream input = SharedLibraryLoader.class.getResourceAsStream("/" + path); if (input == null) throw new GdxRuntimeException("Unable to read file for extraction: " + path); return input; } // Read from JAR. try { ZipFile file = new ZipFile(nativesJar); ZipEntry entry = file.getEntry(path); if (entry == null) throw new GdxRuntimeException("Couldn't find '" + path + "' in JAR: " + nativesJar); return file.getInputStream(entry); } catch (IOException ex) { throw new GdxRuntimeException("Error reading '" + path + "' in JAR: " + nativesJar, ex); } }
public List<String> getFiles(File jarFile) throws ZipException, IOException { //System.out.println("Looking at " + jarFile); List<String> files = new ArrayList<>(); ZipFile zin = new ZipFile(jarFile); Enumeration<? extends ZipEntry> entries = zin.entries(); while (entries.hasMoreElements()) { ZipEntry entry = entries.nextElement(); String name = entry.getName(); if (name.matches(pattern)) { files.add(name); } } Collections.sort(files); return files; } }
private InputStream readFile (String path) { if (nativesJar == null) { InputStream input = SharedLibraryLoader.class.getResourceAsStream("/" + path); if (input == null) throw new GdxRuntimeException("Unable to read file for extraction: " + path); return input; } // Read from JAR. try { ZipFile file = new ZipFile(nativesJar); ZipEntry entry = file.getEntry(path); if (entry == null) throw new GdxRuntimeException("Couldn't find '" + path + "' in JAR: " + nativesJar); return file.getInputStream(entry); } catch (IOException ex) { throw new GdxRuntimeException("Error reading '" + path + "' in JAR: " + nativesJar, ex); } }
/** * Lists zip content. */ public static List<String> listZip(final File zipFile) throws IOException { List<String> entries = new ArrayList<>(); ZipFile zip = new ZipFile(zipFile); Enumeration zipEntries = zip.entries(); while (zipEntries.hasMoreElements()) { ZipEntry entry = (ZipEntry) zipEntries.nextElement(); String entryName = entry.getName(); entries.add(entryName); } return Collections.unmodifiableList(entries); }