Example usage for org.apache.commons.compress.archivers.tar TarArchiveEntry getName

List of usage examples for org.apache.commons.compress.archivers.tar TarArchiveEntry getName

Introduction

In this page you can find the example usage for org.apache.commons.compress.archivers.tar TarArchiveEntry getName.

Prototype

public String getName() 

Source Link

Document

Get this entry's name.

Usage

From source file:gdt.data.entity.ArchiveHandler.java

private void getTarEntries(TarArchiveEntry tarEntry, Stack<TarArchiveEntry> s, String root$) {
    if (tarEntry == null)
        return;/*from  w  w  w  . ja v  a 2  s.c om*/
    if (tarEntry.isDirectory()) {
        try {
            TarArchiveEntry[] tea = tarEntry.getDirectoryEntries();
            if (tea != null) {
                for (TarArchiveEntry aTea : tea) {
                    getTarEntries(aTea, s, root$);
                }
            }

        } catch (Exception e) {
            LOGGER.severe(":getTarEntities:" + e.toString());
        }
    } else {
        String entryName$;
        entryName$ = tarEntry.getName().substring(root$.length());
        tarEntry.setName(entryName$);
        s.push(tarEntry);
    }
}

From source file:gdt.data.entity.ArchiveHandler.java

private boolean append(Entigrator entigrator, String root$, String source$, TarArchiveOutputStream aos) {
    try {/*from w  ww .j av  a 2s .  c om*/

        File[] fa = null;
        File source = new File(source$);
        if (source.exists())
            if (source.isFile())
                fa = new File[] { source };
            else
                fa = source.listFiles();
        if (fa == null)
            return true;
        File recordFile = null;

        Stack<TarArchiveEntry> s = new Stack<TarArchiveEntry>();
        int cnt = 0;

        TarArchiveEntry entry = null;
        for (File aFa : fa) {
            recordFile = aFa;
            entry = new TarArchiveEntry(recordFile);
            entry.setSize(recordFile.length());
            s.clear();
            getTarEntries(entry, s, root$);
            cnt = s.size();
            //  System.out.println("EximpExpert:append:cnt=" + cnt);
            File nextFile = null;
            for (int j = 0; j < cnt; j++) {
                entry = (TarArchiveEntry) s.pop();
                try {
                    String nextFile$ = entigrator.getEntihome() + "/" + entry.getName();
                    //            System.out.println("EximpExpert:append:try next file=" + nextFile$);
                    nextFile = new File(nextFile$);
                    if (!nextFile.exists() || nextFile.length() < 1) {
                        System.out.println("ArchiveHandler:append:wrong next file=" + nextFile$);
                        continue;
                    }
                    aos.putArchiveEntry(entry);
                    IOUtils.copy(new FileInputStream(nextFile$), aos);
                    // System.out.println("EximpExpert:tar_write:j="+j);
                    aos.closeArchiveEntry();
                } catch (Exception ee) {
                    //   System.out.println("EximpExpert:append:" + ee.toString());
                    LOGGER.severe(":append:" + ee.toString());
                }
            }
        }
        //System.out.println("EximpExpert:tar_write:finish");
        return true;

        //System.out.println("EximpExpert:tar_write:exit");
    } catch (Exception e) {
        LOGGER.severe(":append:" + e.toString());
        return false;
    }
}

From source file:edu.harvard.iq.dvn.core.web.study.AddFilesPage.java

