Example usage for java.util.zip ZipEntry getName

List of usage examples for java.util.zip ZipEntry getName

Introduction

In this page you can find the example usage for java.util.zip ZipEntry getName.

Prototype

public String getName() 

Source Link

Document

Returns the name of the entry.

Usage

From source file:net.servicefixture.util.ReflectionUtils.java

/**
 * Finds all the concrete subclasses for given class in the the SAME JAR
 * file where the baseClass is loaded from.
 * // ww  w  .ja va 2 s  . co  m
 * @param baseClass
 *            the base class
 */
public static Class[] findSubClasses(Class baseClass) {
    String packagePath = "/" + baseClass.getPackage().getName().replace('.', '/');
    URL url = baseClass.getResource(packagePath);
    if (url == null) {
        return new Class[0];
    }
    List<Class> derivedClasses = new ArrayList<Class>();
    try {
        URLConnection connection = url.openConnection();
        if (connection instanceof JarURLConnection) {
            JarFile jarFile = ((JarURLConnection) connection).getJarFile();
            Enumeration e = jarFile.entries();
            while (e.hasMoreElements()) {
                ZipEntry entry = (ZipEntry) e.nextElement();
                String entryName = entry.getName();
                if (entryName.endsWith(".class")) {
                    String clazzName = entryName.substring(0, entryName.length() - 6);

                    clazzName = clazzName.replace('/', '.');
                    try {
                        Class clazz = Class.forName(clazzName);
                        if (isConcreteSubclass(baseClass, clazz)) {
                            derivedClasses.add(clazz);
                        }
                    } catch (Throwable ignoreIt) {
                    }
                }
            }
        } else if (connection instanceof FileURLConnection) {
            File file = new File(url.getFile());
            File[] files = file.listFiles();
            for (int i = 0; i < files.length; i++) {
                String filename = files[i].getName();
                if (filename.endsWith(".class")) {
                    filename = filename.substring(0, filename.length() - 6);
                    String clazzname = baseClass.getPackage().getName() + "." + filename;
                    try {
                        Class clazz = Class.forName(clazzname);
                        if (isConcreteSubclass(baseClass, clazz)) {
                            derivedClasses.add(clazz);
                        }
                    } catch (Throwable ignoreIt) {
                    }
                }
            }
        }
    } catch (IOException ignoreIt) {
    }
    return derivedClasses.toArray(new Class[derivedClasses.size()]);
}

From source file:com.comcast.magicwand.spells.web.iexplore.IePhoenixDriver.java

private static void extract(File src, String dstDir) throws IOException {
    ZipInputStream zis = null;/*from  ww  w  .  j  a  v a2  s. co m*/
    ZipEntry entry;

    try {
        zis = new ZipInputStream(new BufferedInputStream(new FileInputStream(src)));

        while (null != (entry = zis.getNextEntry())) {
            File dst = Paths.get(dstDir, entry.getName()).toFile();
            FileOutputStream fos = new FileOutputStream(dst);

            try {
                IOUtils.copy(zis, fos);
                fos.close();
            } finally {
                IOUtils.closeQuietly(fos);
            }
        }

        zis.close();
    } finally {
        IOUtils.closeQuietly(zis);
    }
}

From source file:com.taobao.android.builder.tools.sign.LocalSignHelper.java

private static Predicate<String> getNoCompressPredicate(String srcPath) throws IOException {
    Set<String> noCompressEntries = new HashSet<>();
    ZipFile zipFile = new ZipFile(srcPath);
    Enumeration<? extends ZipEntry> entries = zipFile.entries();
    while (entries.hasMoreElements()) {
        ZipEntry zipEntry = entries.nextElement();
        if (zipEntry.getMethod() == 0) {
            if (zipEntry.getName().endsWith(".so")) {
                nativeLibrariesPackagingMode = NativeLibrariesPackagingMode.UNCOMPRESSED_AND_ALIGNED;
            }/*from   ww  w.  ja  va 2  s.  c o m*/
            noCompressEntries.add(zipEntry.getName());
        }
    }
    return s -> noCompressEntries.contains(s);
}

From source file:Main.java

/** Returns true if these zip files act like equivalent sets.
 * The order of the zip entries is not important: if they contain
 * exactly the same contents, this returns true.
 * @param zip1 one zip file //www  .  j  a v  a 2  s  .co  m
 * @param zip2 another zip file
 * @return true if the two zip archives are equivalent sets
 * @throws IOException
 */
