use of org.apache.commons.compress.compressors.gzip.GzipCompressorInputStream in project frontend-maven-plugin by eirslett.
the class DefaultArchiveExtractor method extract.
@Override
public void extract(String archive, String destinationDirectory) throws ArchiveExtractionException {
final File archiveFile = new File(archive);
try (FileInputStream fis = new FileInputStream(archiveFile)) {
if ("msi".equals(FileUtils.getExtension(archiveFile.getAbsolutePath()))) {
String command = "msiexec /a " + archiveFile.getAbsolutePath() + " /qn TARGETDIR=\"" + destinationDirectory + "\"";
Process child = Runtime.getRuntime().exec(command);
try {
int result = child.waitFor();
if (result != 0) {
throw new ArchiveExtractionException("Could not extract " + archiveFile.getAbsolutePath() + "; return code " + result);
}
} catch (InterruptedException e) {
throw new ArchiveExtractionException("Unexpected interruption of while waiting for extraction process", e);
}
} else if ("zip".equals(FileUtils.getExtension(archiveFile.getAbsolutePath()))) {
ZipFile zipFile = new ZipFile(archiveFile);
try {
Enumeration<? extends ZipEntry> entries = zipFile.entries();
while (entries.hasMoreElements()) {
ZipEntry entry = entries.nextElement();
final File destPath = new File(destinationDirectory + File.separator + entry.getName());
prepDestination(destPath, entry.isDirectory());
if (!entry.isDirectory()) {
InputStream in = null;
OutputStream out = null;
try {
in = zipFile.getInputStream(entry);
out = new FileOutputStream(destPath);
IOUtils.copy(in, out);
} finally {
IOUtils.closeQuietly(in);
IOUtils.closeQuietly(out);
}
}
}
} finally {
zipFile.close();
}
} else {
// TarArchiveInputStream can be constructed with a normal FileInputStream if
// we ever need to extract regular '.tar' files.
TarArchiveInputStream tarIn = null;
try {
tarIn = new TarArchiveInputStream(new GzipCompressorInputStream(fis));
TarArchiveEntry tarEntry = tarIn.getNextTarEntry();
String canonicalDestinationDirectory = new File(destinationDirectory).getCanonicalPath();
while (tarEntry != null) {
// Create a file for this tarEntry
final File destPath = new File(destinationDirectory + File.separator + tarEntry.getName());
prepDestination(destPath, tarEntry.isDirectory());
if (!startsWithPath(destPath.getCanonicalPath(), canonicalDestinationDirectory)) {
throw new IOException("Expanding " + tarEntry.getName() + " would create file outside of " + canonicalDestinationDirectory);
}
if (!tarEntry.isDirectory()) {
destPath.createNewFile();
boolean isExecutable = (tarEntry.getMode() & 0100) > 0;
destPath.setExecutable(isExecutable);
OutputStream out = null;
try {
out = new FileOutputStream(destPath);
IOUtils.copy(tarIn, out);
} finally {
IOUtils.closeQuietly(out);
}
}
tarEntry = tarIn.getNextTarEntry();
}
} finally {
IOUtils.closeQuietly(tarIn);
}
}
} catch (IOException e) {
throw new ArchiveExtractionException("Could not extract archive: '" + archive + "'", e);
}
}
use of org.apache.commons.compress.compressors.gzip.GzipCompressorInputStream in project gerrit by GerritCodeReview.
the class GetArchiveIT method getTgzArchive.
@Test
public void getTgzArchive() throws Exception {
BinaryResult res = gApi.changes().id(changeId).current().getArchive(ArchiveFormat.TGZ);
assertThat(res.getAttachmentName()).isEqualTo(commit.abbreviate(ObjectIds.ABBREV_STR_LEN).name() + ".tar.gz");
assertThat(res.getContentType()).isEqualTo("application/x-gzip");
assertThat(res.canGzip()).isFalse();
byte[] archiveBytes = getBinaryContent(res);
try (ByteArrayInputStream in = new ByteArrayInputStream(archiveBytes);
GzipCompressorInputStream gzipIn = new GzipCompressorInputStream(in)) {
HashMap<String, String> archiveEntries = getTarContent(gzipIn);
assertThat(archiveEntries).containsExactly(DIRECTORY_NAME + "/", null, FILE_NAME, FILE_CONTENT);
}
}
use of org.apache.commons.compress.compressors.gzip.GzipCompressorInputStream in project twister2 by DSC-SPIDAL.
the class TarGzipPacker method unpack.
/**
* unpackage the given tar.gz file to the provided output directory
*/
public static boolean unpack(final Path sourceGzip, Path outputDir) {
GzipCompressorInputStream gzIn = null;
TarArchiveInputStream tarInputStream = null;
try {
// construct input stream
InputStream fin = Files.newInputStream(sourceGzip);
BufferedInputStream in = new BufferedInputStream(fin);
gzIn = new GzipCompressorInputStream(in);
tarInputStream = new TarArchiveInputStream(gzIn);
TarArchiveEntry entry = null;
while ((entry = (TarArchiveEntry) tarInputStream.getNextEntry()) != null) {
File outputFile = new File(outputDir.toFile(), entry.getName());
if (!outputFile.getParentFile().exists()) {
boolean dirCreated = outputFile.getParentFile().mkdirs();
if (!dirCreated) {
LOG.severe("Can not create the output directory: " + outputFile.getParentFile() + "\nFile unpack is unsuccessful.");
return false;
}
}
if (!outputFile.isDirectory()) {
final OutputStream outputFileStream = new FileOutputStream(outputFile);
IOUtils.copy(tarInputStream, outputFileStream);
outputFileStream.close();
// if the file has sh extension, make it executable
if (entry.getName().endsWith(".sh")) {
outputFile.setExecutable(true);
}
}
}
tarInputStream.close();
gzIn.close();
return true;
} catch (IOException e) {
LOG.log(Level.SEVERE, "Exception when unpacking job package. ", e);
return false;
}
}
use of org.apache.commons.compress.compressors.gzip.GzipCompressorInputStream in project incubator-gobblin by apache.
the class StreamUtilsTest method testTarDir.
@Test
public void testTarDir() throws IOException {
FileSystem localFs = FileSystem.getLocal(new Configuration());
// Set of expected Paths to be in the resulting tar file
Set<Path> expectedPaths = Sets.newHashSet();
// Create input directory
Path testInDir = new Path("testDir");
expectedPaths.add(testInDir);
// Create output file path
Path testOutFile = new Path("testTarOut" + UUID.randomUUID() + ".tar.gz");
try {
localFs.mkdirs(testInDir);
// Create a test file path
Path testFile1 = new Path(testInDir, "testFile1");
expectedPaths.add(testFile1);
FSDataOutputStream testFileOut1 = localFs.create(testFile1);
testFileOut1.close();
// Create a test file path
Path testFile2 = new Path(testInDir, "testFile2");
expectedPaths.add(testFile2);
FSDataOutputStream testFileOut2 = localFs.create(testFile2);
testFileOut2.close();
// tar the input directory to the specific output file
StreamUtils.tar(localFs, testInDir, testOutFile);
// Confirm the contents of the tar file are valid
try (TarArchiveInputStream tarArchiveInputStream = new TarArchiveInputStream(new GzipCompressorInputStream(localFs.open(testOutFile)))) {
TarArchiveEntry tarArchiveEntry;
while (null != (tarArchiveEntry = tarArchiveInputStream.getNextTarEntry())) {
assertThat(new Path(tarArchiveEntry.getName()), isIn(expectedPaths));
}
}
} finally {
if (localFs.exists(testInDir)) {
localFs.delete(testInDir, true);
}
if (localFs.exists(testOutFile)) {
localFs.delete(testOutFile, true);
}
}
}
use of org.apache.commons.compress.compressors.gzip.GzipCompressorInputStream in project incubator-gobblin by apache.
the class StreamUtilsTest method testTarFile.
@Test
public void testTarFile() throws IOException {
FileSystem localFs = FileSystem.getLocal(new Configuration());
// Set of expected Paths to be in the resulting tar file
Set<Path> expectedPaths = Sets.newHashSet();
// Create input file path
Path testFile = new Path("testFile");
expectedPaths.add(testFile);
// Create output file path
Path testOutFile = new Path("testTarOut" + UUID.randomUUID() + ".tar.gz");
try {
// Create the input file
FSDataOutputStream testFileOut1 = localFs.create(testFile);
testFileOut1.close();
// tar the input file to the specific output file
StreamUtils.tar(localFs, testFile, testOutFile);
// Confirm the contents of the tar file are valid
try (TarArchiveInputStream tarArchiveInputStream = new TarArchiveInputStream(new GzipCompressorInputStream(localFs.open(testOutFile)))) {
TarArchiveEntry tarArchiveEntry;
while (null != (tarArchiveEntry = tarArchiveInputStream.getNextTarEntry())) {
MatcherAssert.assertThat(new Path(tarArchiveEntry.getName()), Matchers.isIn(expectedPaths));
}
}
} finally {
if (localFs.exists(testFile)) {
localFs.delete(testFile, true);
}
if (localFs.exists(testOutFile)) {
localFs.delete(testOutFile, true);
}
}
}
Aggregations