Search in sources :

Example 1 with JarFile

use of org.springframework.boot.loader.jar.JarFile in project spring-boot by spring-projects.

the class JarFileArchive method getNestedArchive.

protected Archive getNestedArchive(Entry entry) throws IOException {
    JarEntry jarEntry = ((JarFileEntry) entry).getJarEntry();
    if (jarEntry.getComment().startsWith(UNPACK_MARKER)) {
        return getUnpackedNestedArchive(jarEntry);
    }
    JarFile jarFile = this.jarFile.getNestedJarFile(jarEntry);
    return new JarFileArchive(jarFile);
}
Also used : JarEntry(java.util.jar.JarEntry) JarFile(org.springframework.boot.loader.jar.JarFile)

Example 2 with JarFile

use of org.springframework.boot.loader.jar.JarFile in project spring-boot by spring-projects.

the class JarFileArchiveTests method nestedZip64ArchivesAreHandledGracefully.

@Test
void nestedZip64ArchivesAreHandledGracefully() throws Exception {
    File file = new File(this.tempDir, "test.jar");
    try (JarOutputStream output = new JarOutputStream(new FileOutputStream(file))) {
        JarEntry zip64JarEntry = new JarEntry("nested/zip64.jar");
        output.putNextEntry(zip64JarEntry);
        byte[] zip64JarData = writeZip64Jar();
        zip64JarEntry.setSize(zip64JarData.length);
        zip64JarEntry.setCompressedSize(zip64JarData.length);
        zip64JarEntry.setMethod(ZipEntry.STORED);
        CRC32 crc32 = new CRC32();
        crc32.update(zip64JarData);
        zip64JarEntry.setCrc(crc32.getValue());
        output.write(zip64JarData);
        output.closeEntry();
    }
    try (JarFile jarFile = new JarFile(file)) {
        ZipEntry nestedEntry = jarFile.getEntry("nested/zip64.jar");
        try (JarFile nestedJarFile = jarFile.getNestedJarFile(nestedEntry)) {
            Iterator<JarEntry> iterator = nestedJarFile.iterator();
            for (int i = 0; i < 65537; i++) {
                assertThat(iterator.hasNext()).as(i + "nth file is present").isTrue();
                iterator.next();
            }
        }
    }
}
Also used : CRC32(java.util.zip.CRC32) FileOutputStream(java.io.FileOutputStream) ZipEntry(java.util.zip.ZipEntry) JarOutputStream(java.util.jar.JarOutputStream) JarEntry(java.util.jar.JarEntry) JarFile(org.springframework.boot.loader.jar.JarFile) File(java.io.File) JarFile(org.springframework.boot.loader.jar.JarFile) Test(org.junit.jupiter.api.Test)

Example 3 with JarFile

use of org.springframework.boot.loader.jar.JarFile in project spring-boot by spring-projects.

the class LaunchedURLClassLoaderTests method resolveFromNestedWhileThreadIsInterrupted.

@Test
void resolveFromNestedWhileThreadIsInterrupted() throws Exception {
    File file = new File(this.tempDir, "test.jar");
    TestJarCreator.createTestJar(file);
    try (JarFile jarFile = new JarFile(file)) {
        URL url = jarFile.getUrl();
        try (LaunchedURLClassLoader loader = new LaunchedURLClassLoader(new URL[] { url }, null)) {
            Thread.currentThread().interrupt();
            URL resource = loader.getResource("nested.jar!/3.dat");
            assertThat(resource.toString()).isEqualTo(url + "nested.jar!/3.dat");
            URLConnection connection = resource.openConnection();
            try (InputStream input = connection.getInputStream()) {
                assertThat(input.read()).isEqualTo(3);
            }
            ((JarURLConnection) connection).getJarFile().close();
        } finally {
            Thread.interrupted();
        }
    }
}
Also used : InputStream(java.io.InputStream) JarFile(org.springframework.boot.loader.jar.JarFile) JarFile(org.springframework.boot.loader.jar.JarFile) File(java.io.File) URL(java.net.URL) URLConnection(java.net.URLConnection) JarURLConnection(java.net.JarURLConnection) Test(org.junit.jupiter.api.Test)

Example 4 with JarFile

use of org.springframework.boot.loader.jar.JarFile in project spring-boot by spring-projects.

the class PropertiesLauncherTests method loadResourceFromJarFile.

// gh-21575
@Test
void loadResourceFromJarFile() throws Exception {
    File jarFile = new File(this.tempDir, "app.jar");
    TestJarCreator.createTestJar(jarFile);
    System.setProperty("loader.home", this.tempDir.getAbsolutePath());
    System.setProperty("loader.path", "app.jar");
    this.launcher = new PropertiesLauncher();
    try {
        this.launcher.launch(new String[0]);
    } catch (Exception ex) {
        // Expected ClassNotFoundException
        LaunchedURLClassLoader classLoader = (LaunchedURLClassLoader) Thread.currentThread().getContextClassLoader();
        classLoader.close();
    }
    URL resource = new URL("jar:" + jarFile.toURI() + "!/nested.jar!/3.dat");
    byte[] bytes = FileCopyUtils.copyToByteArray(resource.openStream());
    assertThat(bytes).isNotEmpty();
}
Also used : File(java.io.File) JarFile(org.springframework.boot.loader.jar.JarFile) Assertions.assertThatIllegalStateException(org.assertj.core.api.Assertions.assertThatIllegalStateException) IOException(java.io.IOException) URL(java.net.URL) Test(org.junit.jupiter.api.Test)

Example 5 with JarFile

use of org.springframework.boot.loader.jar.JarFile in project spring-boot by spring-projects.

the class LaunchedURLClassLoaderTests method resolveFromNested.

@Test
void resolveFromNested() throws Exception {
    File file = new File(this.tempDir, "test.jar");
    TestJarCreator.createTestJar(file);
    try (JarFile jarFile = new JarFile(file)) {
        URL url = jarFile.getUrl();
        try (LaunchedURLClassLoader loader = new LaunchedURLClassLoader(new URL[] { url }, null)) {
            URL resource = loader.getResource("nested.jar!/3.dat");
            assertThat(resource.toString()).isEqualTo(url + "nested.jar!/3.dat");
            try (InputStream input = resource.openConnection().getInputStream()) {
                assertThat(input.read()).isEqualTo(3);
            }
        }
    }
}
Also used : InputStream(java.io.InputStream) JarFile(org.springframework.boot.loader.jar.JarFile) JarFile(org.springframework.boot.loader.jar.JarFile) File(java.io.File) URL(java.net.URL) Test(org.junit.jupiter.api.Test)

Aggregations

JarFile (org.springframework.boot.loader.jar.JarFile)5 File (java.io.File)4 Test (org.junit.jupiter.api.Test)4 URL (java.net.URL)3 InputStream (java.io.InputStream)2 JarEntry (java.util.jar.JarEntry)2 FileOutputStream (java.io.FileOutputStream)1 IOException (java.io.IOException)1 JarURLConnection (java.net.JarURLConnection)1 URLConnection (java.net.URLConnection)1 JarOutputStream (java.util.jar.JarOutputStream)1 CRC32 (java.util.zip.CRC32)1 ZipEntry (java.util.zip.ZipEntry)1 Assertions.assertThatIllegalStateException (org.assertj.core.api.Assertions.assertThatIllegalStateException)1