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:org.dataconservancy.ui.util.TarPackageExtractor.java

@Override
protected List<File> unpackFilesFromStream(InputStream packageInputStream, String packageDir, String fileName)
        throws UnpackException {
    final TarArchiveInputStream tarInStream;

    if (!TarArchiveInputStream.class.isAssignableFrom(packageInputStream.getClass())) {
        tarInStream = new TarArchiveInputStream(packageInputStream);
    } else {//  ww  w . j a  v  a 2 s .  co  m
        tarInStream = (TarArchiveInputStream) packageInputStream;
    }

    List<File> files = new ArrayList<File>();
    try {
        TarArchiveEntry entry = tarInStream.getNextTarEntry();
        //Get next tar entry returns null when there are no more entries
        while (entry != null) {
            //Directories are automatically handled by the base class so we can ignore them in this class.
            if (!entry.isDirectory()) {
                File entryFile = new File(packageDir, entry.getName());
                if (entryFile != null) {
                    List<File> savedFiles = saveExtractedFile(entryFile, tarInStream);
                    files.addAll(savedFiles);
                }
            }
            entry = tarInStream.getNextTarEntry();
        }

        tarInStream.close();
    } catch (IOException e) {
        final String msg = "Error processing TarArchiveInputStream: " + e.getMessage();
        log.error(msg, e);
        throw new UnpackException(msg, e);
    }

    return files;
}

From source file:org.datavec.api.util.ArchiveUtils.java

/**
 * Extracts files to the specified destination
 * @param file the file to extract to/*w  w w  .ja va2  s  . c  o  m*/
 * @param dest the destination directory
 * @throws java.io.IOException
 */
public static void unzipFileTo(String file, String dest) throws IOException {
    File target = new File(file);
    if (!target.exists())
        throw new IllegalArgumentException("Archive doesnt exist");
    FileInputStream fin = new FileInputStream(target);
    int BUFFER = 2048;
    byte data[] = new byte[BUFFER];

    if (file.endsWith(".zip") || file.endsWith(".jar")) {
        //getFromOrigin the zip file content
        ZipInputStream zis = new ZipInputStream(fin);
        //getFromOrigin the zipped file list entry
        ZipEntry ze = zis.getNextEntry();

        while (ze != null) {
            String fileName = ze.getName();

            File newFile = new File(dest + File.separator + fileName);

            if (ze.isDirectory()) {
                newFile.mkdirs();
                zis.closeEntry();
                ze = zis.getNextEntry();
                continue;
            }

            log.info("file unzip : " + newFile.getAbsoluteFile());

            //create all non exists folders
            //else you will hit FileNotFoundException for compressed folder

            FileOutputStream fos = new FileOutputStream(newFile);

            int len;
            while ((len = zis.read(data)) > 0) {
                fos.write(data, 0, len);
            }

            fos.flush();
            fos.close();
            zis.closeEntry();
            ze = zis.getNextEntry();
        }

        zis.close();

    }

    else if (file.endsWith(".tar")) {

        BufferedInputStream in = new BufferedInputStream(fin);
        TarArchiveInputStream tarIn = new TarArchiveInputStream(in);

        TarArchiveEntry entry = null;

        /** Read the tar entries using the getNextEntry method **/

        while ((entry = (TarArchiveEntry) tarIn.getNextEntry()) != null) {

            log.info("Extracting: " + entry.getName());

            /** If the entry is a directory, createComplex the directory. **/

            if (entry.isDirectory()) {

                File f = new File(dest + File.separator + entry.getName());
                f.mkdirs();
            }
            /**
             * If the entry is a file,write the decompressed file to the disk
             * and close destination stream.
             **/
            else {
                int count;

                FileOutputStream fos = new FileOutputStream(dest + File.separator + entry.getName());
                BufferedOutputStream destStream = new BufferedOutputStream(fos, BUFFER);
                while ((count = tarIn.read(data, 0, BUFFER)) != -1) {
                    destStream.write(data, 0, count);
                }

                destStream.flush();
                ;

                IOUtils.closeQuietly(destStream);
            }
        }

        /** Close the input stream **/

        tarIn.close();
    }

    else if (file.endsWith(".tar.gz") || file.endsWith(".tgz")) {

        BufferedInputStream in = new BufferedInputStream(fin);
        GzipCompressorInputStream gzIn = new GzipCompressorInputStream(in);
        TarArchiveInputStream tarIn = new TarArchiveInputStream(gzIn);

        TarArchiveEntry entry = null;

        /** Read the tar entries using the getNextEntry method **/

        while ((entry = (TarArchiveEntry) tarIn.getNextEntry()) != null) {

            log.info("Extracting: " + entry.getName());

            /** If the entry is a directory, createComplex the directory. **/

            if (entry.isDirectory()) {

                File f = new File(dest + File.separator + entry.getName());
                f.mkdirs();
            }
            /**
             * If the entry is a file,write the decompressed file to the disk
             * and close destination stream.
             **/
            else {
                int count;

                FileOutputStream fos = new FileOutputStream(dest + File.separator + entry.getName());
                BufferedOutputStream destStream = new BufferedOutputStream(fos, BUFFER);
                while ((count = tarIn.read(data, 0, BUFFER)) != -1) {
                    destStream.write(data, 0, count);
                }

                destStream.flush();

                IOUtils.closeQuietly(destStream);
            }
        }

        /** Close the input stream **/

        tarIn.close();
    }

    else if (file.endsWith(".gz")) {
        GZIPInputStream is2 = new GZIPInputStream(fin);
        File extracted = new File(target.getParent(), target.getName().replace(".gz", ""));
        if (extracted.exists())
            extracted.delete();
        extracted.createNewFile();
        OutputStream fos = FileUtils.openOutputStream(extracted);
        IOUtils.copyLarge(is2, fos);
        is2.close();
        fos.flush();
        fos.close();
    }

}

