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;
}
Aggregations