private List<StudyFileEditBean> createStudyFilesFromTar(File uploadedInputFile) {
    List<StudyFileEditBean> fbList = new ArrayList<StudyFileEditBean>();

    File dir = new File(uploadedInputFile.getParentFile(), study.getId().toString());
    if (!dir.exists()) {
        dir.mkdir();/*from w  ww .ja  v a  2  s.  c o m*/
    }

    List<File> directoriesToDelete = new ArrayList<File>();
    File unzippedFile = null;
    TarArchiveInputStream tiStream = null;
    FileOutputStream tempOutStream = null;
    String unzipError = "";
    if (GzipUtils.isCompressedFilename(uploadedInputFile.getName())) {
        try {
            GZIPInputStream zippedInput = new GZIPInputStream(new FileInputStream(uploadedInputFile));
            unzippedFile = new File(dir, "unzipped-file-" + UUID.randomUUID());
            FileOutputStream unzippedOutput = new FileOutputStream(unzippedFile);
            byte[] dataBuffer = new byte[8192];
            int i = 0;
            while ((i = zippedInput.read(dataBuffer)) > 0) {
                unzippedOutput.write(dataBuffer, 0, i);
                unzippedOutput.flush();
            }
            tiStream = new TarArchiveInputStream(new FileInputStream(unzippedFile));
        } catch (Exception ex) {
            unzipError = " A common gzip extension was found but is the file corrupt?";
        }
    } else if (BZip2Utils.isCompressedFilename(uploadedInputFile.getName())) {
        try {
            BZip2CompressorInputStream zippedInput = new BZip2CompressorInputStream(
                    new FileInputStream(uploadedInputFile));
            unzippedFile = new File(dir, "unzipped-file-" + UUID.randomUUID());
            FileOutputStream unzippedOutput = new FileOutputStream(unzippedFile);
            byte[] dataBuffer = new byte[8192];
            int i = 0;
            while ((i = zippedInput.read(dataBuffer)) > 0) {
                unzippedOutput.write(dataBuffer, 0, i);
                unzippedOutput.flush();
            }
            tiStream = new TarArchiveInputStream(new FileInputStream(unzippedFile));
        } catch (Exception ex) {
            unzipError = " A common bzip2 extension was found but is the file corrupt?";
        }
    } else {
        try {
            // no need to decompress, carry on
            tiStream = new TarArchiveInputStream(new FileInputStream(uploadedInputFile));
        } catch (FileNotFoundException ex) {
            unzipError = " Is the tar file corrupt?";
        }
    }

    TarArchiveEntry tEntry = null;

    if (tiStream == null) {
        String msg = "Problem reading uploaded file." + unzipError;
        setTarValidationErrorMessage(msg);
        uploadedInputFile.delete();
        fbList = new ArrayList<StudyFileEditBean>();
        return fbList;
    }
    try {
        while ((tEntry = tiStream.getNextTarEntry()) != null) {

            String fileEntryName = tEntry.getName();

            if (!tEntry.isDirectory()) {

                if (fileEntryName != null && !fileEntryName.equals("")) {

                    String dirName = null;
                    String finalFileName = fileEntryName;

                    int ind = fileEntryName.lastIndexOf('/');

                    if (ind > -1) {
                        finalFileName = fileEntryName.substring(ind + 1);
                        if (ind > 0) {
                            dirName = fileEntryName.substring(0, ind);
                            dirName = dirName.replace('/', '-');
                        }
                    } else {
                        finalFileName = fileEntryName;
                    }

                    // only process normal tar entries, not the ones that start with "._" because they were created on a mac:
                    // http://superuser.com/questions/61185/why-do-i-get-files-like-foo-in-my-tarball-on-os-x
                    // http://superuser.com/questions/212896/is-there-any-way-to-prevent-a-mac-from-creating-dot-underscore-files
                    if (!finalFileName.startsWith("._")) {

                        File tempUploadedFile = null;
                        try {
                            tempUploadedFile = FileUtil.createTempFile(dir, finalFileName);
                        } catch (Exception ex) {
                            Logger.getLogger(AddFilesPage.class.getName()).log(Level.SEVERE, null, ex);
                            String msg = "Problem creating temporary file.";
                            setTarValidationErrorMessage(msg);
                        }

                        tempOutStream = new FileOutputStream(tempUploadedFile);

                        byte[] dataBuffer = new byte[8192];
                        int i = 0;

                        while ((i = tiStream.read(dataBuffer)) > 0) {
                            tempOutStream.write(dataBuffer, 0, i);
                            tempOutStream.flush();
                        }

                        tempOutStream.close();

                        try {
                            StudyFileEditBean tempFileBean = new StudyFileEditBean(tempUploadedFile,
                                    studyService.generateFileSystemNameSequence(), study);
                            tempFileBean.setSizeFormatted(tempUploadedFile.length());
                            if (dirName != null) {
                                tempFileBean.getFileMetadata().setCategory(dirName);
                            }
                            fbList.add(tempFileBean);
                            setTarValidationErrorMessage(null);
                        } catch (Exception ex) {
                            String msg = "Problem preparing files for ingest. Is the tar file corrupt?";
                            setTarValidationErrorMessage(msg);
                            uploadedInputFile.delete();
                            tempUploadedFile.delete();
                            fbList = new ArrayList<StudyFileEditBean>();
                        }
                    }
                }
            } else {
                File directory = new File(dir, fileEntryName);
                directory.mkdir();
                directoriesToDelete.add(directory);
            }
        }
    } catch (IOException ex) {
        String msg = "Problem reading tar file. Is it corrupt?";
        setTarValidationErrorMessage(msg);
    }

    // teardown and cleanup
    try {
        tiStream.close();
    } catch (IOException ex) {
        Logger.getLogger(AddFilesPage.class.getName()).log(Level.SEVERE, null, ex);
    }
    if (tiStream != null) {
        try {
            tiStream.close();
        } catch (IOException ex) {
            Logger.getLogger(AddFilesPage.class.getName()).log(Level.SEVERE, null, ex);
        }
    }
    if (tempOutStream != null) {
        try {
            tempOutStream.close();
        } catch (IOException ex) {
            Logger.getLogger(AddFilesPage.class.getName()).log(Level.SEVERE, null, ex);
        }
    }
    uploadedInputFile.delete();
    if (unzippedFile != null) {
        unzippedFile.delete();
    }
    for (File dirToDelete : directoriesToDelete) {
        if (dirToDelete.exists()) {
            try {
                FileUtils.forceDelete(dirToDelete);
            } catch (IOException ex) {
                Logger.getLogger(AddFilesPage.class.getName()).log(Level.SEVERE, null, ex);
            }
        }
    }

    return fbList;
}