From source file:org.dcm4chee.storage.tar.TarContainerProvider.java

@Override
public InputStream seekEntry(RetrieveContext ctx, String name, String entryName, InputStream in)
        throws IOException {
    TarArchiveInputStream tar = new TarArchiveInputStream(in);
    String checksumEntry = container.getChecksumEntry();
    TarArchiveEntry nextEntry;
    while ((nextEntry = tar.getNextTarEntry()) != null) {
        String nextEntryName = nextEntry.getName();
        if (nextEntry.isDirectory() || nextEntryName.equals(checksumEntry))
            continue;

        if (nextEntryName.equals(entryName))
            return tar;
    }//from  w  w  w .  j a v  a2s  .  co  m
    throw new ObjectNotFoundException(ctx.getStorageSystem().getStorageSystemPath(), name, entryName);
}

From source file:org.dcm4chee.storage.tar.TarContainerProvider.java

@Override
public void extractEntries(RetrieveContext ctx, String name, ExtractTask extractTask, InputStream in)
        throws IOException {
    TarArchiveInputStream tar = new TarArchiveInputStream(in);
    TarArchiveEntry entry = skipDirectoryEntries(tar);
    if (entry == null)
        throw new IOException("No entries in " + name);
    String entryName = entry.getName();
    Map<String, byte[]> checksums = null;
    String checksumEntry = container.getChecksumEntry();
    MessageDigest digest = null;// w  ww. ja  v  a2 s .com
    if (checksumEntry != null) {
        if (checksumEntry.equals(entryName)) {
            try {
                digest = MessageDigest
                        .getInstance(ctx.getStorageSystem().getStorageSystemGroup().getDigestAlgorithm());
            } catch (NoSuchAlgorithmException e) {
                throw new RuntimeException(e);
            }
            checksums = ContainerEntry.readChecksumsFrom(tar);
        } else
            LOG.warn("Misssing checksum entry in %s", name);
        entry = skipDirectoryEntries(tar);
    }

    for (; entry != null; entry = skipDirectoryEntries(tar)) {
        entryName = entry.getName();
        InputStream in0 = tar;
        byte[] checksum = null;
        if (checksums != null && digest != null) {
            checksum = checksums.remove(entryName);
            if (checksum == null)
                throw new ChecksumException(
                        "Missing checksum for container entry: " + entryName + " in " + name);
            digest.reset();
            in0 = new DigestInputStream(tar, digest);
        }

        extractTask.copyStream(entryName, in0);

        if (checksums != null && digest != null) {
            if (!Arrays.equals(digest.digest(), checksum)) {
                throw new ChecksumException(
                        "Checksums do not match for container entry: " + entry.getName() + " in " + name);
            }
        }

        extractTask.entryExtracted(entryName);
    }
}

From source file:org.dcm4chee.storage.test.unit.tar.TarContainerProviderTest.java

private static void assertTarEntryEquals(TarArchiveEntry expected, TarArchiveEntry actual) {
    assertEquals(expected.getName(), actual.getName());
    assertEquals(expected.getSize(), actual.getSize());
}

From source file:org.deeplearning4j.examples.multigpu.w2vsentiment.DataSetsBuilder.java

