Search in sources :

Example 96 with TarArchiveInputStream

use of org.apache.commons.compress.archivers.tar.TarArchiveInputStream in project systemml by apache.

the class ValidateLicAndNotice method extractFileFromTGZ.

/**
 * This will return the file from tgz file and store it in specified location.
 *
 * @param	tgzFileName is the name of tgz file from which file to be extracted.
 * @param	fileName is the name of the file to be extracted.
 * @param	strDestLoc is the location where file will be extracted.
 * @param 	bFirstDirLevel to indicate to get file from first directory level.
 * @return	Sucess or Failure
 */
public static boolean extractFileFromTGZ(String tgzFileName, String fileName, String strDestLoc, boolean bFirstDirLevel) {
    boolean bRetCode = Constants.bFAILURE;
    TarArchiveInputStream tarIn = null;
    try {
        tarIn = new TarArchiveInputStream(new GzipCompressorInputStream(new BufferedInputStream(new FileInputStream(tgzFileName))));
    } catch (Exception e) {
        Utility.debugPrint(Constants.DEBUG_ERROR, "Exception in unzipping tar file: " + e);
        return bRetCode;
    }
    try {
        BufferedOutputStream bufOut = null;
        BufferedInputStream bufIn = null;
        TarArchiveEntry tarEntry = null;
        while ((tarEntry = tarIn.getNextTarEntry()) != null) {
            if (!tarEntry.getName().endsWith(fileName))
                continue;
            // Get file at root (in single directory) level. This is for License in root location.
            if (bFirstDirLevel && (tarEntry.getName().indexOf('/') != tarEntry.getName().lastIndexOf('/')))
                continue;
            bufIn = new BufferedInputStream(tarIn);
            int count;
            byte[] data = new byte[Constants.BUFFER];
            String strOutFileName = strDestLoc == null ? tarEntry.getName() : strDestLoc + "/" + fileName;
            FileOutputStream fos = new FileOutputStream(strOutFileName);
            bufOut = new BufferedOutputStream(fos, Constants.BUFFER);
            while ((count = bufIn.read(data, 0, Constants.BUFFER)) != -1) {
                bufOut.write(data, 0, count);
            }
            bufOut.flush();
            bufOut.close();
            bufIn.close();
            bRetCode = Constants.bSUCCESS;
            break;
        }
    } catch (Exception e) {
        e.printStackTrace();
    }
    return bRetCode;
}
Also used : TarArchiveInputStream(org.apache.commons.compress.archivers.tar.TarArchiveInputStream) GzipCompressorInputStream(org.apache.commons.compress.compressors.gzip.GzipCompressorInputStream) BufferedInputStream(java.io.BufferedInputStream) FileOutputStream(java.io.FileOutputStream) BufferedOutputStream(java.io.BufferedOutputStream) FileInputStream(java.io.FileInputStream) IOException(java.io.IOException) TarArchiveEntry(org.apache.commons.compress.archivers.tar.TarArchiveEntry)

Example 97 with TarArchiveInputStream

use of org.apache.commons.compress.archivers.tar.TarArchiveInputStream in project kontraktor by RuedigerMoeller.

the class JNPM method unTar.

private static List<File> unTar(final InputStream is, final File outputDir) throws FileNotFoundException, IOException, ArchiveException {
    final List<File> untaredFiles = new LinkedList<File>();
    final TarArchiveInputStream debInputStream = (TarArchiveInputStream) new ArchiveStreamFactory().createArchiveInputStream("tar", is);
    TarArchiveEntry entry = null;
    while ((entry = (TarArchiveEntry) debInputStream.getNextEntry()) != null) {
        String name = entry.getName();
        if (name.startsWith("package/"))
            name = name.substring("package/".length());
        final File outputFile = new File(outputDir, name);
        if (entry.isDirectory()) {
            if (!outputFile.mkdirs()) {
                throw new IllegalStateException(String.format("Couldn't create directory %s.", outputFile.getAbsolutePath()));
            }
        } else {
            outputFile.getParentFile().mkdirs();
            final OutputStream outputFileStream = new FileOutputStream(outputFile);
            IOUtils.copy(debInputStream, outputFileStream);
            outputFileStream.close();
        }
        untaredFiles.add(outputFile);
    }
    debInputStream.close();
    return untaredFiles;
}
Also used : TarArchiveInputStream(org.apache.commons.compress.archivers.tar.TarArchiveInputStream) ArchiveStreamFactory(org.apache.commons.compress.archivers.ArchiveStreamFactory) TarArchiveEntry(org.apache.commons.compress.archivers.tar.TarArchiveEntry)