public static boolean zipEquals(File zip1, File zip2) throws IOException {
    if (zip1.equals(zip2))
        return true;

    InputStream in = null;
    ZipInputStream zipIn = null;
    try {
        in = new FileInputStream(zip1);
        zipIn = new ZipInputStream(in);
        ZipEntry e = zipIn.getNextEntry();
        Vector<String> entries = new Vector<String>();
        while (e != null) {
            entries.add(e.getName());

            InputStream other = getZipEntry(zip2, e.getName());
            if (other == null) {
                return false;
            }

            if (equals(zipIn, other) == false) {
                return false;
            }
            e = zipIn.getNextEntry();
        }
        //now we've established everything in zip1 is in zip2

        //but what if zip2 has entries zip1 doesn't?
        zipIn.close();
        in.close();

        in = new FileInputStream(zip2);
        zipIn = new ZipInputStream(in);
        e = zipIn.getNextEntry();
        while (e != null) {
            if (entries.contains(e.getName()) == false) {
                return false;
            }
            e = zipIn.getNextEntry();
        }

        //the entries are exactly the same
        return true;
    } finally {
        try {
            zipIn.close();
        } catch (Throwable t) {
        }
        try {
            in.close();
        } catch (Throwable t) {
        }
    }
}

From source file:com.ariatemplates.attester.maven.ArtifactExtractor.java

public static void unzip(File zipFile, File outputFolder) throws IOException {
    ZipInputStream zipInputStream = null;
    try {/*  w w  w  .  j a  v  a 2  s. c om*/
        ZipEntry entry = null;
        zipInputStream = new ZipInputStream(FileUtils.openInputStream(zipFile));
        while ((entry = zipInputStream.getNextEntry()) != null) {
            File outputFile = new File(outputFolder, entry.getName());

            if (entry.isDirectory()) {
                outputFile.mkdirs();
                continue;
            }

            OutputStream outputStream = null;
            try {
                outputStream = FileUtils.openOutputStream(outputFile);
                IOUtils.copy(zipInputStream, outputStream);
                outputStream.close();
            } catch (IOException exception) {
                outputFile.delete();
                throw new IOException(exception);
            } finally {
                IOUtils.closeQuietly(outputStream);
            }
        }
        zipInputStream.close();
    } finally {
        IOUtils.closeQuietly(zipInputStream);
    }
}

From source file:JarUtils.java

public static void unjar(InputStream in, File dest) throws IOException {
    if (!dest.exists()) {
        dest.mkdirs();/*from w  ww .j  ava  2 s .  c om*/
    }
    if (!dest.isDirectory()) {
        throw new IOException("Destination must be a directory.");
    }
    JarInputStream jin = new JarInputStream(in);
    byte[] buffer = new byte[1024];

    ZipEntry entry = jin.getNextEntry();
    while (entry != null) {
        String fileName = entry.getName();
        if (fileName.charAt(fileName.length() - 1) == '/') {
            fileName = fileName.substring(0, fileName.length() - 1);
        }
        if (fileName.charAt(0) == '/') {
            fileName = fileName.substring(1);
        }
        if (File.separatorChar != '/') {
            fileName = fileName.replace('/', File.separatorChar);
        }
        File file = new File(dest, fileName);
        if (entry.isDirectory()) {
            // make sure the directory exists
            file.mkdirs();
            jin.closeEntry();
        } else {
            // make sure the directory exists
            File parent = file.getParentFile();
            if (parent != null && !parent.exists()) {
                parent.mkdirs();
            }

            // dump the file
            OutputStream out = new FileOutputStream(file);
            int len = 0;
            while ((len = jin.read(buffer, 0, buffer.length)) != -1) {
                out.write(buffer, 0, len);
            }
            out.flush();
            out.close();
            jin.closeEntry();
            file.setLastModified(entry.getTime());
        }
        entry = jin.getNextEntry();
    }
    /* Explicity write out the META-INF/MANIFEST.MF so that any headers such
    as the Class-Path are see for the unpackaged jar
    */
    Manifest mf = jin.getManifest();
    if (mf != null) {
        File file = new File(dest, "META-INF/MANIFEST.MF");
        File parent = file.getParentFile();
        if (parent.exists() == false) {
            parent.mkdirs();
        }
        OutputStream out = new FileOutputStream(file);
        mf.write(out);
        out.flush();
        out.close();
    }
    jin.close();
}

From source file:com.taobao.android.builder.tools.sign.LocalSignHelper.java

private static Predicate<String> getNoCompressPredicate(File inputFile) throws IOException {
    List<String> paths = new ArrayList<>();
    ZipFile zFile = new ZipFile(inputFile);
    Enumeration<? extends ZipEntry> enumeration = zFile.entries();
    while (enumeration.hasMoreElements()) {
        ZipEntry zipEntry = enumeration.nextElement();
        if (zipEntry.getMethod() == 0) {
            paths.add(zipEntry.getName());
        }//from   w w w.ja  va 2  s . c om
    }
    return s -> paths.contains(s);

}

From source file:ch.admin.suis.msghandler.util.ZipUtils.java

/**
 * Decompress the given file to the specified directory.
 *
 * @param zipFile the ZIP file to decompress
 * @param toDir   the directory where the files from the archive must be placed; the
 *                file will be replaced if it already exists
 * @return a list of files that were extracted into the destination directory
 * @throws IllegalArgumentException if the provided file does not exist or the specified destination
 *                                  is not a directory
 * @throws IOException              if an IO error has occured (probably, a corrupted ZIP file?)
 *//*from  ww w. j  a v  a 2s.c om*/
public static List<File> decompress(File zipFile, File toDir) throws IOException {
    Validate.isTrue(zipFile.exists(), "ZIP file does not exist", zipFile.getAbsolutePath());
    Validate.isTrue(toDir.isDirectory(), toDir.getAbsolutePath() + " is not a directory");

    final ArrayList<File> files = new ArrayList<>();

    try (ZipInputStream zis = new ZipInputStream(new BufferedInputStream(new FileInputStream(zipFile)))) {

        // read the entries
        ZipEntry entry;
        while (null != (entry = zis.getNextEntry())) {
            if (entry.isDirectory()) {
                LOG.error(MessageFormat.format(
                        "cannot extract the entry {0} from the {1}. because it is a directory", entry.getName(),
                        zipFile.getAbsolutePath()));
                continue;
            }

            // extract the file to the provided destination
            // we have to watch out for a unique name of the file to be extracted:
            // it can happen, that several at the same time incoming messages have a file with the same name
            File extracted = new File(FileUtils.getFilename(toDir, entry.getName()));
            if (!extracted.getParentFile().mkdirs()) {
                LOG.debug("cannot make all the necessary directories for the file "
                        + extracted.getAbsolutePath() + " or " + "the path is already created ");
            }

            try (BufferedOutputStream dest = new BufferedOutputStream(new FileOutputStream(extracted),
                    BUFFER_SIZE)) {
                byte[] data = new byte[BUFFER_SIZE];
                int count;
                while ((count = zis.read(data, 0, BUFFER_SIZE)) != -1) {
                    dest.write(data, 0, count);
                }

                files.add(extracted);
            }
        }

    }

    return files;
}

From source file:com.codelanx.codelanxlib.util.Reflections.java

/**
 * Checks whether or not there is a plugin on the server with the name of
 * the passed {@code name} paramater. This method achieves this by scanning
 * the plugins folder and reading the {@code plugin.yml} files of any
 * respective jarfiles in the directory.
 * /*from  w  ww .  j  a v a  2  s .com*/
 * @since 0.1.0
 * @version 0.1.0
 * 
 * @param name The name of the plugin as specified in the {@code plugin.yml}
 * @return The {@link File} for the plugin jarfile, or {@code null} if not
 *         found
 */
public static File findPluginJarfile(String name) {
    File plugins = new File("plugins");
    Exceptions.illegalState(plugins.isDirectory(), "'plugins' isn't a directory! (wat)");
    for (File f : plugins.listFiles((File pathname) -> {
        return pathname.getPath().endsWith(".jar");
    })) {
        try (InputStream is = new FileInputStream(f); ZipInputStream zi = new ZipInputStream(is)) {
            ZipEntry ent = null;
            while ((ent = zi.getNextEntry()) != null) {
                if (ent.getName().equalsIgnoreCase("plugin.yml")) {
                    break;
                }
            }
            if (ent == null) {
                continue; //no plugin.yml found
            }
            ZipFile z = new ZipFile(f);
            try (InputStream fis = z.getInputStream(ent);
                    InputStreamReader fisr = new InputStreamReader(fis);
                    BufferedReader scan = new BufferedReader(fisr)) {
                String in;
                while ((in = scan.readLine()) != null) {
                    if (in.startsWith("name: ")) {
                        if (in.substring(6).equalsIgnoreCase(name)) {
                            return f;
                        }
                    }
                }
            }
        } catch (IOException ex) {
            Debugger.error(ex, "Error reading plugin jarfiles");
        }
    }
    return null;
}

From source file:eu.openanalytics.rsb.message.MultiFilesJob.java

/**
 * Adds all the files contained in a Zip archive to a job. Rejects Zips that
 * contain sub-directories./*from   w w  w.  j  a  v a2  s.  c  om*/
 * 
 * @param data
 * @param job
 * @throws IOException
 */
public static void addZipFilesToJob(final InputStream data, final MultiFilesJob job) throws IOException {
    final ZipInputStream zis = new ZipInputStream(data);
    ZipEntry ze = null;

    while ((ze = zis.getNextEntry()) != null) {
        if (ze.isDirectory()) {
            job.destroy();
            throw new IllegalArgumentException("Invalid zip archive: nested directories are not supported");
        }
        job.addFile(ze.getName(), zis);
        zis.closeEntry();
    }

    IOUtils.closeQuietly(zis);
}