private static void extractTarGz(String filePath, String outputPath) throws IOException {
    int fileCount = 0;
    int dirCount = 0;
    System.out.print("Extracting files");
    try (TarArchiveInputStream tais = new TarArchiveInputStream(
            new GzipCompressorInputStream(new BufferedInputStream(new FileInputStream(filePath))))) {
        TarArchiveEntry entry;

        /** Read the tar entries using the getNextEntry method **/
        while ((entry = (TarArchiveEntry) tais.getNextEntry()) != null) {
            //System.out.println("Extracting file: " + entry.getName());

            //Create directories as required
            if (entry.isDirectory()) {
                new File(outputPath + entry.getName()).mkdirs();
                dirCount++;/*from  ww  w  .j  a  va  2  s .co m*/
            } else {
                int count;
                byte data[] = new byte[BUFFER_SIZE];

                FileOutputStream fos = new FileOutputStream(outputPath + entry.getName());
                BufferedOutputStream dest = new BufferedOutputStream(fos, BUFFER_SIZE);
                while ((count = tais.read(data, 0, BUFFER_SIZE)) != -1) {
                    dest.write(data, 0, count);
                }
                dest.close();
                fileCount++;
            }
            if (fileCount % 1000 == 0)
                System.out.print(".");
        }
    }

    System.out
            .println("\n" + fileCount + " files and " + dirCount + " directories extracted to: " + outputPath);
}

From source file:org.deeplearning4j.examples.utilities.DataUtilities.java

/**
 * Extract a "tar.gz" file into a local folder.
 * @param inputPath Input file path./*from  ww  w  . j  a va  2 s. c  o  m*/
 * @param outputPath Output directory path.
 * @throws IOException IO error.
 */
public static void extractTarGz(String inputPath, String outputPath) throws IOException {
    if (inputPath == null || outputPath == null)
        return;
    final int bufferSize = 4096;
    if (!outputPath.endsWith("" + File.separatorChar))
        outputPath = outputPath + File.separatorChar;
    try (TarArchiveInputStream tais = new TarArchiveInputStream(
            new GzipCompressorInputStream(new BufferedInputStream(new FileInputStream(inputPath))))) {
        TarArchiveEntry entry;
        while ((entry = (TarArchiveEntry) tais.getNextEntry()) != null) {
            if (entry.isDirectory()) {
                new File(outputPath + entry.getName()).mkdirs();
            } else {
                int count;
                byte data[] = new byte[bufferSize];
                FileOutputStream fos = new FileOutputStream(outputPath + entry.getName());
                BufferedOutputStream dest = new BufferedOutputStream(fos, bufferSize);
                while ((count = tais.read(data, 0, bufferSize)) != -1) {
                    dest.write(data, 0, count);
                }
                dest.close();
            }
        }
    }
}

From source file:org.deeplearning4j.util.ArchiveUtils.java

/**
 * Extracts files to the specified destination
 * @param file the file to extract to//from  w w w.j  a va 2  s  .  co  m
 * @param dest the destination directory
 * @throws IOException
 */
public static void unzipFileTo(String file, String dest) throws IOException {
    File target = new File(file);
    if (!target.exists())
        throw new IllegalArgumentException("Archive doesnt exist");
    FileInputStream fin = new FileInputStream(target);
    int BUFFER = 2048;
    byte data[] = new byte[BUFFER];

    if (file.endsWith(".zip")) {
        //getFromOrigin the zip file content
        ZipInputStream zis = new ZipInputStream(fin);
        //getFromOrigin the zipped file list entry
        ZipEntry ze = zis.getNextEntry();

        while (ze != null) {

            String fileName = ze.getName();
            File newFile = new File(dest + File.separator + fileName);

            log.info("file unzip : " + newFile.getAbsoluteFile());

            //createComplex all non exists folders
            //else you will hit FileNotFoundException for compressed folder
            new File(newFile.getParent()).mkdirs();

            FileOutputStream fos = new FileOutputStream(newFile);

            int len;
            while ((len = zis.read(data)) > 0) {
                fos.write(data, 0, len);
            }

            fos.close();
            ze = zis.getNextEntry();
        }

        zis.closeEntry();
        zis.close();

    }

    else if (file.endsWith(".tar.gz") || file.endsWith(".tgz")) {

        BufferedInputStream in = new BufferedInputStream(fin);
        GzipCompressorInputStream gzIn = new GzipCompressorInputStream(in);
        TarArchiveInputStream tarIn = new TarArchiveInputStream(gzIn);

        TarArchiveEntry entry = null;

        /** Read the tar entries using the getNextEntry method **/

        while ((entry = (TarArchiveEntry) tarIn.getNextEntry()) != null) {

            log.info("Extracting: " + entry.getName());

            /** If the entry is a directory, createComplex the directory. **/

            if (entry.isDirectory()) {

                File f = new File(dest + File.separator + entry.getName());
                f.mkdirs();
            }
            /**
             * If the entry is a file,write the decompressed file to the disk
             * and close destination stream.
             **/
            else {
                int count;

                FileOutputStream fos = new FileOutputStream(dest + File.separator + entry.getName());
                BufferedOutputStream destStream = new BufferedOutputStream(fos, BUFFER);
                while ((count = tarIn.read(data, 0, BUFFER)) != -1) {
                    destStream.write(data, 0, count);
                }

                destStream.flush();

                IOUtils.closeQuietly(destStream);
            }
        }

        /** Close the input stream **/

        tarIn.close();
    }

    else if (file.endsWith(".gz")) {
        GZIPInputStream is2 = new GZIPInputStream(fin);
        File extracted = new File(target.getParent(), target.getName().replace(".gz", ""));
        if (extracted.exists())
            extracted.delete();
        extracted.createNewFile();
        OutputStream fos = FileUtils.openOutputStream(extracted);
        IOUtils.copyLarge(is2, fos);
        is2.close();
        fos.flush();
        fos.close();
    }

    target.delete();

}

