Skip to content

Commit 49fdff3

Browse files
committed
Encapsulate Downloads unpacking helper methods
The sole public entry point is now Downloads#unpack.
1 parent e59b569 commit 49fdff3

File tree

2 files changed

+45
-9
lines changed

2 files changed

+45
-9
lines changed

src/main/java/org/apposed/appose/tool/Mamba.java

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -157,15 +157,12 @@ public Mamba(final String rootdir) {
157157

158158
@Override
159159
protected void decompress(final File archive) throws IOException, InterruptedException {
160-
final File tempTarFile = File.createTempFile("micromamba", ".tar");
161-
tempTarFile.deleteOnExit();
162-
Downloads.unBZip2(archive, tempTarFile);
163160
File mambaBaseDir = new File(rootdir);
164161
if (!mambaBaseDir.isDirectory() && !mambaBaseDir.mkdirs())
165162
throw new IOException("Failed to create Micromamba default directory " +
166163
mambaBaseDir.getParentFile().getAbsolutePath() +
167164
". Please try installing it in another directory.");
168-
Downloads.unTar(tempTarFile, mambaBaseDir);
165+
Downloads.unpack(archive, mambaBaseDir);
169166
File mmFile = new File(command);
170167
if (!mmFile.exists()) throw new IOException("Expected micromamba binary is missing: " + command);
171168
if (!mmFile.canExecute()) {

src/main/java/org/apposed/appose/util/Downloads.java

Lines changed: 44 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -112,7 +112,7 @@ public static File download(String name, String urlPath, @Nullable BiConsumer<Lo
112112
* @throws IOException if the source file already exists or there is any error with the decompression
113113
* @throws InterruptedException if the thread where the decompression is happening is interrupted
114114
*/
115-
public static void unBZip2(File source, File destination) throws FileNotFoundException, IOException, InterruptedException {
115+
private static void unBZip2(File source, File destination) throws FileNotFoundException, IOException, InterruptedException {
116116
try (
117117
BZip2CompressorInputStream input = new BZip2CompressorInputStream(new BufferedInputStream(new FileInputStream(source)));
118118
FileOutputStream output = new FileOutputStream(destination);
@@ -148,7 +148,7 @@ private static long copy(final InputStream input, final OutputStream output) thr
148148
public static void unpack(final File inputFile, final File outputDir) throws FileNotFoundException, IOException, InterruptedException {
149149
String filename = inputFile.getName().toLowerCase();
150150
if (filename.endsWith(".tar")) unTar(inputFile, outputDir);
151-
else if (filename.endsWith(".tar.bz2")) unBZip2(inputFile, outputDir);
151+
else if (filename.endsWith(".tar.bz2")) unTarBz2(inputFile, outputDir);
152152
else if (filename.endsWith(".tar.gz")) unTarGz(inputFile, outputDir);
153153
else if (filename.endsWith(".zip")) unZip(inputFile, outputDir);
154154
else throw new IllegalArgumentException("Unsupported archive type for file: " + inputFile.getName());
@@ -162,7 +162,7 @@ public static void unpack(final File inputFile, final File outputDir) throws Fil
162162
* @throws IOException if the source file already exists or there is any error with the decompression
163163
* @throws InterruptedException if the thread where the decompression is happening is interrupted
164164
*/
165-
public static void unZip(File source, File destination) throws FileNotFoundException, IOException, InterruptedException {
165+
private static void unZip(File source, File destination) throws FileNotFoundException, IOException, InterruptedException {
166166
try (
167167
InputStream is = new FileInputStream(source);
168168
ZipArchiveInputStream zipInputStream = new ZipArchiveInputStream(new BufferedInputStream(is));
@@ -203,7 +203,7 @@ public static void unZip(File source, File destination) throws FileNotFoundExcep
203203
* @throws IOException
204204
* @throws FileNotFoundException
205205
*/
206-
public static void unTar(final File inputFile, final File outputDir) throws FileNotFoundException, IOException, InterruptedException {
206+
private static void unTar(final File inputFile, final File outputDir) throws FileNotFoundException, IOException, InterruptedException {
207207

208208
try (
209209
InputStream is = new FileInputStream(inputFile);
@@ -245,7 +245,7 @@ public static void unTar(final File inputFile, final File outputDir) throws File
245245
* @throws FileNotFoundException if the file is not found
246246
* @throws InterruptedException if the thread is interrupted
247247
*/
248-
public static void unTarGz(final File inputFile, final File outputDir) throws FileNotFoundException, IOException, InterruptedException {
248+
private static void unTarGz(final File inputFile, final File outputDir) throws FileNotFoundException, IOException, InterruptedException {
249249
try (
250250
InputStream is = new FileInputStream(inputFile);
251251
InputStream gzipIs = new GzipCompressorInputStream(new BufferedInputStream(is));
@@ -275,6 +275,45 @@ public static void unTarGz(final File inputFile, final File outputDir) throws Fi
275275
throw new IOException(e);
276276
}
277277
}
278+
279+
/**
280+
* Decompress and untar a .tar.bz2 file into a directory.
281+
* @param inputFile the input .tar.bz2 file
282+
* @param outputDir the output directory file.
283+
* @throws FileNotFoundException if the .tar.bz2 file is not found or does not exist
284+
* @throws IOException if there is any error with the decompression
285+
* @throws InterruptedException if the thread where the decompression is happening is interrupted
286+
*/
287+
private static void unTarBz2(final File inputFile, final File outputDir) throws FileNotFoundException, IOException, InterruptedException {
288+
try (
289+
InputStream is = new FileInputStream(inputFile);
290+
InputStream bzip2Is = new BZip2CompressorInputStream(new BufferedInputStream(is));
291+
TarArchiveInputStream tarInputStream = (TarArchiveInputStream) new ArchiveStreamFactory().createArchiveInputStream("tar", bzip2Is);
292+
) {
293+
TarArchiveEntry entry = null;
294+
while ((entry = tarInputStream.getNextEntry()) != null) {
295+
final File outputFile = new File(outputDir, entry.getName());
296+
if (entry.isDirectory()) {
297+
if (!outputFile.exists()) {
298+
if (!outputFile.mkdirs()) {
299+
throw new IOException(String.format("Couldn't create directory %s.", outputFile.getAbsolutePath()));
300+
}
301+
}
302+
} else {
303+
if (!outputFile.getParentFile().exists()) {
304+
if (!outputFile.getParentFile().mkdirs())
305+
throw new IOException("Failed to create directory " + outputFile.getParentFile().getAbsolutePath());
306+
}
307+
try (OutputStream outputFileStream = new FileOutputStream(outputFile)) {
308+
copy(tarInputStream, outputFileStream);
309+
}
310+
}
311+
}
312+
}
313+
catch (ArchiveException e) {
314+
throw new IOException(e);
315+
}
316+
}
278317

279318
/**
280319
* This method should be used when we get the following response codes from

0 commit comments

Comments
 (0)