|
| 1 | +package com.baeldung.logreg; |
| 2 | + |
| 3 | +import java.io.BufferedInputStream; |
| 4 | +import java.io.BufferedOutputStream; |
| 5 | +import java.io.File; |
| 6 | +import java.io.FileInputStream; |
| 7 | +import java.io.FileOutputStream; |
| 8 | +import java.io.IOException; |
| 9 | +import java.io.InputStream; |
| 10 | + |
| 11 | +import org.apache.commons.compress.archivers.ArchiveEntry; |
| 12 | +import org.apache.commons.compress.archivers.tar.TarArchiveEntry; |
| 13 | +import org.apache.commons.compress.archivers.tar.TarArchiveInputStream; |
| 14 | +import org.apache.commons.compress.compressors.gzip.GzipCompressorInputStream; |
| 15 | +import org.apache.http.HttpEntity; |
| 16 | +import org.apache.http.client.methods.CloseableHttpResponse; |
| 17 | +import org.apache.http.client.methods.HttpGet; |
| 18 | +import org.apache.http.impl.client.CloseableHttpClient; |
| 19 | +import org.apache.http.impl.client.HttpClientBuilder; |
| 20 | +import org.slf4j.Logger; |
| 21 | +import org.slf4j.LoggerFactory; |
| 22 | + |
| 23 | +/** |
| 24 | + * Utility class for digit classifier. |
| 25 | + * |
| 26 | + */ |
| 27 | +public class Utils { |
| 28 | + |
| 29 | + private static final Logger logger = LoggerFactory.getLogger(Utils.class); |
| 30 | + |
| 31 | + private Utils() { |
| 32 | + } |
| 33 | + |
| 34 | + /** |
| 35 | + * Download the content of the given url and save it into a file. |
| 36 | + * @param url |
| 37 | + * @param file |
| 38 | + */ |
| 39 | + public static void downloadAndSave(String url, File file) throws IOException { |
| 40 | + CloseableHttpClient client = HttpClientBuilder.create() |
| 41 | + .build(); |
| 42 | + logger.info("Connecting to {}", url); |
| 43 | + try (CloseableHttpResponse response = client.execute(new HttpGet(url))) { |
| 44 | + HttpEntity entity = response.getEntity(); |
| 45 | + if (entity != null) { |
| 46 | + logger.info("Downloaded {} bytes", entity.getContentLength()); |
| 47 | + try (FileOutputStream outstream = new FileOutputStream(file)) { |
| 48 | + logger.info("Saving to the local file"); |
| 49 | + entity.writeTo(outstream); |
| 50 | + outstream.flush(); |
| 51 | + logger.info("Local file saved"); |
| 52 | + } |
| 53 | + } |
| 54 | + } |
| 55 | + } |
| 56 | + |
| 57 | + /** |
| 58 | + * Extract a "tar.gz" file into a given folder. |
| 59 | + * @param file |
| 60 | + * @param folder |
| 61 | + */ |
| 62 | + public static void extractTarArchive(File file, String folder) throws IOException { |
| 63 | + logger.info("Extracting archive {} into folder {}", file.getName(), folder); |
| 64 | + // @formatter:off |
| 65 | + try (FileInputStream fis = new FileInputStream(file); |
| 66 | + BufferedInputStream bis = new BufferedInputStream(fis); |
| 67 | + GzipCompressorInputStream gzip = new GzipCompressorInputStream(bis); |
| 68 | + TarArchiveInputStream tar = new TarArchiveInputStream(gzip)) { |
| 69 | + // @formatter:on |
| 70 | + TarArchiveEntry entry; |
| 71 | + while ((entry = (TarArchiveEntry) tar.getNextEntry()) != null) { |
| 72 | + extractEntry(entry, tar, folder); |
| 73 | + } |
| 74 | + } |
| 75 | + logger.info("Archive extracted"); |
| 76 | + } |
| 77 | + |
| 78 | + /** |
| 79 | + * Extract an entry of the input stream into a given folder |
| 80 | + * @param entry |
| 81 | + * @param tar |
| 82 | + * @param folder |
| 83 | + * @throws IOException |
| 84 | + */ |
| 85 | + public static void extractEntry(ArchiveEntry entry, InputStream tar, String folder) throws IOException { |
| 86 | + final int bufferSize = 4096; |
| 87 | + final String path = folder + entry.getName(); |
| 88 | + if (entry.isDirectory()) { |
| 89 | + new File(path).mkdirs(); |
| 90 | + } else { |
| 91 | + int count; |
| 92 | + byte[] data = new byte[bufferSize]; |
| 93 | + // @formatter:off |
| 94 | + try (FileOutputStream os = new FileOutputStream(path); |
| 95 | + BufferedOutputStream dest = new BufferedOutputStream(os, bufferSize)) { |
| 96 | + // @formatter:off |
| 97 | + while ((count = tar.read(data, 0, bufferSize)) != -1) { |
| 98 | + dest.write(data, 0, count); |
| 99 | + } |
| 100 | + } |
| 101 | + } |
| 102 | + } |
| 103 | +} |
0 commit comments