Example 98 with TarArchiveInputStream

use of org.apache.commons.compress.archivers.tar.TarArchiveInputStream in project photon-model by vmware.

the class OvfRetriever method downloadIfOva.

/**
 * If ovaOrOvfUri points to a OVA it will be download locally and extracted. The uri is considered OVA if it is
 * in tar format and there is at least one .ovf inside.
 *
 * @param ovaOrOvfUri
 * @return the first .ovf file from the extracted tar of the input parameter if it's a local file or not a
 * tar archive
 */
public URI downloadIfOva(URI ovaOrOvfUri) throws IOException {
    if (ovaOrOvfUri.getScheme().equals("file")) {
        // local files are assumed to be ovfs
        return ovaOrOvfUri;
    }
    HttpGet get = new HttpGet(ovaOrOvfUri);
    HttpResponse check = null;
    logger.debug("Downloading ovf/ova from {}", ovaOrOvfUri);
    try {
        check = this.client.execute(get);
        byte[] buffer = new byte[TAR_MAGIC_OFFSET + TAR_MAGIC.length];
        int read = IOUtils.read(check.getEntity().getContent(), buffer);
        if (read != buffer.length) {
            // not a tar file, probably OVF, lets caller decide further
            return ovaOrOvfUri;
        }
        for (int i = 0; i < TAR_MAGIC.length; i++) {
            byte b = buffer[TAR_MAGIC_OFFSET + i];
            if (b != TAR_MAGIC[i] && b != TAR_MAGIC2[i]) {
                // magic numbers don't match
                logger.info("{} is not a tar file, assuming OVF", ovaOrOvfUri);
                return ovaOrOvfUri;
            }
        }
    } finally {
        get.abort();
        if (check != null) {
            EntityUtils.consumeQuietly(check.getEntity());
        }
    }
    // it's an OVA (at least a tar file), download to a local folder
    String folderName = hash(ovaOrOvfUri);
    File destination = new File(getBaseOvaExtractionDir(), folderName);
    if (new File(destination, MARKER_FILE).isFile()) {
        // marker file exists so the archive is already downloaded
        logger.info("Marker file for {} exists in {}, not downloading again", ovaOrOvfUri, destination);
        return findFirstOvfInFolder(destination);
    }
    destination.mkdirs();
    logger.info("Downloading OVA to {}", destination);
    get = new HttpGet(ovaOrOvfUri);
    HttpResponse response = null;
    try {
        response = this.client.execute(get);
        TarArchiveInputStream tarStream = new TarArchiveInputStream(response.getEntity().getContent());
        TarArchiveEntry entry;
        while ((entry = tarStream.getNextTarEntry()) != null) {
            extractEntry(tarStream, destination, entry);
        }
    } finally {
        if (response != null) {
            EntityUtils.consumeQuietly(response.getEntity());
        }
    }
    // store download progress
    writeMarkerFile(destination, ovaOrOvfUri);
    return findFirstOvfInFolder(destination);
}
Also used : TarArchiveInputStream(org.apache.commons.compress.archivers.tar.TarArchiveInputStream) HttpGet(org.apache.http.client.methods.HttpGet) HttpResponse(org.apache.http.HttpResponse) File(java.io.File) TarArchiveEntry(org.apache.commons.compress.archivers.tar.TarArchiveEntry)

Example 99 with TarArchiveInputStream

use of org.apache.commons.compress.archivers.tar.TarArchiveInputStream in project xodus by JetBrains.

