Package org.apache.commons.compress.archivers.tar

Examples of org.apache.commons.compress.archivers.tar.TarArchiveInputStream


   */
  public static List<String> getAllPaths(InputStream in) throws IOException {
    Assert.notNull(in);

    List<String> paths = new ArrayList<String>();
    TarArchiveInputStream archiveIn = new TarArchiveInputStream(new GZIPInputStream(in));
    try {
      for (ArchiveEntry nextEntry = null; (nextEntry = archiveIn.getNextEntry()) != null;) {
        paths.add(nextEntry.getName());
      }
      return paths;
    } finally {
      StreamUtils.close(archiveIn);
View Full Code Here


  private TarFileUtils() {
    // inhibit instantiation
  }

  public static boolean hasGitFolder(InputStream inputStream) throws IOException {
    TarArchiveInputStream tarInputStream = null;
    try {
      boolean gitFolderPresent = false;
      tarInputStream = new TarArchiveInputStream(new GzipCompressorInputStream(inputStream));
      for (TarArchiveEntry entry = null; (entry = tarInputStream.getNextTarEntry()) != null;) {
        if (GIT_FOLDER_NAME.equals(entry.getName())
            && entry.isDirectory()) {
          gitFolderPresent = true;
          break;
        }
View Full Code Here

            if (Files.notExists(destDir)) {
                logger.trace("Create dir: {}", destDir);
                Files.createDirectories(destDir);
            }

            TarArchiveInputStream in = new TarArchiveInputStream(new GzipCompressorInputStream(Files.newInputStream(tgzFile)));

            TarArchiveEntry entry;
            while ((entry = in.getNextTarEntry()) != null) {
                if (entry.isDirectory()) {
                    Path dir = destDir.resolve(entry.getName());
                    logger.trace("Create dir {}", dir);
                    Files.createDirectories(dir);
                } else {
                    Path file = destDir.resolve(entry.getName());
                    logger.trace("Create file {}: {} bytes", file, entry.getSize());
                    OutputStream out = Files.newOutputStream(file);
                    IOUtils.copy(in, out);
                    out.close();
                }
            }

            in.close();
        } catch (IOException e) {
            throw new RuntimeIOException("Exception expanding " + tgzFile + " to " + destDir, e);
        }
    }
View Full Code Here

    }

    public void testCOMPRESS114() throws Exception {
        final File input = getFile("COMPRESS-114.tar");
        final InputStream is = new FileInputStream(input);
        final ArchiveInputStream in = new TarArchiveInputStream(is,
                CharsetNames.ISO_8859_1);
        TarArchiveEntry entry = (TarArchiveEntry)in.getNextEntry();
        assertEquals("3\u00b1\u00b1\u00b1F06\u00b1W2345\u00b1ZB\u00b1la\u00b1\u00b1\u00b1\u00b1\u00b1\u00b1\u00b1\u00b1BLA", entry.getName());
        entry = (TarArchiveEntry)in.getNextEntry();
        assertEquals("0302-0601-3\u00b1\u00b1\u00b1F06\u00b1W2345\u00b1ZB\u00b1la\u00b1\u00b1\u00b1\u00b1\u00b1\u00b1\u00b1\u00b1BLA",entry.getName());
        in.close();
    }
View Full Code Here

    public void testDirectoryEntryFromFile() throws Exception {
        File[] tmp = createTempDirAndFile();
        File archive = null;
        TarArchiveOutputStream tos = null;
        TarArchiveInputStream tis = null;
        try {
            archive = File.createTempFile("test.", ".tar", tmp[0]);
            archive.deleteOnExit();
            tos = new TarArchiveOutputStream(new FileOutputStream(archive));
            long beforeArchiveWrite = tmp[0].lastModified();
            TarArchiveEntry in = new TarArchiveEntry(tmp[0], "foo");
            tos.putArchiveEntry(in);
            tos.closeArchiveEntry();
            tos.close();
            tos = null;
            tis = new TarArchiveInputStream(new FileInputStream(archive));
            TarArchiveEntry out = tis.getNextTarEntry();
            tis.close();
            tis = null;
            assertNotNull(out);
            assertEquals("foo/", out.getName());
            assertEquals(0, out.getSize());
            // TAR stores time with a granularity of 1 second
            assertEquals(beforeArchiveWrite / 1000,
                         out.getLastModifiedDate().getTime() / 1000);
            assertTrue(out.isDirectory());
        } finally {
            if (tis != null) {
                tis.close();
            }
            if (tos != null) {
                tos.close();
            }
            tryHardToDelete(archive);
View Full Code Here

    public void testExplicitDirectoryEntry() throws Exception {
        File[] tmp = createTempDirAndFile();
        File archive = null;
        TarArchiveOutputStream tos = null;
        TarArchiveInputStream tis = null;
        try {
            archive = File.createTempFile("test.", ".tar", tmp[0]);
            archive.deleteOnExit();
            tos = new TarArchiveOutputStream(new FileOutputStream(archive));
            long beforeArchiveWrite = tmp[0].lastModified();
            TarArchiveEntry in = new TarArchiveEntry("foo/");
            in.setModTime(beforeArchiveWrite);
            tos.putArchiveEntry(in);
            tos.closeArchiveEntry();
            tos.close();
            tos = null;
            tis = new TarArchiveInputStream(new FileInputStream(archive));
            TarArchiveEntry out = tis.getNextTarEntry();
            tis.close();
            tis = null;
            assertNotNull(out);
            assertEquals("foo/", out.getName());
            assertEquals(0, out.getSize());
            assertEquals(beforeArchiveWrite / 1000,
                         out.getLastModifiedDate().getTime() / 1000);
            assertTrue(out.isDirectory());
        } finally {
            if (tis != null) {
                tis.close();
            }
            if (tos != null) {
                tos.close();
            }
            tryHardToDelete(archive);
View Full Code Here

    public void testFileEntryFromFile() throws Exception {
        File[] tmp = createTempDirAndFile();
        File archive = null;
        TarArchiveOutputStream tos = null;
        TarArchiveInputStream tis = null;
        FileInputStream fis = null;
        try {
            archive = File.createTempFile("test.", ".tar", tmp[0]);
            archive.deleteOnExit();
            tos = new TarArchiveOutputStream(new FileOutputStream(archive));
            TarArchiveEntry in = new TarArchiveEntry(tmp[1], "foo");
            tos.putArchiveEntry(in);
            byte[] b = new byte[(int) tmp[1].length()];
            fis = new FileInputStream(tmp[1]);
            while (fis.read(b) > 0) {
                tos.write(b);
            }
            fis.close();
            fis = null;
            tos.closeArchiveEntry();
            tos.close();
            tos = null;
            tis = new TarArchiveInputStream(new FileInputStream(archive));
            TarArchiveEntry out = tis.getNextTarEntry();
            tis.close();
            tis = null;
            assertNotNull(out);
            assertEquals("foo", out.getName());
            assertEquals(tmp[1].length(), out.getSize());
            assertEquals(tmp[1].lastModified() / 1000,
                         out.getLastModifiedDate().getTime() / 1000);
            assertFalse(out.isDirectory());
        } finally {
            if (tis != null) {
                tis.close();
            }
            if (tos != null) {
                tos.close();
            }
            tryHardToDelete(archive);
View Full Code Here

    public void testExplicitFileEntry() throws Exception {
        File[] tmp = createTempDirAndFile();
        File archive = null;
        TarArchiveOutputStream tos = null;
        TarArchiveInputStream tis = null;
        FileInputStream fis = null;
        try {
            archive = File.createTempFile("test.", ".tar", tmp[0]);
            archive.deleteOnExit();
            tos = new TarArchiveOutputStream(new FileOutputStream(archive));
            TarArchiveEntry in = new TarArchiveEntry("foo");
            in.setModTime(tmp[1].lastModified());
            in.setSize(tmp[1].length());
            tos.putArchiveEntry(in);
            byte[] b = new byte[(int) tmp[1].length()];
            fis = new FileInputStream(tmp[1]);
            while (fis.read(b) > 0) {
                tos.write(b);
            }
            fis.close();
            fis = null;
            tos.closeArchiveEntry();
            tos.close();
            tos = null;
            tis = new TarArchiveInputStream(new FileInputStream(archive));
            TarArchiveEntry out = tis.getNextTarEntry();
            tis.close();
            tis = null;
            assertNotNull(out);
            assertEquals("foo", out.getName());
            assertEquals(tmp[1].length(), out.getSize());
            assertEquals(tmp[1].lastModified() / 1000,
                         out.getLastModifiedDate().getTime() / 1000);
            assertFalse(out.isDirectory());
        } finally {
            if (tis != null) {
                tis.close();
            }
            if (tos != null) {
                tos.close();
            }
            tryHardToDelete(archive);
View Full Code Here

        final String newPath = "tar:" + slash1 + path + request.getFileName() + "!" + slash;
        slash1 = "";
        InputStream tarIn = null;
        try {
            tarIn = request.getSourceInputStream();
            final TarArchiveInputStream in = new TarArchiveInputStream(tarIn);
            try {
                TarArchiveEntry entry = null;
                while ((entry = (TarArchiveEntry) in.getNextTarEntry()) != null) {
                    String name = entry.getName();
                    if (!entry.isDirectory()) {
                        final RequestMetaData metaData = new RequestMetaData(1L, 2L, name);
                        final RequestIdentifier identifier = new RequestIdentifier(uri);
                        final TarEntryIdentificationRequest tarRequest =
                            new TarEntryIdentificationRequest(metaData, identifier, tmpDir);
                        tarRequest.open(in);
                        final IdentificationResultCollection tarResults =
                                binarySignatureIdentifier.matchBinarySignatures(tarRequest);
                        final ResultPrinter resultPrinter =
                                new ResultPrinter(binarySignatureIdentifier,
                                    containerSignatureDefinitions, newPath, slash, slash1, true);
                        resultPrinter.print(tarResults, tarRequest);
                    }
                }
            } finally {
                if (in != null) {
                    in.close();
                }
            }
        } catch (IOException ioe) {
            System.err.println(ioe + " (" + newPath + ")"); // continue after corrupt archive
        } finally {
View Full Code Here

            if (Files.notExists(destDir)) {
                logger.trace("Create dir: {}", destDir);
                Files.createDirectories(destDir);
            }

            TarArchiveInputStream in = new TarArchiveInputStream(new GzipCompressorInputStream(Files.newInputStream(tgzFile)));

            TarArchiveEntry entry;
            while ((entry = in.getNextTarEntry()) != null) {
                if (entry.isDirectory()) {
                    Path dir = destDir.resolve(entry.getName());
                    logger.trace("Create dir {}", dir);
                    Files.createDirectories(dir);
                } else {
                    Path file = destDir.resolve(entry.getName());
                    logger.trace("Create file {}: {} bytes", file, entry.getSize());
                    OutputStream out = Files.newOutputStream(file);
                    IOUtils.copy(in, out);
                    out.close();
                }
            }

            in.close();
        } catch (IOException e) {
            throw new RuntimeIOException("Exception expanding " + tgzFile + " to " + destDir, e);
        }
    }
View Full Code Here

TOP

Related Classes of org.apache.commons.compress.archivers.tar.TarArchiveInputStream

Copyright © 2018 www.massapicom. All rights reserved.
All source code are property of their respective owners. Java is a trademark of Sun Microsystems, Inc and owned by ORACLE Inc. Contact coftware#gmail.com.