Search in sources :

Example 1 with UnzipUtility

use of org.vcell.util.UnzipUtility in project vcell by virtualcell.

the class BioModelsNetPanel method download.

private ExternalDocInfo download(String name, String id) throws Exception {
    // C:\Users\vasilescu\.vcell\simdata
    String simDataDir = ResourceUtil.getLocalRootDir().getAbsolutePath();
    String tempDir = simDataDir + File.separator + "temp";
    String destDirectory = tempDir + File.separator + id;
    String zipFilePath = destDirectory + ".zip";
    Path tempDirPath = Paths.get(tempDir);
    // temp may not be there, we make it
    Files.createDirectories(tempDirPath);
    byte[] responseContent = null;
    URL url = new URL(BeanUtils.getDynamicClientProperties().getProperty(PropertyLoader.BMDB_DOWNLOAD_URL) + id);
    ByteArrayOutputStream baos = new ByteArrayOutputStream();
    InputStream is = null;
    try {
        try {
            is = url.openStream();
        } catch (Exception e) {
            e.printStackTrace();
            if (is != null) {
                is.close();
            }
            // Try with http instead of https
            String newUrlString = url.toString().replaceFirst("^https", "http");
            url = new URL(newUrlString);
            is = url.openStream();
        }
        // Or whatever size you want to read in at a time.
        byte[] byteChunk = new byte[4096];
        int n;
        while ((n = is.read(byteChunk)) > 0) {
            baos.write(byteChunk, 0, n);
        }
        responseContent = baos.toByteArray();
    } catch (IOException e) {
        System.err.printf("Failed while reading bytes from %s: %s", url.toExternalForm(), e.getMessage());
        // e.printStackTrace ();
        throw new RuntimeException("Failed while reading bytes from: " + url.toExternalForm());
    } finally {
        if (is != null) {
            is.close();
        }
    }
    if (responseContent == null) {
        throw new RuntimeException("Failed while reading bytes from: " + url.toExternalForm());
    }
    try {
        File file = new File(zipFilePath);
        Files.write(file.toPath(), responseContent, StandardOpenOption.CREATE, StandardOpenOption.WRITE, StandardOpenOption.TRUNCATE_EXISTING);
        UnzipUtility uu = new UnzipUtility();
        uu.unzip(zipFilePath, destDirectory);
    } catch (IOException e) {
        e.printStackTrace();
    }
    String unzippedPath = destDirectory + File.separator + id + ".xml";
    String bioModelSBML = new String(Files.readAllBytes(Paths.get(unzippedPath)), StandardCharsets.UTF_8);
    // bioModelSBML = bioModelSBML.replace("<notanumber/>", "<ci> a </ci>");
    try {
        // the original zip file
        Files.deleteIfExists(Paths.get(zipFilePath));
        // the unzipped SBML file
        Files.deleteIfExists(Paths.get(unzippedPath));
        // its directory
        Files.deleteIfExists(Paths.get(destDirectory));
    // Files.deleteIfExists(Paths.get(prettyXML));			// the pretty xml
    } catch (IOException e) {
        e.printStackTrace();
    }
    ExternalDocInfo externalDocInfo = ExternalDocInfo.createBioModelsNetExternalDocInfo(bioModelSBML, name);
    return externalDocInfo;
}
Also used : Path(java.nio.file.Path) TreePath(javax.swing.tree.TreePath) InputStream(java.io.InputStream) ByteArrayOutputStream(java.io.ByteArrayOutputStream) IOException(java.io.IOException) URL(java.net.URL) FileNotFoundException(java.io.FileNotFoundException) DataConversionException(org.jdom.DataConversionException) IOException(java.io.IOException) ExternalDocInfo(cbit.vcell.xml.ExternalDocInfo) UnzipUtility(org.vcell.util.UnzipUtility) File(java.io.File)

Aggregations

ExternalDocInfo (cbit.vcell.xml.ExternalDocInfo)1 ByteArrayOutputStream (java.io.ByteArrayOutputStream)1 File (java.io.File)1 FileNotFoundException (java.io.FileNotFoundException)1 IOException (java.io.IOException)1 InputStream (java.io.InputStream)1 URL (java.net.URL)1 Path (java.nio.file.Path)1 TreePath (javax.swing.tree.TreePath)1 DataConversionException (org.jdom.DataConversionException)1 UnzipUtility (org.vcell.util.UnzipUtility)1