public static File getResource()

in wagon-tcks/wagon-tck-http/src/main/java/org/apache/maven/wagon/tck/http/util/TestUtil.java [50:129]


    public static File getResource(final String path) throws URISyntaxException, IOException {
        URL resource = Thread.currentThread().getContextClassLoader().getResource(path);
        if (resource == null) {
            throw new IllegalStateException("Cannot find classpath resource: " + path);
        }

        if (resource.getProtocol().startsWith("jar")) {
            // File f = new File( path );
            // f = File.createTempFile( f.getName() + ".", ".tmp" );

            String url = resource.toExternalForm();
            int startIdx = url.lastIndexOf(':') + 1;
            int endIdx = url.indexOf("!");
            url = url.substring(startIdx, endIdx);

            File base = BASES.get(url);
            if (base == null) {
                File urlFile = new File(url);

                base = new File("target/tck-resources/" + urlFile.getName());
                base.getParentFile().mkdirs();

                logger.info("unpacking test resources in jar: " + url);
                JarFile jf = null;
                try {
                    jf = new JarFile(urlFile);

                    InputStream in = null;
                    OutputStream out = null;

                    for (Enumeration<JarEntry> en = jf.entries(); en.hasMoreElements(); ) {
                        JarEntry je = en.nextElement();
                        final File zipEntryFile = new File(base, je.getName());
                        if (!zipEntryFile
                                .toPath()
                                .normalize()
                                .startsWith(base.toPath().normalize())) {
                            throw new IOException("Bad zip entry");
                        }
                        File target = zipEntryFile.getAbsoluteFile();
                        if (je.isDirectory()) {
                            target.mkdirs();
                        } else {
                            target.getParentFile().mkdirs();

                            try {
                                in = jf.getInputStream(je);
                                out = new FileOutputStream(target);

                                IOUtil.copy(in, out);

                                out.close();
                                out = null;

                                in.close();
                                in = null;
                            } finally {
                                IOUtil.close(in);
                                IOUtil.close(out);
                            }
                        }
                    }

                    BASES.put(url, base);
                } finally {
                    if (jf != null) {
                        try {
                            jf.close();
                        } catch (Exception e) {
                            // ignore
                        }
                    }
                }
            }

            return new File(base, path);
        } else {
            return new File(resource.toURI().normalize());
        }
    }