All Downloads are FREE. Search and download functionalities are using the official Maven repository.

net.ravendb.embedded.ExtractFromJarServerProvider Maven / Gradle / Ivy

There is a newer version: 6.0.1
Show newest version
package net.ravendb.embedded;

import org.apache.commons.io.IOUtils;

import java.io.*;
import java.util.zip.ZipEntry;
import java.util.zip.ZipInputStream;

public class ExtractFromJarServerProvider implements IProvideRavenDBServer {

    @Override
    public void provide(String targetDirectory) throws IOException {
        InputStream resourceAsStream =
                ExtractFromJarServerProvider.class.getResourceAsStream("/ravendb-server.zip");

        if (resourceAsStream == null) {
            throw new IllegalStateException("Unable to find resource: ravendb-server.zip");
        }

        unzip(resourceAsStream, targetDirectory);
    }

    public static void unzip(InputStream source, String out) throws IOException {
        try (ZipInputStream zis = new ZipInputStream(source)) {

            ZipEntry entry = zis.getNextEntry();

            while (entry != null) {

                File file = new File(out, entry.getName());

                if (entry.isDirectory()) {
                    file.mkdirs();
                } else {
                    File parent = file.getParentFile();

                    if (!parent.exists()) {
                        parent.mkdirs();
                    }

                    try (BufferedOutputStream bos = new BufferedOutputStream(new FileOutputStream(file))) {
                        IOUtils.copy(zis, bos);
                    }
                }
                entry = zis.getNextEntry();
            }
        }
    }
}




© 2015 - 2025 Weber Informatics LLC | Privacy Policy