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

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


            in.mark(tarheader.length);
            signatureLength = in.read(tarheader);
            in.reset();
            if (TarArchiveInputStream.matches(tarheader, signatureLength)) {
                if (entryEncoding != null) {
                    return new TarArchiveInputStream(in, entryEncoding);
                } else {
                    return new TarArchiveInputStream(in);
                }
            }
            // COMPRESS-117 - improve auto-recognition
            if (signatureLength >= 512) {
                TarArchiveInputStream tais = null;
                try {
                    tais = new TarArchiveInputStream(new ByteArrayInputStream(tarheader));
                    // COMPRESS-191 - verify the header checksum
                    if (tais.getNextTarEntry().isCheckSumOK()) {
                        return new TarArchiveInputStream(in);
                    }
                } catch (Exception e) { // NOPMD
                    // can generate IllegalArgumentException as well
                    // as IOException
                    // autodetection, simply not a TAR
                    // ignored
                } finally {
                    if (tais != null) {
                        try {
                            tais.close();
                        } catch (IOException ignored) {
                            // ignored
                        }
                    }
                }
View Full Code Here


    private void extractTarGzDistribution(URL sourceDistribution, File targetFolder) throws IOException,
        FileNotFoundException {
        File uncompressedFile = File.createTempFile("uncompressedTarGz-", ".tar");
        extractGzArchive(sourceDistribution.openStream(), uncompressedFile);
        extract(new TarArchiveInputStream(new FileInputStream(uncompressedFile)), targetFolder);
        FileUtils.forceDelete(uncompressedFile);
    }
View Full Code Here

  }

  private void unzipFile(Location libZip) throws IOException, ArchiveException {
    String path = libZip.toURI().getPath();
    String outDir = Locations.getParent(libZip).toURI().getPath();
    TarArchiveInputStream archiveInputStream = new TarArchiveInputStream(
      new GZIPInputStream(new FileInputStream(path)));
    try {
      TarArchiveEntry entry = archiveInputStream.getNextTarEntry();
      while (entry != null) {
        File destFile = new File(outDir, entry.getName());
        destFile.getParentFile().mkdirs();
        if (!entry.isDirectory()) {
          ByteStreams.copy(archiveInputStream, Files.newOutputStreamSupplier(destFile));
          //TODO: Set executable permission based on entry.getMode()
          destFile.setExecutable(true, false);
        }
        entry = archiveInputStream.getNextTarEntry();
      }
    } finally {
      archiveInputStream.close();
    }
  }
View Full Code Here

    Runtime.getRuntime().exec(args);
    LOGGER.info("Set execute permissions on " + file);
  }

  private void untarTarFile(File tarFile, File destDir) throws Exception {
    TarArchiveInputStream tarInputStream = null;
    try {
      tarInputStream = new TarArchiveInputStream(new FileInputStream(tarFile));
      TarArchiveEntry entry = null;
      while ((entry = tarInputStream.getNextTarEntry()) != null) {
        String name = entry.getName();
        LOGGER.debug("Next file: " + name);
        File destFile = new File(destDir, entry.getName());
        if (entry.isDirectory()) {
          destFile.mkdirs();
          continue;
        }
        File destParent = destFile.getParentFile();
        destParent.mkdirs();
        OutputStream entryOutputStream = null;
        try {
          entryOutputStream = new FileOutputStream(destFile);
          byte[] buffer = new byte[2048];
          int length = 0;
          while ((length = tarInputStream.read(buffer, 0, 2048)) != -1) {
            entryOutputStream.write(buffer, 0, length);
          }
        } catch (Exception ex) {
          LOGGER.error("Exception while expanding tar file", ex);
          throw ex;
        } finally {
          if (entryOutputStream != null) {
            try {
              entryOutputStream.close();
            } catch (Exception ex) {
              LOGGER.warn("Failed to close entry output stream", ex);
            }
          }
        }
      }
    } catch (Exception ex) {
      LOGGER.error("Exception caught while untarring tar file: "
            + tarFile.getAbsolutePath(), ex);
      throw ex;
    } finally {
      if (tarInputStream != null) {
        try {
          tarInputStream.close();
        } catch (Exception ex) {
          LOGGER.warn("Unable to close tar input stream: "
                + tarFile.getCanonicalPath(), ex);
        }
      }
View Full Code Here

public class ChainingTestCase extends AbstractTestCase {

    public void testTarGzip() throws Exception {
        File file = getFile("bla.tgz");
        final TarArchiveInputStream is = new TarArchiveInputStream(new GzipCompressorInputStream(new FileInputStream(file)));
        final TarArchiveEntry entry = (TarArchiveEntry)is.getNextEntry();
        assertNotNull(entry);
        assertEquals("test1.xml", entry.getName());
        is.close();
    }
View Full Code Here

        is.close();
    }

    public void testTarBzip2() throws Exception {
        File file = getFile("bla.tar.bz2");
        final TarArchiveInputStream is = new TarArchiveInputStream(new BZip2CompressorInputStream(new FileInputStream(file)));
        final TarArchiveEntry entry = (TarArchiveEntry)is.getNextEntry();
        assertNotNull(entry);
        assertEquals("test1.xml", entry.getName());
        is.close();
    }
View Full Code Here

                is = new BZip2CompressorInputStream(is);
            } else {
                throw new IllegalStateException("Unsupported compression format " + archiveFormat + "!. "
                                                + "Please report this to stanbol-dev mailing list!");
            }
            ais = new TarArchiveInputStream(is);
        }
        return ais;
    }
View Full Code Here

        }
        if (ZIP.equalsIgnoreCase(archiverName)) {
            return new ZipArchiveInputStream(in);
        }
        if (TAR.equalsIgnoreCase(archiverName)) {
            return new TarArchiveInputStream(in);
        }
        if (JAR.equalsIgnoreCase(archiverName)) {
            return new JarArchiveInputStream(in);
        }
        if (CPIO.equalsIgnoreCase(archiverName)) {
View Full Code Here

            final byte[] tarheader = new byte[512];
            in.mark(tarheader.length);
            signatureLength = in.read(tarheader);
            in.reset();
            if (TarArchiveInputStream.matches(tarheader, signatureLength)) {
                return new TarArchiveInputStream(in);
            }
            // COMPRESS-117 - improve auto-recognition
            try {
                TarArchiveInputStream tais = new TarArchiveInputStream(new ByteArrayInputStream(tarheader));
                tais.getNextEntry();
                return new TarArchiveInputStream(in);
            } catch (Exception e) { // NOPMD
                // can generate IllegalArgumentException as well as IOException
                // autodetection, simply not a TAR
                // ignored
            }
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

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.