Skip to content
Snippets Groups Projects
Commit 3c8764fe authored by jez04's avatar jez04
Browse files

feat: add download script

parent 379fb347
No related merge requests found
import java.io.BufferedOutputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.net.MalformedURLException;
import java.net.URL;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.nio.file.StandardCopyOption;
import java.util.List;
import java.util.logging.Level;
import java.util.logging.Logger;
import java.util.zip.ZipEntry;
import java.util.zip.ZipInputStream;
public class Download {
private static Logger log = java.util.logging.Logger.getLogger(Download.class.getName());
public static final List<String> skipNames = List.of("target", ".settings", ".project", ".classpath");
public static final String TAR_GZ_PATTERN = ".*\\.tar\\.gz";
public static void main(String[] args) {
try {
if (args.length < 2) {
printUsage();
} else {
boolean unzip = false;
int cutoff = 0;
String url = null;
String destPath = null;
for (String arg : args) {
if ("-unzip".equalsIgnoreCase(arg)) {
unzip = true;
} else if (arg.toLowerCase().startsWith("-cutoff:")) {
String[] parts = arg.split(":");
if (parts.length > 1) {
try {
cutoff = Integer.parseInt(parts[1]);
} catch (Exception e) {
log.log(Level.WARNING, "Cannot cunvert cutoff count into integer.", e);
}
}
} else if (url == null) {
url = arg;
} else if (destPath == null) {
destPath = arg;
} else {
log.log(Level.WARNING, "Uknown argument '{}'.", arg);
}
}
if (url != null && destPath != null) {
download(url, Paths.get(destPath), unzip, cutoff);
} else {
printUsage();
}
}
} catch (Exception e) {
e.printStackTrace();
}
}
private static void printUsage() {
log.info(
"Usage:\n java [--source 21 --enable-preview] FixDirStructure.java [-unzip] [-cutoff:n] <url> <destFile> ");
}
private static void download(String urlString, Path destPath, boolean unzip, int cutoffParrent) {
try (InputStream inputStream = new URL(urlString).openStream()) {
if (unzip) {
destPath.toFile().mkdirs();
extractZipStream(inputStream, destPath, cutoffParrent);
} else {
Path parent = destPath.getParent();
if(parent != null) {
parent.toFile().mkdirs();
}
Files.copy(inputStream, destPath, StandardCopyOption.REPLACE_EXISTING);
}
} catch (MalformedURLException ex) {
log.log(Level.SEVERE, "Cannot connect to git", ex);
} catch (IOException ex) {
log.log(Level.SEVERE, "Cannot douwnload and extract multi project evaluator", ex);
}
}
private static final int BUFFER_SIZE = 4096;
/**
* Extracts a zip file specified by the zipFilePath to a directory specified by
* destDirectory (will be created if does not exists)
*
* @param zipFilePath
* @throws IOException
*/
public static void unzip(File zipFilePath, Path destDir) throws IOException {
if (!destDir.toFile().exists()) {
destDir.toFile().mkdir();
}
try (InputStream in = new FileInputStream(zipFilePath)) {
extractZipStream(in, destDir);
}
}
private static void extractZipStream(InputStream zipInputStream, Path destDir) throws IOException {
extractZipStream(zipInputStream, destDir, 0);
}
private static void extractZipStream(InputStream zipInputStream, Path destDir, int cutoffParent)
throws FileNotFoundException, IOException {
ZipInputStream zipIn = new ZipInputStream(zipInputStream);
ZipEntry entry = zipIn.getNextEntry();
// iterates over entries in the zip file
while (entry != null) {
Path entryPath = Paths.get(entry.getName());
if (entry.isDirectory() && entryPath.getNameCount() <= cutoffParent) {
entryPath = Paths.get("");
} else if (!entry.isDirectory() && entryPath.getNameCount() <= cutoffParent) {
entryPath = entryPath.getFileName();
} else {
entryPath = entryPath.subpath(cutoffParent, entryPath.getNameCount());
}
Path filePath = destDir.resolve(entryPath);
if (!entry.isDirectory()) {
// if the entry is a file, extracts it
extractFile(zipIn, filePath);
} else {
// if the entry is a directory, make the directory
File dir = filePath.toFile();
dir.mkdirs();
}
zipIn.closeEntry();
entry = zipIn.getNextEntry();
}
zipIn.close();
}
/**
* Extracts a zip entry (file entry)
*
* @param zipIn
* @param filePath
* @throws IOException
*/
private static void extractFile(ZipInputStream zipIn, Path filePath) throws IOException {
try (BufferedOutputStream bos = new BufferedOutputStream(new FileOutputStream(filePath.toFile()))) {
byte[] bytesIn = new byte[BUFFER_SIZE];
int read = 0;
while ((read = zipIn.read(bytesIn)) != -1) {
bos.write(bytesIn, 0, read);
}
}
}
}
......@@ -57,7 +57,7 @@ import org.xml.sax.SAXException;
public class FixDirStructure {
private static Logger log = java.util.logging.Logger.getLogger(FixDirStructure.class.getName());
public static final List<String> skipNames = List.of("target", ".settings", ".project", ".classpath");
public static final List<String> skipNames = List.of("target", ".settings", ".project", ".classpath", ".kelvin-utils");
public static final String TAR_GZ_PATTERN = ".*\\.tar\\.gz";
public static void main(String[] args) {
......
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment