From 42b5d8f7f6c88f4cf6ee8c90697859d5ba740aee Mon Sep 17 00:00:00 2001 From: esoe Date: Wed, 9 Nov 2022 16:25:59 +0300 Subject: [PATCH] qq --- .../sourceListener/SourceListener.java | 97 +++++++++++++++++-- 1 file changed, 87 insertions(+), 10 deletions(-) diff --git a/src/main/java/ru/molokoin/sourceListener/SourceListener.java b/src/main/java/ru/molokoin/sourceListener/SourceListener.java index 52dddca..af97918 100644 --- a/src/main/java/ru/molokoin/sourceListener/SourceListener.java +++ b/src/main/java/ru/molokoin/sourceListener/SourceListener.java @@ -1,31 +1,108 @@ package ru.molokoin.sourceListener; +import java.io.BufferedInputStream; +import java.io.File; +import java.io.FileOutputStream; +import java.io.IOException; import java.net.URI; import java.net.http.HttpClient; import java.net.http.HttpRequest; import java.net.http.HttpResponse; +import java.nio.file.Path; +import java.nio.file.StandardOpenOption; +import java.util.Enumeration; +import java.util.zip.ZipEntry; +import java.util.zip.ZipFile; + public class SourceListener { + private URI link = URI.create("http://molokoin.ru:3000/esoe/molokoin-client/archive/master.zip"); + private Path downloadPath = Path.of(System.getProperty("user.dir")); - public static void main(String[] args) { + /** + * извлечение данных архива + */ + public void extract()throws IOException{ + String fileZip = "C:\\Users\\devuser\\Documents\\code\\sourceListener\\molokoin-client-master.zip"; + File destDir = new File("src/main/resources/unzip"); + + try (ZipFile file = new ZipFile(fileZip)) + { + Enumeration zipEntries = file.entries(); + while (zipEntries.hasMoreElements()){ + ZipEntry zipEntry = zipEntries.nextElement(); + File newFile = new File(destDir, zipEntry.getName()); + + //create sub directories + newFile.getParentFile().mkdirs(); + + if (!zipEntry.isDirectory()){ + try (FileOutputStream outputStream = new FileOutputStream(newFile)){ + BufferedInputStream inputStream = new BufferedInputStream(file.getInputStream(zipEntry)); + while (inputStream.available() > 0){ + outputStream.write(inputStream.read()); + } + inputStream.close(); + } + } + } + } + + } + /** + * скачиваем архив проекта из системы контроля версий, + * или иной файл с другого ресурса ... + */ + public void download(){ HttpClient client = HttpClient.newHttpClient(); HttpRequest request = HttpRequest.newBuilder() - .uri(URI.create("http://molokoin.ru:3000/esoe/molokoin-client")) - //.uri(URI.create("http://molokoin.ru:3000/esoe/molokoin-client/archive/master.zip")) - .GET() // GET is default - .build(); - + .uri(getLink()) + .build(); try { - HttpResponse response = client.send(request, - HttpResponse.BodyHandlers.ofString()); + HttpResponse response = client.send(request, + HttpResponse.BodyHandlers + .ofFileDownload(getDownloadPath(), StandardOpenOption.CREATE, StandardOpenOption.WRITE)); System.out.println(response.statusCode()); System.out.println(response.headers()); - System.out.println(response.body()); + Path path = response.body(); + System.out.println("Path = " + path); } catch (Exception e) { System.out.println(e.getMessage()); } - + } + /** + * @param link the link to set + */ + public void setLink(URI link) { + this.link = link; + } + /** + * @return the link + */ + public URI getLink() { + return link; + } + /** + * @param downloadPath the downloadPath to set + */ + public void setDownloadPath(Path downloadPath) { + this.downloadPath = downloadPath; + } + /** + * @return the downloadPath + */ + public Path getDownloadPath() { + return downloadPath; } + public static void main(String[] args) { + SourceListener ear = new SourceListener(); + ear.download(); + try { + ear.extract(); + } catch (IOException e) { + System.out.println(e.getMessage()); + } + } }