Java Code Examples for java.util.zip.ZipFile#OPEN_READ

The following examples show how to use java.util.zip.ZipFile#OPEN_READ . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: DetectZipUtil.java    From synopsys-detect with Apache License 2.0 6 votes vote down vote up
public static void unzip(final File zip, final File dest, final Charset charset) throws IOException {
    final Path destPath = dest.toPath();
    try (final ZipFile zipFile = new ZipFile(zip, ZipFile.OPEN_READ, charset)) {
        final Enumeration<? extends ZipEntry> entries = zipFile.entries();
        while (entries.hasMoreElements()) {
            final ZipEntry entry = entries.nextElement();
            final 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 (final InputStream in = zipFile.getInputStream(entry)) {
                    try (final OutputStream out = new FileOutputStream(entryPath.toFile())) {
                        IOUtils.copy(in, out);
                    }
                }
            }
        }
    }
}
 
Example 2
Source File: MultiReleaseJarAPI.java    From openjdk-jdk9 with GNU General Public License v2.0 6 votes vote down vote up
@Test
public void testNames() throws Exception {
    String rname = "version/Version.class";
    String vname = "META-INF/versions/9/version/Version.class";
    ZipEntry ze1;
    ZipEntry ze2;
    try (JarFile jf = new JarFile(multirelease)) {
        ze1 = jf.getEntry(vname);
    }
    Assert.assertEquals(ze1.getName(), vname);
    try (JarFile jf = new JarFile(multirelease, true, ZipFile.OPEN_READ, Runtime.Version.parse("9"))) {
        ze2 = jf.getEntry(rname);
    }
    Assert.assertEquals(ze2.getName(), rname);
    Assert.assertNotEquals(ze1.getName(), ze2.getName());
}
 
Example 3
Source File: DetectZipUtil.java    From hub-detect with Apache License 2.0 6 votes vote down vote up
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);
                    }
                }
            }
        }
    }
}
 
Example 4
Source File: MultiReleaseJarSecurity.java    From openjdk-jdk9 with GNU General Public License v2.0 5 votes vote down vote up
@Test
public void testCertsAndSigners() throws IOException {
    try (JarFile jf = new JarFile(signedmultirelease, true, ZipFile.OPEN_READ, Runtime.version())) {
        CertsAndSigners vcas = new CertsAndSigners(jf, jf.getJarEntry("version/Version.class"));
        CertsAndSigners rcas = new CertsAndSigners(jf, jf.getJarEntry("META-INF/versions/" + MAJOR_VERSION + "/version/Version.class"));
        Assert.assertTrue(Arrays.equals(rcas.getCertificates(), vcas.getCertificates()));
        Assert.assertTrue(Arrays.equals(rcas.getCodeSigners(), vcas.getCodeSigners()));
    }
}
 
Example 5
Source File: ZipLocator.java    From MikuMikuStudio with BSD 2-Clause "Simplified" License 5 votes vote down vote up
public void setRootPath(String rootPath) {
    try{
        zipfile = new ZipFile(new File(rootPath), ZipFile.OPEN_READ);
    }catch (IOException ex){
        throw new AssetLoadException("Failed to open zip file: " + rootPath, ex);
    }
}
 
Example 6
Source File: PluginManifestUtil.java    From AndroidPlugin with MIT License 5 votes vote down vote up
static void setManifestInfo(Context context, String apkPath, PlugInfo info)
        throws XmlPullParserException, IOException {

    ZipFile zipFile = new ZipFile(new File(apkPath), ZipFile.OPEN_READ);
    ZipEntry manifestXmlEntry = zipFile
            .getEntry(XmlManifestReader.DEFAULT_XML);

    String manifestXML = XmlManifestReader.getManifestXMLFromAPK(zipFile,
            manifestXmlEntry);
    PackageInfo pkgInfo = context.getPackageManager()
            .getPackageArchiveInfo(
                    apkPath,
                    PackageManager.GET_ACTIVITIES
                            | PackageManager.GET_RECEIVERS//
                            | PackageManager.GET_PROVIDERS//
                            | PackageManager.GET_META_DATA//
                            | PackageManager.GET_SHARED_LIBRARY_FILES//
            // | PackageManager.GET_SERVICES//
            // | PackageManager.GET_SIGNATURES//
            );
    // Log.d("ManifestReader: setManifestInfo", "GET_SHARED_LIBRARY_FILES="
    // + pkgInfo.applicationInfo.nativeLibraryDir);
    info.setPackageInfo(pkgInfo);
    File libdir = ActivityOverider.getPluginLibDir(info.getId());
    try {
        if (extractLibFile(zipFile, libdir)) {
            pkgInfo.applicationInfo.nativeLibraryDir = libdir
                    .getAbsolutePath();
        }
    } finally {
        zipFile.close();
    }
    setAttrs(info, manifestXML);
}
 
