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

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

Introduction

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

Prototype

public TarArchiveEntry(File file, String fileName) 

Source Link

Document

Construct an entry for a file.

Usage

From source file:lk.score.androphsy.main.NewCase.java

private void addFileToCompression(TarArchiveOutputStream taos, File f, String dir) throws IOException {
    System.out.println(f.getName() + " dir : " + dir);
    TarArchiveEntry tae = new TarArchiveEntry(f, dir);
    taos.putArchiveEntry(tae);/*  w w w. jav a  2s .c  om*/
    if (f.isDirectory()) {
        System.out.println("is a directory");
        taos.closeArchiveEntry();
        for (File childFile : f.listFiles()) {
            System.out.println("child: " + childFile.getName());
            addFileToCompression(taos, childFile, dir + "/" + childFile.getName());
        }
    } else {
        System.out.println("is a file " + f.getName());
        FileInputStream fis = new FileInputStream(f);
        IOUtils.copy(fis, taos);
        System.out.println("file added");
        taos.flush();
        taos.closeArchiveEntry();
    }
}

From source file:frameworks.Masken.java

public static void addFileToTarGz(TarArchiveOutputStream tOut, String path, String base) throws IOException

{
    File f = new File(path);
    System.out.println(f.exists());
    //tmp wegschneiden
    if (base.startsWith("tmp")) {
        base = base.substring(4, base.length());
    }//from w  w  w .j av  a 2s  .  c o m
    String entryName = base + f.getName();

    TarArchiveEntry tarEntry = new TarArchiveEntry(f, entryName);
    tarEntry.setSize(f.length());
    tOut.putArchiveEntry(tarEntry);

    if (f.isFile()) {
        IOUtils.copy(new FileInputStream(f), tOut);
        //  FileInputStream in = new FileInputStream(f);
        //  IOUtils.copy(in, tOut);
        // in.close();

    } else {
        tOut.closeArchiveEntry();

        File[] children = f.listFiles();
        if (children != null) {
            for (File child : children) {
                System.out.println(child.getName());
                if (!child.getName().startsWith("screen.")) {
                    addFileToTarGz(tOut, child.getAbsolutePath(), entryName + "/");

                }
            }

        }
    }
}

From source file:frameworks.Masken.java

public static void createTarGzip(String dirPath, String tarGzPath) throws IOException {
    File inputDirectoryPath = new File(dirPath);
    File outputFile = new File(tarGzPath);

    try (FileOutputStream fileOutputStream = new FileOutputStream(outputFile);
            BufferedOutputStream bufferedOutputStream = new BufferedOutputStream(fileOutputStream);
            GzipCompressorOutputStream gzipOutputStream = new GzipCompressorOutputStream(bufferedOutputStream);
            TarArchiveOutputStream tarArchiveOutputStream = new TarArchiveOutputStream(gzipOutputStream)) {

        tarArchiveOutputStream.setBigNumberMode(TarArchiveOutputStream.BIGNUMBER_POSIX);
        tarArchiveOutputStream.setLongFileMode(TarArchiveOutputStream.LONGFILE_GNU);

        List<File> files = new ArrayList<>(FileUtils.listFiles(inputDirectoryPath,
                new RegexFileFilter("^(.*?)"), DirectoryFileFilter.DIRECTORY));

        for (int i = 0; i < files.size(); i++) {

            File currentFile = files.get(i);
            if (!currentFile.getName().contains(".tgz")) {
                String relativeFilePath = new File(inputDirectoryPath.toURI()).toURI()
                        .relativize(new File(currentFile.getAbsolutePath()).toURI()).getPath();

                TarArchiveEntry tarEntry = new TarArchiveEntry(currentFile, relativeFilePath);
                tarEntry.setSize(currentFile.length());

                tarArchiveOutputStream.putArchiveEntry(tarEntry);
                FileInputStream in = new FileInputStream(currentFile);
                //tarArchiveOutputStream.write(IOUtils.toByteArray(new FileInputStream(currentFile)));
                tarArchiveOutputStream.write(IOUtils.toByteArray(in));

                tarArchiveOutputStream.closeArchiveEntry();
                in.close();/*from w w w  .jav a2s  . c  o  m*/
            }
        }
        tarArchiveOutputStream.close();

    }
}

From source file:org.apache.ant.compress.taskdefs.Tar.java

