Skip to content

Commit

Permalink
minor improements to the download mangement
Browse files Browse the repository at this point in the history
  • Loading branch information
carlosuc3m committed Nov 19, 2024
1 parent 75386e5 commit 96e4ce9
Show file tree
Hide file tree
Showing 2 changed files with 79 additions and 0 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@
*/
package io.bioimage.modelrunner.engine.installation;

import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.net.HttpURLConnection;
Expand All @@ -37,6 +38,7 @@
import javax.net.ssl.X509TrustManager;

import io.bioimage.modelrunner.utils.CommonUtils;
import io.bioimage.modelrunner.utils.Constants;

public class FileDownloader {
private ReadableByteChannel rbc;
Expand Down Expand Up @@ -196,4 +198,18 @@ public static URL redirectedURL(URL url) throws MalformedURLException, URISyntax
conn.disconnect();
return redirectedURL(new URL(mainDomain + newURL));
}

/**
* Gets the filename of the file in an URL from the url String
* @param str
* the URL string
* @return the file name of the file in the URL
* @throws MalformedURLException if the String does not correspond to an URL
*/
public static String getFileNameFromURLString(String str) throws MalformedURLException {
if (str.startsWith(Constants.ZENODO_DOMAIN) && str.endsWith(Constants.ZENODO_ANNOYING_SUFFIX))
str = str.substring(0, str.length() - Constants.ZENODO_ANNOYING_SUFFIX.length());
URL url = new URL(str);
return new File(url.getPath()).getName();
}
}
63 changes: 63 additions & 0 deletions src/main/java/io/bioimage/modelrunner/utils/ZipUtils.java
Original file line number Diff line number Diff line change
Expand Up @@ -24,11 +24,14 @@
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.nio.file.Files;
import java.nio.file.Paths;
import java.util.Enumeration;
import java.util.function.Consumer;
import java.util.zip.ZipEntry;
import java.util.zip.ZipException;
import java.util.zip.ZipFile;
import java.util.zip.ZipInputStream;


Expand Down Expand Up @@ -115,4 +118,64 @@ public static void unzipFolder(String sourcePath, String targetPath, Consumer<Do
zis.close();
fis.close();
}

/**
* Calculate the uncompressed size of a .zip file
* @param zipFile
* the zip file of interest
* @return the size in bytes if the zip file was to be uncompressed
* @throws IOException if there is any error finding the size
*/
public static long getUncompressedSize(File zipFile) throws IOException {
return getUncompressedSize(zipFile, Thread.currentThread());
}

/**
* Calculate the uncompressed size of a .zip file
* @param zipFile
* the zip file of interest
* @param parentThread
* thread from where this operation is being launched
* @return the size in bytes if the zip file was to be uncompressed
* @throws IOException if there is any error finding the size
*/
public static long getUncompressedSize(File zipFile, Thread parentThread) throws IOException {
long totalSize = 0;

try (ZipFile zip = new ZipFile(zipFile)) {
Enumeration<? extends ZipEntry> entries = zip.entries();

while (entries.hasMoreElements() && !parentThread.isInterrupted()) {
ZipEntry entry = entries.nextElement();

// Skip directories
if (!entry.isDirectory()) {
long size = entry.getSize();

// If size is not known (-1), read the entry to determine its size
if (size == -1) {
size = calculateEntrySize(zip, entry, parentThread);
}

totalSize += size;
}
}
}

return totalSize;
}

private static long calculateEntrySize(ZipFile zip, ZipEntry entry, Thread parentThread) throws IOException {
long size = 0;
byte[] buffer = new byte[8192];

try (InputStream is = zip.getInputStream(entry)) {
int read;
while ((read = is.read(buffer)) != -1 && !parentThread.isInterrupted()) {
size += read;
}
}

return size;
}
}

0 comments on commit 96e4ce9

Please sign in to comment.