Example 7
Source File: MultiReleaseJarAPI.java    From openjdk-jdk9 with GNU General Public License v2.0 4 votes vote down vote up
@Test
public void isMultiReleaseJar() throws Exception {
    try (JarFile jf = new JarFile(unversioned)) {
        Assert.assertFalse(jf.isMultiRelease());
    }

    try (JarFile jf = new JarFile(unversioned, true, ZipFile.OPEN_READ, Runtime.version())) {
        Assert.assertFalse(jf.isMultiRelease());
    }

    try (JarFile jf = new JarFile(multirelease)) {
        Assert.assertTrue(jf.isMultiRelease());
    }

    try (JarFile jf = new JarFile(multirelease, true, ZipFile.OPEN_READ, Runtime.version())) {
        Assert.assertTrue(jf.isMultiRelease());
    }

    testCustomMultiReleaseValue("true", true);
    testCustomMultiReleaseValue("true\r\nOther: value", true);
    testCustomMultiReleaseValue("true\nOther: value", true);
    testCustomMultiReleaseValue("true\rOther: value", true);

    testCustomMultiReleaseValue("false", false);
    testCustomMultiReleaseValue(" true", false);
    testCustomMultiReleaseValue("true ", false);
    testCustomMultiReleaseValue("true\n ", false);
    testCustomMultiReleaseValue("true\r ", false);
    testCustomMultiReleaseValue("true\n true", false);
    testCustomMultiReleaseValue("true\r\n true", false);

    // generate "random" Strings to use as extra attributes, and
    // verify that Multi-Release: true is always properly matched
    for (int i = 0; i < 100; i++) {
        byte[] keyBytes = new byte[RANDOM.nextInt(70) + 1];
        Arrays.fill(keyBytes, (byte)('a' + RANDOM.nextInt(24)));
        byte[] valueBytes = new byte[RANDOM.nextInt(70) + 1];
        Arrays.fill(valueBytes, (byte)('a' + RANDOM.nextInt(24)));

        String key = new String(keyBytes, StandardCharsets.UTF_8);
        String value = new String(valueBytes, StandardCharsets.UTF_8);
        // test that Multi-Release: true anywhere in the manifest always
        // return true
        testCustomMultiReleaseValue("true", Map.of(key, value), true);

        // test that we don't get any false positives
        testCustomMultiReleaseValue("false", Map.of(key, value), false);
    }
}
 
Example 8
Source File: URLJarFile.java    From openjdk-jdk9 with GNU General Public License v2.0 4 votes vote down vote up
private URLJarFile(URL url, URLJarFileCloseController closeController, Runtime.Version version)
        throws IOException {
    super(new File(ParseUtil.decode(url.getFile())), true, ZipFile.OPEN_READ, version);
    this.closeController = closeController;
}
 
Example 9
Source File: URLJarFile.java    From jdk8u-jdk with GNU General Public License v2.0 4 votes vote down vote up
public URLJarFile(File file, URLJarFileCloseController closeController) throws IOException {
    super(file, true, ZipFile.OPEN_READ | ZipFile.OPEN_DELETE);
    this.closeController = closeController;
}
 
Example 10
Source File: URLJarFile.java    From openjdk-8-source with GNU General Public License v2.0 4 votes vote down vote up
public URLJarFile(File file, URLJarFileCloseController closeController) throws IOException {
    super(file, true, ZipFile.OPEN_READ | ZipFile.OPEN_DELETE);
    this.closeController = closeController;
}
 
Example 11
Source File: URLJarFile.java    From Bytecoder with Apache License 2.0 4 votes vote down vote up
private URLJarFile(URL url, URLJarFileCloseController closeController, Runtime.Version version)
        throws IOException {
    super(new File(ParseUtil.decode(url.getFile())), true, ZipFile.OPEN_READ, version);
    this.closeController = closeController;
}
 
Example 12
Source File: URLJarFile.java    From Bytecoder with Apache License 2.0 4 votes vote down vote up
private URLJarFile(File file, URLJarFileCloseController closeController, Runtime.Version version)
        throws IOException {
    super(file, true, ZipFile.OPEN_READ | ZipFile.OPEN_DELETE, version);
    this.closeController = closeController;
}
 