From source file:com.spotify.docker.client.DefaultDockerClientTest.java

@Test
public void testExportContainer() throws Exception {
    // Pull image
    sut.pull(BUSYBOX_LATEST);// www.  jav  a 2s .  co  m

    // Create container
    final ContainerConfig config = ContainerConfig.builder().image(BUSYBOX_LATEST).build();
    final String name = randomName();
    final ContainerCreation creation = sut.createContainer(config, name);
    final String id = creation.id();

    ImmutableSet.Builder<String> files = ImmutableSet.builder();
    try (TarArchiveInputStream tarStream = new TarArchiveInputStream(sut.exportContainer(id))) {
        TarArchiveEntry entry;
        while ((entry = tarStream.getNextTarEntry()) != null) {
            files.add(entry.getName());
        }
    }

    // Check that some common files exist
    assertThat(files.build(), both(hasItem("bin/")).and(hasItem("bin/sh")));
}

From source file:com.spotify.docker.client.DefaultDockerClientTest.java

@Test
public void testCopyContainer() throws Exception {
    // Pull image
    sut.pull(BUSYBOX_LATEST);/*from   w ww .  j a v  a 2s.  c  o m*/

    // Create container
    final ContainerConfig config = ContainerConfig.builder().image(BUSYBOX_LATEST).build();
    final String name = randomName();
    final ContainerCreation creation = sut.createContainer(config, name);
    final String id = creation.id();

    ImmutableSet.Builder<String> files = ImmutableSet.builder();
    try (TarArchiveInputStream tarStream = new TarArchiveInputStream(sut.copyContainer(id, "/bin"))) {
        TarArchiveEntry entry;
        while ((entry = tarStream.getNextTarEntry()) != null) {
            files.add(entry.getName());
        }
    }

    // Check that some common files exist
    assertThat(files.build(), both(hasItem("bin/")).and(hasItem("bin/wc")));
}

