mirror of
https://github.com/GravitLauncher/Launcher
synced 2024-11-16 12:09:12 +03:00
36 lines
1.2 KiB
Java
36 lines
1.2 KiB
Java
|
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)));
|
||
|
}
|
||
|
}
|
||
|
}
|
||
|
}
|