Example 13
Source File: ClassFileSourceImpl.java    From cfr with MIT License 4 votes vote down vote up
private JarContent processClassPathFile(final File file, boolean dump, AnalysisType analysisType) {
    List<String> content = ListFactory.newList();
    Map<String, String> manifest;
    try {
        ZipFile zipFile = new ZipFile(file, ZipFile.OPEN_READ);
        manifest = getManifestContent(zipFile);
        try {
            Enumeration<? extends ZipEntry> enumeration = zipFile.entries();
            while (enumeration.hasMoreElements()) {
                ZipEntry entry = enumeration.nextElement();
                if (!entry.isDirectory()) {
                    String name = entry.getName();
                    if (name.endsWith(".class")) {
                        if (dump) {
                            System.out.println("  " + name);
                        }
                        content.add(name);
                    } else {
                        if (dump) {
                            System.out.println("  [ignoring] " + name);
                        }
                    }
                }
            }
        } finally {
            zipFile.close();
        }
    } catch (IOException e) {
        return null;
    }
    if (analysisType == AnalysisType.WAR) {
        // Strip WEB-INF/classes from the front of class files.
        final int prefixLen = MiscConstants.WAR_PREFIX.length();
        content = Functional.map(Functional.filter(content, new Predicate<String>() {
            @Override
            public boolean test(String in) {
                return in.startsWith(MiscConstants.WAR_PREFIX);
            }
        }), new UnaryFunction<String, String>() {
            @Override
            public String invoke(String arg) {
                return arg.substring(prefixLen);
            }
        });
    }

    return new JarContentImpl(content, manifest, analysisType);
}
 
Example 14
Source File: URLJarFile.java    From openjdk-8 with GNU General Public License v2.0 4 votes vote down vote up
public URLJarFile(File file, URLJarFileCloseController closeController) throws IOException {
    super(file, true, ZipFile.OPEN_READ | ZipFile.OPEN_DELETE);
    this.closeController = closeController;
}
 
Example 15
Source File: URLJarFile.java    From jdk8u-jdk with GNU General Public License v2.0 4 votes vote down vote up
public URLJarFile(File file, URLJarFileCloseController closeController) throws IOException {
    super(file, true, ZipFile.OPEN_READ | ZipFile.OPEN_DELETE);
    this.closeController = closeController;
}
 
Example 16
Source File: URLJarFile.java    From openjdk-jdk8u with GNU General Public License v2.0 4 votes vote down vote up
public URLJarFile(File file, URLJarFileCloseController closeController) throws IOException {
    super(file, true, ZipFile.OPEN_READ | ZipFile.OPEN_DELETE);
    this.closeController = closeController;
}
 
Example 17
Source File: URLJarFile.java    From TencentKona-8 with GNU General Public License v2.0 4 votes vote down vote up
public URLJarFile(File file, URLJarFileCloseController closeController) throws IOException {
    super(file, true, ZipFile.OPEN_READ | ZipFile.OPEN_DELETE);
    this.closeController = closeController;
}
 
Example 18
Source File: URLJarFile.java    From hottub with GNU General Public License v2.0 4 votes vote down vote up
public URLJarFile(File file, URLJarFileCloseController closeController) throws IOException {
    super(file, true, ZipFile.OPEN_READ | ZipFile.OPEN_DELETE);
    this.closeController = closeController;
}
 
Example 19
Source File: JarFile.java    From Bytecoder with Apache License 2.0 2 votes vote down vote up
/**
 * Creates a new {@code JarFile} to read from the specified
 * {@code File} object. The {@code JarFile} will be verified if
 * it is signed.
 * @param file the jar file to be opened for reading
 * @throws IOException if an I/O error has occurred
 * @throws SecurityException if access to the file is denied
 *         by the SecurityManager
 */
public JarFile(File file) throws IOException {
    this(file, true, ZipFile.OPEN_READ);
}
 
Example 20
Source File: JarFile.java    From Bytecoder with Apache License 2.0 2 votes vote down vote up
/**
 * Creates a new {@code JarFile} to read from the specified
 * file {@code name}. The {@code JarFile} will be verified if
 * it is signed.
 * @param name the name of the jar file to be opened for reading
 * @throws IOException if an I/O error has occurred
 * @throws SecurityException if access to the file is denied
 *         by the SecurityManager
 */
public JarFile(String name) throws IOException {
    this(new File(name), true, ZipFile.OPEN_READ);
}