Launcher/libLauncher/src/main/java/ru/gravit/utils/HttpDownloader.java

36 lines
1.2 KiB
Java
Raw Normal View History

2018-09-19 17:37:12 +03:00
package ru.gravit.utils;
import ru.gravit.utils.helper.IOHelper;
import ru.gravit.utils.helper.LogHelper;
import java.io.FileOutputStream;
import java.io.IOException;
import java.net.URL;
import java.nio.channels.Channels;
import java.nio.channels.ReadableByteChannel;
import java.nio.file.Path;
import java.util.zip.ZipEntry;
import java.util.zip.ZipInputStream;
public class HttpDownloader {
public static void downloadFile(URL url, String file) throws IOException {
ReadableByteChannel rbc = Channels.newChannel(url.openStream());
try (FileOutputStream fos = new FileOutputStream(file)) {
fos.getChannel().transferFrom(rbc, 0, Long.MAX_VALUE);
}
}
public static void downloadZip(URL url, Path dir) throws IOException {
try (ZipInputStream input = IOHelper.newZipInput(url)) {
for (ZipEntry entry = input.getNextEntry(); entry != null; entry = input.getNextEntry()) {
if (entry.isDirectory())
continue; // Skip directories
// Unpack entry
String name = entry.getName();
LogHelper.subInfo("Downloading file: '%s'", name);
IOHelper.transfer(input, dir.resolve(IOHelper.toPath(name)));
}
}
}
}