From source file:com.spotify.docker.client.DefaultDockerClientTest.java

@Test
public void integrationTest() throws Exception {
    // Pull image
    sut.pull(BUSYBOX_LATEST);/*  w  ww.ja  va 2s .  c  o  m*/

    // Create container
    final ContainerConfig config = ContainerConfig.builder().image(BUSYBOX_LATEST)
            .cmd("sh", "-c", "while :; do sleep 1; done").build();
    final String name = randomName();
    final ContainerCreation creation = sut.createContainer(config, name);
    final String id = creation.id();
    assertThat(creation.getWarnings(), anyOf(is(empty()), is(nullValue())));
    assertThat(id, is(any(String.class)));

    // Inspect using container ID
    {
        final ContainerInfo info = sut.inspectContainer(id);
        assertThat(info.id(), equalTo(id));
        assertThat(info.config().image(), equalTo(config.image()));
        assertThat(info.config().cmd(), equalTo(config.cmd()));
    }

    // Inspect using container name
    {
        final ContainerInfo info = sut.inspectContainer(name);
        assertThat(info.config().image(), equalTo(config.image()));
        assertThat(info.config().cmd(), equalTo(config.cmd()));
    }

    // Start container
    sut.startContainer(id);

    final String dockerDirectory = Resources.getResource("dockerSslDirectory").getPath();

    // Copy files to container
    // Docker API should be at least v1.20 to support extracting an archive of files or folders
    // to a directory in a container
    if (compareVersion(sut.version().apiVersion(), "1.20") >= 0) {
        try {
            sut.copyToContainer(Paths.get(dockerDirectory), id, "/tmp");
        } catch (Exception e) {
            fail("error copying files to container");
        }

        // Copy the same files from container
        final ImmutableSet.Builder<String> filesDownloaded = ImmutableSet.builder();
        try (TarArchiveInputStream tarStream = new TarArchiveInputStream(sut.copyContainer(id, "/tmp"))) {
            TarArchiveEntry entry;
            while ((entry = tarStream.getNextTarEntry()) != null) {
                filesDownloaded.add(entry.getName());
            }
        }

        // Check that we got back what we put in
        final File folder = new File(dockerDirectory);
        final File[] files = folder.listFiles();
        if (files != null) {
            for (File file : files) {
                if (!file.isDirectory()) {
                    Boolean found = false;
                    for (String fileDownloaded : filesDownloaded.build()) {
                        if (fileDownloaded.contains(file.getName())) {
                            found = true;
                        }
                    }
                    assertTrue(found);
                }
            }
        }
    }

    // Kill container
    sut.killContainer(id);

    try {
        // Remove the container
        sut.removeContainer(id);
    } catch (DockerRequestException e) {
        // CircleCI doesn't let you remove a container :(
        if (!CIRCLECI) {
            // Verify that the container is gone
            exception.expect(ContainerNotFoundException.class);
            sut.inspectContainer(id);
        }
    }
}

From source file:fur.shadowdrake.minecraft.InstallPanel.java

/**
 * Untar an input file into an output file.
 *
 * The output file is created in the output folder, having the same name as
 * the input file, minus the '.tar' extension.
 *
 * @param is the input .tar stream./* w  w w .  j  a v  a2 s .co m*/
 * @param outputDir the output directory file.
 * @throws IOException
 * @throws FileNotFoundException
 *
 * @return The {@link List} of {@link File}s with the untared content.
 * @throws ArchiveException
 */