the class CompressBackupUtilTest method testFolderArchived.

@Test
public void testFolderArchived() throws Exception {
    File src = new File(randName);
    src.mkdir();
    FileWriter fw = new FileWriter(new File(src, "1.txt"));
    fw.write("12345");
    fw.close();
    fw = new FileWriter(new File(src, "2.txt"));
    fw.write("12");
    fw.close();
    CompressBackupUtil.tar(src, dest);
    Assert.assertTrue("No destination archive created", dest.exists());
    TarArchiveInputStream tai = new TarArchiveInputStream(new GZIPInputStream(new BufferedInputStream(new FileInputStream(dest))));
    ArchiveEntry entry1 = tai.getNextEntry();
    ArchiveEntry entry2 = tai.getNextEntry();
    if (entry1.getName().compareTo(entry2.getName()) > 0) {
        // kinda sort them lol
        ArchiveEntry tmp = entry1;
        entry1 = entry2;
        entry2 = tmp;
    }
    Assert.assertNotNull("No entry found in destination archive", entry1);
    Assert.assertEquals("Entry has wrong size", 5, entry1.getSize());
    System.out.println(entry1.getName());
    Assert.assertEquals("Entry has wrong relative path", src.getName() + "/1.txt", entry1.getName());
    System.out.println(entry2.getName());
    Assert.assertEquals("Entry has wrong size", 2, entry2.getSize());
    Assert.assertEquals("Entry has wrong relative path", src.getName() + "/2.txt", entry2.getName());
}
Also used : TarArchiveInputStream(org.apache.commons.compress.archivers.tar.TarArchiveInputStream) GZIPInputStream(java.util.zip.GZIPInputStream) BufferedInputStream(java.io.BufferedInputStream) FileWriter(java.io.FileWriter) ArchiveEntry(org.apache.commons.compress.archivers.ArchiveEntry) File(java.io.File) FileInputStream(java.io.FileInputStream) Test(org.junit.Test)

Example 100 with TarArchiveInputStream

use of org.apache.commons.compress.archivers.tar.TarArchiveInputStream 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);
        }
    }
}
Also used : Path(org.apache.hadoop.fs.Path) TarArchiveInputStream(org.apache.commons.compress.archivers.tar.TarArchiveInputStream) GzipCompressorInputStream(org.apache.commons.compress.compressors.gzip.GzipCompressorInputStream) Configuration(org.apache.hadoop.conf.Configuration) FileSystem(org.apache.hadoop.fs.FileSystem) FSDataOutputStream(org.apache.hadoop.fs.FSDataOutputStream) TarArchiveEntry(org.apache.commons.compress.archivers.tar.TarArchiveEntry) Test(org.testng.annotations.Test)

Aggregations

TarArchiveInputStream (org.apache.commons.compress.archivers.tar.TarArchiveInputStream)132 TarArchiveEntry (org.apache.commons.compress.archivers.tar.TarArchiveEntry)99 File (java.io.File)52 IOException (java.io.IOException)50 FileInputStream (java.io.FileInputStream)46 GzipCompressorInputStream (org.apache.commons.compress.compressors.gzip.GzipCompressorInputStream)46 InputStream (java.io.InputStream)35 FileOutputStream (java.io.FileOutputStream)34 BufferedInputStream (java.io.BufferedInputStream)31 ByteArrayInputStream (java.io.ByteArrayInputStream)28 Test (org.junit.Test)23 ArrayList (java.util.ArrayList)20 GZIPInputStream (java.util.zip.GZIPInputStream)20 ByteArrayOutputStream (java.io.ByteArrayOutputStream)19 ArchiveEntry (org.apache.commons.compress.archivers.ArchiveEntry)17 OutputStream (java.io.OutputStream)16 Path (java.nio.file.Path)16 BufferedOutputStream (java.io.BufferedOutputStream)12 ArchiveStreamFactory (org.apache.commons.compress.archivers.ArchiveStreamFactory)12 TarArchiveOutputStream (org.apache.commons.compress.archivers.tar.TarArchiveOutputStream)8