public Tar() {
    setFactory(new TarStreamFactory() {
        public ArchiveOutputStream getArchiveStream(OutputStream stream, String encoding) throws IOException {
            TarArchiveOutputStream o = (TarArchiveOutputStream) super.getArchiveStream(stream, encoding);
            if (format.equals(Format.OLDGNU)) {
                o.setLongFileMode(TarArchiveOutputStream.LONGFILE_GNU);
            } else if (format.equals(Format.GNU)) {
                o.setLongFileMode(TarArchiveOutputStream.LONGFILE_GNU);
                o.setBigNumberMode(TarArchiveOutputStream.BIGNUMBER_STAR);
            } else if (format.equals(Format.STAR)) {
                o.setLongFileMode(TarArchiveOutputStream.LONGFILE_POSIX);
                o.setBigNumberMode(TarArchiveOutputStream.BIGNUMBER_STAR);
            } else if (format.equals(Format.PAX)) {
                o.setLongFileMode(TarArchiveOutputStream.LONGFILE_POSIX);
                o.setBigNumberMode(TarArchiveOutputStream.BIGNUMBER_POSIX);
                o.setAddPaxHeadersForNonAsciiNames(true);
            }/*from w  ww .  j  av a  2s . co m*/
            return o;
        }
    });
    setEntryBuilder(new ArchiveBase.EntryBuilder() {
        public ArchiveEntry buildEntry(ArchiveBase.ResourceWithFlags r) {
            boolean isDir = r.getResource().isDirectory();
            String name = r.getName();
            if (isDir && !name.endsWith("/")) {
                name += "/";
            } else if (!isDir && name.endsWith("/")) {
                name = name.substring(0, name.length() - 1);
            }
            TarArchiveEntry ent = new TarArchiveEntry(name, getPreserveLeadingSlashes());
            ent.setModTime(round(r.getResource().getLastModified(), 1000));
            ent.setSize(isDir ? 0 : r.getResource().getSize());

            if (!isDir && r.getCollectionFlags().hasModeBeenSet()) {
                ent.setMode(r.getCollectionFlags().getMode());
            } else if (isDir && r.getCollectionFlags().hasDirModeBeenSet()) {
                ent.setMode(r.getCollectionFlags().getDirMode());
            } else if (r.getResourceFlags().hasModeBeenSet()) {
                ent.setMode(r.getResourceFlags().getMode());
            } else {
                ent.setMode(isDir ? ArchiveFileSet.DEFAULT_DIR_MODE : ArchiveFileSet.DEFAULT_FILE_MODE);
            }

            if (r.getResourceFlags().hasUserIdBeenSet()) {
                ent.setUserId(r.getResourceFlags().getUserId());
            } else if (r.getCollectionFlags().hasUserIdBeenSet()) {
                ent.setUserId(r.getCollectionFlags().getUserId());
            }

            if (r.getResourceFlags().hasGroupIdBeenSet()) {
                ent.setGroupId(r.getResourceFlags().getGroupId());
            } else if (r.getCollectionFlags().hasGroupIdBeenSet()) {
                ent.setGroupId(r.getCollectionFlags().getGroupId());
            }

            if (r.getResourceFlags().hasUserNameBeenSet()) {
                ent.setUserName(r.getResourceFlags().getUserName());
            } else if (r.getCollectionFlags().hasUserNameBeenSet()) {
                ent.setUserName(r.getCollectionFlags().getUserName());
            }

            if (r.getResourceFlags().hasGroupNameBeenSet()) {
                ent.setGroupName(r.getResourceFlags().getGroupName());
            } else if (r.getCollectionFlags().hasGroupNameBeenSet()) {
                ent.setGroupName(r.getCollectionFlags().getGroupName());
            }

            return ent;
        }
    });
    setFileSetBuilder(new ArchiveBase.FileSetBuilder() {
        public ArchiveFileSet buildFileSet(Resource dest) {
            ArchiveFileSet afs = new TarFileSet();
            afs.setSrcResource(dest);
            return afs;
        }
    });
}

From source file:org.apache.hadoop.hive.common.CompressionUtils.java

/**
 * Archive all the files in the inputFiles into outputFile
 *
 * @param inputFiles//ww w  .  j  av a 2 s  .co  m
 * @param outputFile
 * @throws IOException
 */
public static void tar(String parentDir, String[] inputFiles, String outputFile) throws IOException {

    FileOutputStream out = null;
    try {
        out = new FileOutputStream(new File(parentDir, outputFile));
        TarArchiveOutputStream tOut = new TarArchiveOutputStream(
                new GzipCompressorOutputStream(new BufferedOutputStream(out)));

        for (int i = 0; i < inputFiles.length; i++) {
            File f = new File(parentDir, inputFiles[i]);
            TarArchiveEntry tarEntry = new TarArchiveEntry(f, f.getName());
            tOut.setLongFileMode(TarArchiveOutputStream.LONGFILE_GNU);
            tOut.putArchiveEntry(tarEntry);
            FileInputStream input = new FileInputStream(f);
            try {
                IOUtils.copy(input, tOut); // copy with 8K buffer, not close
            } finally {
                input.close();
            }
            tOut.closeArchiveEntry();
        }
        tOut.close(); // finishes inside
    } finally {
        // TarArchiveOutputStream seemed not to close files properly in error situation
        org.apache.hadoop.io.IOUtils.closeStream(out);
    }
}

From source file:org.apache.openejb.maven.plugin.BuildTomEEMojo.java

