esoe
2 years ago
1 changed files with 87 additions and 10 deletions
@ -1,31 +1,108 @@
@@ -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<? extends ZipEntry> 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<String> response = client.send(request, |
||||
HttpResponse.BodyHandlers.ofString()); |
||||
HttpResponse<Path> 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()); |
||||
} |
||||
} |
||||
} |
||||
|
Loading…
Reference in new issue