From source file:org.eclipse.acute.OmnisharpStreamConnectionProvider.java

/**
 *
 * @return path to server, unzipping it if necessary. Can be null is fragment is missing.
 */// w w w  .j  ava2s. c om
private @Nullable File getServer() throws IOException {
    File serverPath = new File(AcutePlugin.getDefault().getStateLocation().toFile(), "omnisharp-roslyn"); //$NON-NLS-1$
    if (!serverPath.exists()) {
        serverPath.mkdirs();
        try (InputStream stream = FileLocator.openStream(AcutePlugin.getDefault().getBundle(),
                new Path("omnisharp-roslyn.tar"), true); //$NON-NLS-1$
                TarArchiveInputStream tarStream = new TarArchiveInputStream(stream);) {
            TarArchiveEntry entry = null;
            while ((entry = tarStream.getNextTarEntry()) != null) {
                if (!entry.isDirectory()) {
                    File targetFile = new File(serverPath, entry.getName());
                    targetFile.getParentFile().mkdirs();
                    InputStream in = new BoundedInputStream(tarStream, entry.getSize()); // mustn't be closed
                    try (FileOutputStream out = new FileOutputStream(targetFile);) {
                        IOUtils.copy(in, out);
                        if (!Platform.OS_WIN32.equals(Platform.getOS())) {
                            int xDigit = entry.getMode() % 10;
                            targetFile.setExecutable(xDigit > 0, (xDigit & 1) == 1);
                            int wDigit = (entry.getMode() / 10) % 10;
                            targetFile.setWritable(wDigit > 0, (wDigit & 1) == 1);
                            int rDigit = (entry.getMode() / 100) % 10;
                            targetFile.setReadable(rDigit > 0, (rDigit & 1) == 1);
                        }
                    }
                }
            }
        }
    }
    return serverPath;
}

From source file:org.eclipse.che.api.vfs.TarArchiver.java

@Override
public void extract(InputStream tarInput, boolean overwrite, int stripNumber)
        throws IOException, ForbiddenException, ConflictException, ServerException {
    try (TarArchiveInputStream tarInputStream = new TarArchiveInputStream(tarInput)) {
        InputStream notClosableInputStream = new NotClosableInputStream(tarInputStream);
        TarArchiveEntry tarEntry;
        while ((tarEntry = tarInputStream.getNextTarEntry()) != null) {
            VirtualFile extractFolder = folder;

            Path relativePath = Path.of(tarEntry.getName());

            if (stripNumber > 0) {
                if (relativePath.length() <= stripNumber) {
                    continue;
                }/*from  w  w  w .ja v a  2s  .com*/
                relativePath = relativePath.subPath(stripNumber);
            }

            if (tarEntry.isDirectory()) {
                if (!extractFolder.hasChild(relativePath)) {
                    extractFolder.createFolder(relativePath.toString());
                }
                continue;
            }

            if (relativePath.length() > 1) {
                Path neededParentPath = relativePath.getParent();
                VirtualFile neededParent = extractFolder.getChild(neededParentPath);
                if (neededParent == null) {
                    neededParent = extractFolder.createFolder(neededParentPath.toString());
                }
                extractFolder = neededParent;
            }

            String fileName = relativePath.getName();
            VirtualFile file = extractFolder.getChild(Path.of(fileName));
            if (file == null) {
                extractFolder.createFile(fileName, notClosableInputStream);
            } else {
                if (overwrite) {
                    file.updateContent(notClosableInputStream);
                } else {
                    throw new ConflictException(String.format("File '%s' already exists", file.getPath()));
                }
            }
        }
    }
}