private void tarGz(final TarArchiveOutputStream tarGz, final File f, final String prefix) throws IOException {
    final String path = f.getPath().replace(prefix, "").replace(File.separator, "/");
    final TarArchiveEntry archiveEntry = new TarArchiveEntry(f, path);
    if (isSh(path)) {
        archiveEntry.setMode(0755);//from   w  w  w . j a  v a  2s .  co m
    }
    tarGz.putArchiveEntry(archiveEntry);
    if (f.isDirectory()) {
        tarGz.closeArchiveEntry();
        final File[] files = f.listFiles();
        if (files != null) {
            for (final File child : files) {
                tarGz(tarGz, child, prefix);
            }
        }
    } else {
        IO.copy(f, tarGz);
        tarGz.closeArchiveEntry();
    }
}

From source file:org.apache.reef.runtime.mesos.driver.REEFScheduler.java

private String getReefTarUri(final String jobIdentifier) {
    try {//  w  w  w .j a v  a  2s.c o  m
        // Create REEF_TAR
        final FileOutputStream fileOutputStream = new FileOutputStream(REEF_TAR);
        final TarArchiveOutputStream tarArchiveOutputStream = new TarArchiveOutputStream(
                new GZIPOutputStream(fileOutputStream));
        final File globalFolder = new File(this.fileNames.getGlobalFolderPath());
        final DirectoryStream<Path> directoryStream = Files.newDirectoryStream(globalFolder.toPath());

        for (final Path path : directoryStream) {
            tarArchiveOutputStream.putArchiveEntry(
                    new TarArchiveEntry(path.toFile(), globalFolder + "/" + path.getFileName()));

            final BufferedInputStream bufferedInputStream = new BufferedInputStream(
                    new FileInputStream(path.toFile()));
            IOUtils.copy(bufferedInputStream, tarArchiveOutputStream);
            bufferedInputStream.close();

            tarArchiveOutputStream.closeArchiveEntry();
        }
        directoryStream.close();
        tarArchiveOutputStream.close();
        fileOutputStream.close();

        // Upload REEF_TAR to HDFS
        final FileSystem fileSystem = FileSystem.get(new Configuration());
        final org.apache.hadoop.fs.Path src = new org.apache.hadoop.fs.Path(REEF_TAR);
        final String reefTarUriValue = fileSystem.getUri().toString() + this.jobSubmissionDirectoryPrefix + "/"
                + jobIdentifier + "/" + REEF_TAR;
        final org.apache.hadoop.fs.Path dst = new org.apache.hadoop.fs.Path(reefTarUriValue);
        fileSystem.copyFromLocalFile(src, dst);

        return reefTarUriValue;
    } catch (final IOException e) {
        throw new RuntimeException(e);
    }
}

From source file:org.apache.whirr.util.Tarball.java

private static void addFile(TarArchiveOutputStream tarOutputStream, String path, String base)
        throws IOException {
    File file = new File(path);
    String entryName = base + file.getName();
    TarArchiveEntry tarEntry = new TarArchiveEntry(file, entryName);

    tarOutputStream.setLongFileMode(TarArchiveOutputStream.LONGFILE_GNU);
    tarOutputStream.putArchiveEntry(tarEntry);

    if (file.isFile()) {
        IOUtils.copy(new FileInputStream(file), tarOutputStream);
        tarOutputStream.closeArchiveEntry();
    } else {/* ww  w  . j  ava  2 s  . c o m*/
        tarOutputStream.closeArchiveEntry();
        File[] children = file.listFiles();
        if (children != null) {
            for (File child : children) {
                addFile(tarOutputStream, child.getAbsolutePath(), entryName + "/");
            }
        }
    }
}

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

private static ArchiveEntry createArchiveEntry(File file, String relativePath, ArchiveType archiveType) {
    switch (archiveType) {
    case ZIP://w  w  w.j a va 2s .  c  om
        return new ZipArchiveEntry(file, relativePath);
    case TAR:
        return new TarArchiveEntry(file, relativePath);
    case TARGZ:
        return new TarArchiveEntry(file, relativePath);
    case TGZ:
        return new TarArchiveEntry(file, relativePath);
    }

    throw new IllegalArgumentException("Unsupported archive type: '" + archiveType + "'");
}

From source file:org.cloudbyexample.dc.agent.util.ArchiveUtil.java

/**
 * Add a file to the archive or recurse if a directory is specified.
 *///from ww  w  .ja  va  2  s .  c om
private static void addFiles(TarArchiveOutputStream taos, File file, String dir) throws IOException {
    taos.putArchiveEntry(new TarArchiveEntry(file, dir));

    if (file.isFile()) {
        BufferedInputStream bis = new BufferedInputStream(new FileInputStream(file));
        IOUtils.copy(bis, taos);
        taos.closeArchiveEntry();
        bis.close();
    } else if (file.isDirectory()) {
        taos.closeArchiveEntry();
        for (File childFile : file.listFiles()) {
            addFiles(taos, childFile, childFile.getName());
        }
    }
}