Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add tarball extractor #1883

Merged
merged 4 commits into from
Aug 1, 2019
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
/*
* Copyright 2019 Google LLC.
*
* Licensed under the Apache License, Version 2.0 (the "License"); you may not
* use this file except in compliance with the License. You may obtain a copy of
* the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
* WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
* License for the specific language governing permissions and limitations under
* the License.
*/

package com.google.cloud.tools.jib.tar;

import java.io.BufferedInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.nio.file.Files;
import java.nio.file.Path;
import org.apache.commons.compress.archivers.tar.TarArchiveEntry;
import org.apache.commons.compress.archivers.tar.TarArchiveInputStream;

/** Extracts a tarball. */
public class TarExtractor {

/**
* Extracts a tarball to the specified destination.
*
* @param source the tarball to extract
* @param destination the output directory
* @throws IOException if extraction fails
*/
public static void extract(Path source, Path destination) throws IOException {
TadCordle marked this conversation as resolved.
Show resolved Hide resolved
try (InputStream in = new BufferedInputStream(Files.newInputStream(source));
TarArchiveInputStream tarArchiveInputStream = new TarArchiveInputStream(in)) {
byte[] buffer = new byte[1024];
TarArchiveEntry entry = tarArchiveInputStream.getNextTarEntry();
while (entry != null) {
if (entry.isDirectory()) {
Files.createDirectories(destination.resolve(entry.getName()));
TadCordle marked this conversation as resolved.
Show resolved Hide resolved
} else {
try (OutputStream out = Files.newOutputStream(destination.resolve(entry.getName()))) {
int read = tarArchiveInputStream.read(buffer);
while (read != -1) {
out.write(buffer, 0, read);
read = tarArchiveInputStream.read(buffer);
}
}
}
entry = tarArchiveInputStream.getNextTarEntry();
}
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
/*
* Copyright 2019 Google LLC.
*
* Licensed under the Apache License, Version 2.0 (the "License"); you may not
* use this file except in compliance with the License. You may obtain a copy of
* the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
* WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
* License for the specific language governing permissions and limitations under
* the License.
*/

package com.google.cloud.tools.jib.tar;

import com.google.common.io.Resources;
import java.io.IOException;
import java.net.URISyntaxException;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.util.stream.Collectors;
import java.util.stream.Stream;
import org.junit.Assert;
import org.junit.ClassRule;
import org.junit.Test;
import org.junit.rules.TemporaryFolder;

/** Tests for {@link TarExtractor}. */
public class TarExtractorTest {

@ClassRule public static TemporaryFolder temporaryFolder = new TemporaryFolder();
TadCordle marked this conversation as resolved.
Show resolved Hide resolved

@Test
public void testExtract() throws URISyntaxException, IOException {
Path source = Paths.get(Resources.getResource("core/extract.tar").toURI());
Path destination = temporaryFolder.getRoot().toPath();
TarExtractor.extract(source, destination);

Assert.assertTrue(Files.exists(destination.resolve("file A")));
Assert.assertTrue(Files.exists(destination.resolve("file B")));
Assert.assertTrue(
Files.exists(destination.resolve("folder").resolve("nested folder").resolve("file C")));

try (Stream<String> lines = Files.lines(destination.resolve("file A"))) {
String contents = lines.collect(Collectors.joining());
Assert.assertEquals("Hello", contents);
}
}
}
Binary file added jib-core/src/test/resources/core/extract.tar
Binary file not shown.