List of usage examples for java.util.zip ZipEntry isDirectory
public boolean isDirectory()
From source file:com.acciente.commons.loader.ClassFinder.java
private static Set findInJar(File oJarFile, String sPackageName, Pattern oClassNamePattern, Set oClassNameSet) throws IOException { ZipFile oZipFile;//from www.jav a2 s .com Enumeration oZipFileEntries; oZipFile = new ZipFile(oJarFile); oZipFileEntries = oZipFile.entries(); while (oZipFileEntries.hasMoreElements()) { ZipEntry oZipFileEntry = (ZipEntry) oZipFileEntries.nextElement(); String sClassname = getFQClassname(oZipFileEntry); if (!oZipFileEntry.isDirectory() && sClassname.startsWith(sPackageName)) { if (oClassNamePattern.matcher(sClassname).matches()) { oClassNameSet.add(sClassname); } } } return oClassNameSet; }
From source file:org.apache.maven.classpath.munger.validation.JarValidationUtilsTest.java
private static byte[] createTestJar(Collection<? extends ZipEntry> entriesList, byte... data) throws IOException { ByteArrayOutputStream baos = new ByteArrayOutputStream( data.length + entriesList.size() * 64 * 2 + Byte.MAX_VALUE); try (JarOutputStream jarFile = new JarOutputStream(baos)) { for (ZipEntry zipEntry : entriesList) { jarFile.putNextEntry(zipEntry); try { if (zipEntry.isDirectory()) { continue; }//from www. j a v a 2s .c o m jarFile.write(data); } finally { jarFile.closeEntry(); } } } finally { baos.close(); } return baos.toByteArray(); }
From source file:com.gelakinetic.mtgfam.helpers.ZipUtils.java
/** * Unzip a file directly into getFilesDir() * * @param zipFile The zip file to unzip/* w w w .j ava 2 s.c o m*/ * @param context The application context, for getting files and the like * @throws IOException Thrown if something goes wrong with unzipping and writing */ private static void unZipIt(ZipFile zipFile, Context context) throws IOException { Enumeration<? extends ZipEntry> entries; entries = zipFile.entries(); while (entries.hasMoreElements()) { ZipEntry entry = entries.nextElement(); if (entry.isDirectory()) { /* Assume directories are stored parents first then children. * This is not robust, just for demonstration purposes. */ if (!(new File(entry.getName())).mkdir()) { return; } continue; } String[] path = entry.getName().split("/"); String pathCat = ""; if (path.length > 1) { for (int i = 0; i < path.length - 1; i++) { pathCat += path[i] + "/"; File tmp = new File(context.getFilesDir(), pathCat); if (!tmp.exists()) { if (!tmp.mkdir()) { return; } } } } InputStream in = zipFile.getInputStream(entry); OutputStream out; if (entry.getName().contains("_preferences.xml")) { String sharedPrefsDir = context.getFilesDir().getPath(); sharedPrefsDir = sharedPrefsDir.substring(0, sharedPrefsDir.lastIndexOf("/")) + "/shared_prefs/"; out = new BufferedOutputStream(new FileOutputStream(new File(sharedPrefsDir, entry.getName()))); } else { out = new BufferedOutputStream( new FileOutputStream(new File(context.getFilesDir(), entry.getName()))); } byte[] buffer = new byte[1024]; int len; while ((len = in.read(buffer)) >= 0) { out.write(buffer, 0, len); } in.close(); out.close(); } zipFile.close(); }
From source file:com.blackducksoftware.integration.hub.detect.util.DetectZipUtil.java
public static void unzip(File zip, File dest, Charset charset) throws IOException { Path destPath = dest.toPath(); try (ZipFile zipFile = new ZipFile(zip, ZipFile.OPEN_READ, charset)) { Enumeration<? extends ZipEntry> entries = zipFile.entries(); while (entries.hasMoreElements()) { ZipEntry entry = entries.nextElement(); Path entryPath = destPath.resolve(entry.getName()); if (!entryPath.normalize().startsWith(dest.toPath())) throw new IOException("Zip entry contained path traversal"); if (entry.isDirectory()) { Files.createDirectories(entryPath); } else { Files.createDirectories(entryPath.getParent()); try (InputStream in = zipFile.getInputStream(entry)) { try (OutputStream out = new FileOutputStream(entryPath.toFile())) { IOUtils.copy(in, out); }/*from www. ja v a 2s . c om*/ } } } } }
From source file:Main.java
private static File unzip(Context context, InputStream in) throws IOException { File tmpDb = null;//from w w w . j av a 2 s .com int dbFiles = 0; try { tmpDb = File.createTempFile("import", ".db"); ZipInputStream zin = new ZipInputStream(in); ZipEntry sourceEntry; while (true) { sourceEntry = zin.getNextEntry(); if (sourceEntry == null) { break; } if (sourceEntry.isDirectory()) { zin.closeEntry(); continue; } FileOutputStream fOut; if (sourceEntry.getName().endsWith(".db")) { // Write database to tmp file fOut = new FileOutputStream(tmpDb); dbFiles++; } else { // Write all other files(images) to files dir in apps data int start = sourceEntry.getName().lastIndexOf("/") + 1; String name = sourceEntry.getName().substring(start); fOut = context.openFileOutput(name, Context.MODE_PRIVATE); } final OutputStream targetStream = fOut; try { int read; while (true) { byte[] buffer = new byte[1024]; read = zin.read(buffer); if (read == -1) { break; } targetStream.write(buffer, 0, read); } targetStream.flush(); } finally { safeCloseClosable(targetStream); } zin.closeEntry(); } } finally { safeCloseClosable(in); } if (dbFiles != 1) { throw new IllegalStateException("Input file is not a valid backup"); } return tmpDb; }
From source file:Main.java
/** * extracts a zip file to the given dir//from w w w . ja v a 2s . c o m * @param archive * @param destDir * @throws IOException * @throws ZipException * @throws Exception */ public static void extractZipArchive(File archive, File destDir) throws ZipException, IOException { if (!destDir.exists()) { destDir.mkdir(); } ZipFile zipFile = new ZipFile(archive); Enumeration<? extends ZipEntry> entries = zipFile.entries(); byte[] buffer = new byte[16384]; int len; while (entries.hasMoreElements()) { ZipEntry entry = entries.nextElement(); String entryFileName = entry.getName(); File dir = buildDirectoryHierarchyFor(entryFileName, destDir); if (!dir.exists()) { dir.mkdirs(); } if (!entry.isDirectory()) { BufferedOutputStream bos = new BufferedOutputStream( new FileOutputStream(new File(destDir, entryFileName))); BufferedInputStream bis = new BufferedInputStream(zipFile.getInputStream(entry)); while ((len = bis.read(buffer)) > 0) { bos.write(buffer, 0, len); } bos.flush(); bos.close(); bis.close(); } } }
From source file:net.chris54721.infinitycubed.utils.Utils.java
public static void unzip(File zipFile, File outputFolder) { InputStream in = null;//w w w. ja v a 2 s. c o m OutputStream out = null; try { ZipFile zip = new ZipFile(zipFile); Enumeration<? extends ZipEntry> entries = zip.entries(); if (!outputFolder.isDirectory()) outputFolder.mkdirs(); while (entries.hasMoreElements()) { ZipEntry entry = entries.nextElement(); File entryTarget = new File(outputFolder, entry.getName()); entryTarget.getParentFile().mkdirs(); if (entry.isDirectory()) entryTarget.mkdirs(); else { in = zip.getInputStream(entry); out = new FileOutputStream(entryTarget); IOUtils.copy(in, out); } } } catch (Exception e) { LogHelper.error("Failed extracting " + zipFile.getName(), e); } finally { if (in != null) IOUtils.closeQuietly(in); if (out != null) IOUtils.closeQuietly(out); } }
From source file:herddb.upgrade.ZIPUtils.java
public static List<File> unZip(InputStream fs, File outDir) throws IOException { try (ZipInputStream zipStream = new ZipInputStream(fs, StandardCharsets.UTF_8);) { ZipEntry entry = zipStream.getNextEntry(); List<File> listFiles = new ArrayList<>(); while (entry != null) { if (entry.isDirectory()) { entry = zipStream.getNextEntry(); continue; }// w ww . j ava 2s . c o m String normalized = normalizeFilenameForFileSystem(entry.getName()); File outFile = new File(outDir, normalized); File parentDir = outFile.getParentFile(); if (parentDir != null && !parentDir.isDirectory()) { Files.createDirectories(parentDir.toPath()); } listFiles.add(outFile); try (FileOutputStream out = new FileOutputStream(outFile); SimpleBufferedOutputStream oo = new SimpleBufferedOutputStream(out)) { IOUtils.copyLarge(zipStream, oo); } entry = zipStream.getNextEntry(); } return listFiles; } catch (IllegalArgumentException ex) { throw new IOException(ex); } }
From source file:org.apache.hive.beeline.ClassNameCompleter.java
/** * Get clazz names from a jar file path/*from w w w . j av a2s. c om*/ * @param path specifies the jar file's path * @return */ private static List<String> getClassNamesFromJar(String path) { List<String> classNames = new ArrayList<String>(); ZipInputStream zip = null; try { zip = new ZipInputStream(new FileInputStream(path)); ZipEntry entry = zip.getNextEntry(); while (entry != null) { if (!entry.isDirectory() && entry.getName().endsWith(clazzFileNameExtension)) { StringBuilder className = new StringBuilder(); for (String part : entry.getName().split("/")) { if (className.length() != 0) { className.append("."); } className.append(part); if (part.endsWith(clazzFileNameExtension)) { className.setLength(className.length() - clazzFileNameExtension.length()); } } classNames.add(className.toString()); } entry = zip.getNextEntry(); } } catch (IOException e) { LOG.error("Fail to parse the class name from the Jar file due to the exception:" + e); } finally { if (zip != null) { try { zip.close(); } catch (IOException e) { LOG.error("Fail to close the file due to the exception:" + e); } } } return classNames; }
From source file:functionalTests.vfsprovider.TestProActiveProvider.java
public static void extractZip(final ZipInputStream zipStream, final File dstFile) throws IOException { ZipEntry zipEntry; while ((zipEntry = zipStream.getNextEntry()) != null) { final File dstSubFile = new File(dstFile, zipEntry.getName()); if (zipEntry.isDirectory()) { dstSubFile.mkdirs();//from w ww .j av a 2 s .c o m if (!dstSubFile.exists() || !dstSubFile.isDirectory()) throw new IOException("Could not create directory: " + dstSubFile); } else { final OutputStream os = new BufferedOutputStream(new FileOutputStream(dstSubFile)); try { int data; while ((data = zipStream.read()) != -1) os.write(data); } finally { os.close(); } } } }