@SuppressWarnings("ConvertToTryWithResources")
private ArrayList<String> unTar(final InputStream is, final File outputDir)
        throws FileNotFoundException, IOException, ArchiveException {

    final ArrayList<String> untaredFiles = new ArrayList<>();
    final TarArchiveInputStream archiveStream = (TarArchiveInputStream) new ArchiveStreamFactory()
            .createArchiveInputStream("tar", is);
    TarArchiveEntry entry;
    int bytesRead = 0;
    /*int x = 0;*/
    byte[] buf = new byte[16384];
    /*log.println("|---+----+----+----+----+----+----+----+----+----|");
    log.print("/");*/
    while ((entry = (TarArchiveEntry) archiveStream.getNextEntry()) != null) {
        final File outputFile = new File(outputDir, entry.getName());
        if (entry.isDirectory()) {
            if (!outputFile.exists()) {
                if (!outputFile.mkdirs()) {
                    throw new IllegalStateException(
                            String.format("Couldn't create directory %s.", outputFile.getAbsolutePath()));
                }
            }
        } else {
            final FileOutputStream outputFileStream = new FileOutputStream(outputFile);
            /*int incr = Math.floorDiv(downloadSize, 50);*/
            for (int n = archiveStream.read(buf); n > 0; n = archiveStream.read(buf)) {
                outputFileStream.write(buf, 0, n);
                log.advance(n);
                /*bytesRead += n;
                x++;*/
                /*if (bytesRead >= incr) {
                log.backspace();
                log.print("#");
                switch (Math.floorDiv(x, 100)) {
                    case 0:
                        log.print("/");
                        break;
                    case 1:
                        log.print("-");
                        break;
                    case 2:
                        log.print("\\");
                        break;
                    case 3:
                        log.print("|");
                        break;
                }
                bytesRead -= incr;
                                   }
                                   if (x % 100 == 0) {
                log.backspace();
                switch (Math.floorDiv(x, 100)) {
                    case 0:
                    case 4:
                        log.print("/");
                        x = 0;
                        break;
                    case 1:
                        log.print("-");
                        break;
                    case 2:
                        log.print("\\");
                        break;
                    case 3:
                        log.print("|");
                        break;
                }
                                   }*/
            }
            outputFileStream.close();
        }
        untaredFiles.add(entry.getName());
    }
    archiveStream.close();
    /*log.backspace();
    log.println("#");*/

    return untaredFiles;
}

From source file:net.yacy.document.parser.tarParser.java

@Override
public Document[] parse(final DigestURL location, final String mimeType, final String charset,
        final VocabularyScraper scraper, final int timezoneOffset, InputStream source)
        throws Parser.Failure, InterruptedException {

    final String filename = location.getFileName();
    final String ext = MultiProtocolURL.getFileExtension(filename);
    if (ext.equals("gz") || ext.equals("tgz")) {
        try {//  w ww  .ja  v a2s .  c  o  m
            source = new GZIPInputStream(source);
        } catch (final IOException e) {
            throw new Parser.Failure("tar parser: " + e.getMessage(), location);
        }
    }
    TarArchiveEntry entry;
    final TarArchiveInputStream tis = new TarArchiveInputStream(source);

    // create maindoc for this bzip container
    final Document maindoc = new Document(location, mimeType, charset, this, null, null,
            AbstractParser
                    .singleList(filename.isEmpty() ? location.toTokens() : MultiProtocolURL.unescape(filename)), // title
            null, null, null, null, 0.0d, 0.0d, (Object) null, null, null, null, false, new Date());
    // loop through the elements in the tar file and parse every single file inside
    while (true) {
        try {
            File tmp = null;
            entry = tis.getNextTarEntry();
            if (entry == null)
                break;
            if (entry.isDirectory() || entry.getSize() <= 0)
                continue;
            final String name = entry.getName();
            final int idx = name.lastIndexOf('.');
            final String mime = TextParser.mimeOf((idx > -1) ? name.substring(idx + 1) : "");
            try {
                tmp = FileUtils.createTempFile(this.getClass(), name);
                FileUtils.copy(tis, tmp, entry.getSize());
                final Document[] subDocs = TextParser.parseSource(AnchorURL.newAnchor(location, "#" + name),
                        mime, null, scraper, timezoneOffset, 999, tmp);
                if (subDocs == null)
                    continue;
                maindoc.addSubDocuments(subDocs);
            } catch (final Parser.Failure e) {
                AbstractParser.log.warn("tar parser entry " + name + ": " + e.getMessage());
            } finally {
                if (tmp != null)
                    FileUtils.deletedelete(tmp);
            }
        } catch (final IOException e) {
            AbstractParser.log.warn("tar parser:" + e.getMessage());
            break;
        }
    }
    return new Document[] { maindoc };
}

From source file:net.yacy.utils.tarTools.java

/**
 * Untar for any tar archive, overwrites existing data. Closes the
 * InputStream once terminated.//from w  w w .j  av a  2  s . c o m
 * 
 * @param in
 *            input stream. Must not be null. (use
 *            {@link #getInputStream(String)} for convenience)
 * @param untarDir
 *            destination path. Must not be null.
 * @throws IOException
 *             when a read/write error occurred
 * @throws FileNotFoundException
 *             when the untarDir does not exists
 * @throws NullPointerException
 *             when a parameter is null
 */
public static void unTar(final InputStream in, final String untarDir) throws IOException {
    ConcurrentLog.info("UNTAR", "starting");
    if (new File(untarDir).exists()) {
        final TarArchiveInputStream tin = new TarArchiveInputStream(in);
        try {
            TarArchiveEntry tarEntry = tin.getNextTarEntry();
            if (tarEntry == null) {
                throw new IOException("tar archive is empty or corrupted");
            }
            while (tarEntry != null) {
                final File destPath = new File(untarDir + File.separator + tarEntry.getName());
                if (!tarEntry.isDirectory()) {
                    new File(destPath.getParent()).mkdirs(); // create missing subdirectories
                    final FileOutputStream fout = new FileOutputStream(destPath);
                    IOUtils.copyLarge(tin, fout, 0, tarEntry.getSize());
                    fout.close();
                } else {
                    destPath.mkdir();
                }
                tarEntry = tin.getNextTarEntry();
            }
        } finally {
            try {
                tin.close();
            } catch (IOException ignored) {
                ConcurrentLog.warn("UNTAR", "InputStream could not be closed");
            }
        }
    } else { // untarDir doesn't exist
        ConcurrentLog.warn("UNTAR", "destination " + untarDir + " doesn't exist.");
        /* Still have to close the input stream */
        try {
            in.close();
        } catch (IOException ignored) {
            ConcurrentLog.warn("UNTAR", "InputStream could not be closed");
        }
        throw new FileNotFoundException("Output untar directory not found : " + untarDir);
    }
    ConcurrentLog.info("UNTAR", "finished");
}

From source file:net.zyuiop.remoteworldloader.utils.CompressionUtils.java

public static void uncompressArchive(File archive, File target) throws IOException, CompressorException {
    CompressorInputStream compressor = new GzipCompressorInputStream(new FileInputStream(archive));
    TarArchiveInputStream stream = new TarArchiveInputStream(compressor);

    TarArchiveEntry entry;
    while ((entry = stream.getNextTarEntry()) != null) {
        File f = new File(target.getCanonicalPath(), entry.getName());
        if (f.exists()) {
            Bukkit.getLogger().warning("The file " + f.getCanonicalPath() + " already exists, deleting it.");
            if (!f.delete()) {
                Bukkit.getLogger().warning("Cannot remove, skipping file.");
            }//  w w  w .ja  v  a 2s  . com
        }

        if (entry.isDirectory()) {
            f.mkdirs();
            continue;
        }

        f.getParentFile().mkdirs();
        f.createNewFile();

        try {
            try (OutputStream fos = new BufferedOutputStream(new FileOutputStream(f))) {
                final byte[] buf = new byte[8192];
                int bytesRead;
                while (-1 != (bytesRead = stream.read(buf)))
                    fos.write(buf, 0, bytesRead);
            }
            Bukkit.getLogger().info("Extracted file " + f.getName() + "...");
        } catch (IOException ioe) {
            f.delete();
            throw ioe;
        }
    }
}