Example usage for org.apache.commons.compress.archivers.tar TarArchiveOutputStream putArchiveEntry

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

Introduction

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

Prototype

public void putArchiveEntry(ArchiveEntry archiveEntry) throws IOException 

Source Link

Document

Put an entry on the output stream.

Usage

From source file:edu.wisc.doit.tcrypt.BouncyCastleFileEncrypter.java

protected void startEncryptedFile(String fileName, int size,
        final TarArchiveOutputStream tarArchiveOutputStream, final BufferedBlockCipher cipher)
        throws IOException {
    //Add the TAR entry and calculate the output size based on the input size
    final TarArchiveEntry encfileEntry = new TarArchiveEntry(fileName + ENC_SUFFIX);
    final int outputSize = cipher.getOutputSize(size);
    encfileEntry.setSize(outputSize);/*from w w  w.  jav  a  2 s .  c  o m*/
    tarArchiveOutputStream.putArchiveEntry(encfileEntry);
}

From source file:com.francetelecom.clara.cloud.mvn.consumer.maven.MavenDeployer.java

private File populateTgzArchive(File archive, List<FileRef> fileset) throws IOException {
    archive.getParentFile().mkdirs();/*  w  w w.j a v a  2s  .co m*/
    CompressorOutputStream zip = new GzipCompressorOutputStream(new FileOutputStream(archive));
    TarArchiveOutputStream tar = new TarArchiveOutputStream(zip);
    for (FileRef fileRef : fileset) {
        TarArchiveEntry entry = new TarArchiveEntry(new File(fileRef.getRelativeLocation()));
        byte[] bytes = fileRef.getContent().getBytes();
        entry.setSize(bytes.length);
        tar.putArchiveEntry(entry);
        tar.write(bytes);
        tar.closeArchiveEntry();
    }
    tar.close();
    return archive;
}

From source file:hudson.gridmaven.gridlayer.HadoopInstance.java

private void addFileToTar(TarArchiveOutputStream tOut, String path, String base, String root)
        throws IOException {
    if (!root.equals(path)) {
        File f = new File(path);
        String entryName = base + f.getName();
        TarArchiveEntry tarEntry = new TarArchiveEntry(f, entryName);
        tOut.setLongFileMode(TarArchiveOutputStream.LONGFILE_GNU);
        tOut.putArchiveEntry(tarEntry);

        if (f.isFile()) {
            IOUtils.copy(new FileInputStream(f), tOut);
            tOut.closeArchiveEntry();/* w w  w .  j ava 2 s .  co m*/
        } else {
            tOut.closeArchiveEntry();
            File[] children = f.listFiles();
            if (children != null) {
                for (File child : children) {
                    addFileToTar(tOut, child.getAbsolutePath(), entryName + "/", root);
                }
            }
        }
    } else {
        File f = new File(path);
        File[] children = f.listFiles();
        if (children != null) {
            for (File child : children) {
                addFileToTar(tOut, child.getAbsolutePath(), "", root);
            }
        }
    }
}

From source file:com.netflix.spinnaker.halyard.backup.services.v1.BackupService.java

private void addFileToTar(TarArchiveOutputStream tarArchiveOutputStream, String path, String base) {
    File file = new File(path);
    String fileName = file.getName();

    if (Arrays.stream(omitPaths).anyMatch(s -> s.equals(fileName))) {
        return;//from  ww w  . ja  va2  s .  c o m
    }

    String tarEntryName = String.join("/", base, fileName);
    try {
        if (file.isFile()) {
            TarArchiveEntry tarEntry = new TarArchiveEntry(file, tarEntryName);
            tarArchiveOutputStream.putArchiveEntry(tarEntry);
            IOUtils.copy(new FileInputStream(file), tarArchiveOutputStream);
            tarArchiveOutputStream.closeArchiveEntry();
        } else if (file.isDirectory()) {
            Arrays.stream(file.listFiles()).filter(Objects::nonNull)
                    .forEach(f -> addFileToTar(tarArchiveOutputStream, f.getAbsolutePath(), tarEntryName));
        } else {
            log.warn("Unknown file type: " + file + " - skipping addition to tar archive");
        }
    } catch (IOException e) {
        throw new HalException(Problem.Severity.FATAL, "Unable to file " + file.getName()
                + " to archive entry: " + tarEntryName + " " + e.getMessage(), e);
    }
}

From source file:gov.nih.nci.ncicb.tcga.dcc.dam.processors.FilePackagerFastTest.java

@Test
public void testBigFileMode() throws IOException {
    final File tempTarFile = new File(THIS_FOLDER + "/fakeBigTar.tar.gz");
    try {//from   w  w w  .  j a  v  a 2  s. c om
        TarArchiveOutputStream tarArchiveOutputStream = packager.makeTarGzOutputStream(tempTarFile);
        TarArchiveEntry fakeEntry = new TarArchiveEntry("test");
        fakeEntry.setSize(9999999999L);
        tarArchiveOutputStream.putArchiveEntry(fakeEntry);

        // if the TarArchiveOutputStream cannot handle entries of this size, it will fail.
        // so if it doesn't then the test passes.  I can't think of anything to assert...

    } finally {
        // delete temp tar file made during test
        tempTarFile.deleteOnExit();
    }
}

From source file:gov.nih.nci.nbia.servlet.DownloadServletV2.java

private void sendAnnotationData(List<AnnotationDTO> annoResults, TarArchiveOutputStream tos)
        throws IOException {
    InputStream annoIn = null;//ww  w .j a  va2  s  . co m
    for (AnnotationDTO a : annoResults) {
        String filePath = a.getFilePath();
        String fileName = a.getFileName();

        try {
            File annotationFile = new File(filePath);
            ArchiveEntry tarArchiveEntry = tos.createArchiveEntry(annotationFile, fileName);
            annoIn = new FileInputStream(annotationFile);
            tos.putArchiveEntry(tarArchiveEntry);
            IOUtils.copy(annoIn, tos);
            tos.closeArchiveEntry();
        } catch (FileNotFoundException e) {
            e.printStackTrace();
            // just print the exception and continue the loop so rest of
            // images will get download.
        } finally {
            IOUtils.closeQuietly(annoIn);
            logger.info("DownloadServlet Annotation transferred at " + new Date().getTime());
        }
    }
}

From source file:com.gitblit.servlet.PtServlet.java

@Override
protected void doGet(HttpServletRequest request, HttpServletResponse response)
        throws ServletException, IOException {
    try {//  ww  w  .j  av a  2 s . c o  m
        response.setContentType("application/octet-stream");
        response.setDateHeader("Last-Modified", lastModified);
        response.setHeader("Cache-Control", "none");
        response.setHeader("Pragma", "no-cache");
        response.setDateHeader("Expires", 0);

        boolean windows = false;
        try {
            String useragent = request.getHeader("user-agent").toString();
            windows = useragent.toLowerCase().contains("windows");
        } catch (Exception e) {
        }

        byte[] pyBytes;
        File file = runtimeManager.getFileOrFolder("tickets.pt", "${baseFolder}/pt.py");
        if (file.exists()) {
            // custom script
            pyBytes = readAll(new FileInputStream(file));
        } else {
            // default script
            pyBytes = readAll(getClass().getResourceAsStream("/pt.py"));
        }

        if (windows) {
            // windows: download zip file with pt.py and pt.cmd
            response.setHeader("Content-Disposition", "attachment; filename=\"pt.zip\"");

            OutputStream os = response.getOutputStream();
            ZipArchiveOutputStream zos = new ZipArchiveOutputStream(os);

            // add the Python script
            ZipArchiveEntry pyEntry = new ZipArchiveEntry("pt.py");
            pyEntry.setSize(pyBytes.length);
            pyEntry.setUnixMode(FileMode.EXECUTABLE_FILE.getBits());
            pyEntry.setTime(lastModified);
            zos.putArchiveEntry(pyEntry);
            zos.write(pyBytes);
            zos.closeArchiveEntry();

            // add a Python launch cmd file
            byte[] cmdBytes = readAll(getClass().getResourceAsStream("/pt.cmd"));
            ZipArchiveEntry cmdEntry = new ZipArchiveEntry("pt.cmd");
            cmdEntry.setSize(cmdBytes.length);
            cmdEntry.setUnixMode(FileMode.REGULAR_FILE.getBits());
            cmdEntry.setTime(lastModified);
            zos.putArchiveEntry(cmdEntry);
            zos.write(cmdBytes);
            zos.closeArchiveEntry();

            // add a brief readme
            byte[] txtBytes = readAll(getClass().getResourceAsStream("/pt.txt"));
            ZipArchiveEntry txtEntry = new ZipArchiveEntry("readme.txt");
            txtEntry.setSize(txtBytes.length);
            txtEntry.setUnixMode(FileMode.REGULAR_FILE.getBits());
            txtEntry.setTime(lastModified);
            zos.putArchiveEntry(txtEntry);
            zos.write(txtBytes);
            zos.closeArchiveEntry();

            // cleanup
            zos.finish();
            zos.close();
            os.flush();
        } else {
            // unix: download a tar.gz file with pt.py set with execute permissions
            response.setHeader("Content-Disposition", "attachment; filename=\"pt.tar.gz\"");

            OutputStream os = response.getOutputStream();
            CompressorOutputStream cos = new CompressorStreamFactory()
                    .createCompressorOutputStream(CompressorStreamFactory.GZIP, os);
            TarArchiveOutputStream tos = new TarArchiveOutputStream(cos);
            tos.setAddPaxHeadersForNonAsciiNames(true);
            tos.setLongFileMode(TarArchiveOutputStream.LONGFILE_POSIX);

            // add the Python script
            TarArchiveEntry pyEntry = new TarArchiveEntry("pt");
            pyEntry.setMode(FileMode.EXECUTABLE_FILE.getBits());
            pyEntry.setModTime(lastModified);
            pyEntry.setSize(pyBytes.length);
            tos.putArchiveEntry(pyEntry);
            tos.write(pyBytes);
            tos.closeArchiveEntry();

            // add a brief readme
            byte[] txtBytes = readAll(getClass().getResourceAsStream("/pt.txt"));
            TarArchiveEntry txtEntry = new TarArchiveEntry("README");
            txtEntry.setMode(FileMode.REGULAR_FILE.getBits());
            txtEntry.setModTime(lastModified);
            txtEntry.setSize(txtBytes.length);
            tos.putArchiveEntry(txtEntry);
            tos.write(txtBytes);
            tos.closeArchiveEntry();

            // cleanup
            tos.finish();
            tos.close();
            cos.close();
            os.flush();
        }
    } catch (Exception e) {
        e.printStackTrace();
    }
}

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);
    if (f.isDirectory()) {
        System.out.println("is a directory");
        taos.closeArchiveEntry();/*  ww  w . j a va  2  s  .c o  m*/
        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:com.netflix.spinnaker.halyard.core.registry.v1.GitProfileReader.java

@Override
public InputStream readArchiveProfile(String artifactName, String version, String profileName)
        throws IOException {
    Path profilePath = Paths.get(profilePath(artifactName, version, profileName));

    ByteArrayOutputStream os = new ByteArrayOutputStream();
    TarArchiveOutputStream tarArchive = new TarArchiveOutputStream(os);

    ArrayList<Path> filePathsToAdd = java.nio.file.Files
            .walk(profilePath, Integer.MAX_VALUE, FileVisitOption.FOLLOW_LINKS)
            .filter(path -> path.toFile().isFile()).collect(Collectors.toCollection(ArrayList::new));

    for (Path path : filePathsToAdd) {
        TarArchiveEntry tarEntry = new TarArchiveEntry(path.toFile(), profilePath.relativize(path).toString());
        int permissions = FileModeUtils.getFileMode(Files.getPosixFilePermissions(path));
        permissions = FileModeUtils.setFileBit(permissions);
        tarEntry.setMode(permissions);//from w  w w. j a  v  a2  s. c  om
        tarArchive.putArchiveEntry(tarEntry);
        IOUtils.copy(Files.newInputStream(path), tarArchive);
        tarArchive.closeArchiveEntry();
    }

    tarArchive.finish();
    tarArchive.close();

    return new ByteArrayInputStream(os.toByteArray());
}

From source file:com.st.maven.debian.DebianPackageMojo.java

private void fillControlTar(Config config, ArFileOutputStream output) throws MojoExecutionException {
    TarArchiveOutputStream tar = null;
    try {// w w w .j  ava  2 s  . c  om
        tar = new TarArchiveOutputStream(new GZIPOutputStream(new ArWrapper(output)));
        tar.setLongFileMode(TarArchiveOutputStream.LONGFILE_GNU);
        TarArchiveEntry rootDir = new TarArchiveEntry("./");
        tar.putArchiveEntry(rootDir);
        tar.closeArchiveEntry();

        byte[] controlData = processTemplate(freemarkerConfig, config, "control.ftl");
        TarArchiveEntry controlEntry = new TarArchiveEntry("./control");
        controlEntry.setSize(controlData.length);
        tar.putArchiveEntry(controlEntry);
        tar.write(controlData);
        tar.closeArchiveEntry();

        byte[] preinstBaseData = processTemplate("preinst", freemarkerConfig, config,
                combine("preinst.ftl", BASE_DIR + File.separator + "preinst", false));
        long size = preinstBaseData.length;
        TarArchiveEntry preinstEntry = new TarArchiveEntry("./preinst");
        preinstEntry.setSize(size);
        preinstEntry.setMode(0755);
        tar.putArchiveEntry(preinstEntry);
        tar.write(preinstBaseData);
        tar.closeArchiveEntry();

        byte[] postinstBaseData = processTemplate("postinst", freemarkerConfig, config,
                combine("postinst.ftl", BASE_DIR + File.separator + "postinst", true));
        size = postinstBaseData.length;
        TarArchiveEntry postinstEntry = new TarArchiveEntry("./postinst");
        postinstEntry.setSize(size);
        postinstEntry.setMode(0755);
        tar.putArchiveEntry(postinstEntry);
        tar.write(postinstBaseData);
        tar.closeArchiveEntry();

        byte[] prermBaseData = processTemplate("prerm", freemarkerConfig, config,
                combine("prerm.ftl", BASE_DIR + File.separator + "prerm", false));
        size = prermBaseData.length;
        TarArchiveEntry prermEntry = new TarArchiveEntry("./prerm");
        prermEntry.setSize(size);
        prermEntry.setMode(0755);
        tar.putArchiveEntry(prermEntry);
        tar.write(prermBaseData);
        tar.closeArchiveEntry();

        byte[] postrmBaseData = processTemplate("postrm", freemarkerConfig, config,
                combine("postrm.ftl", BASE_DIR + File.separator + "postrm", false));
        size = postrmBaseData.length;
        TarArchiveEntry postrmEntry = new TarArchiveEntry("./postrm");
        postrmEntry.setSize(size);
        postrmEntry.setMode(0755);
        tar.putArchiveEntry(postrmEntry);
        tar.write(postrmBaseData);
        tar.closeArchiveEntry();

    } catch (Exception e) {
        throw new MojoExecutionException("unable to create control tar", e);
    } finally {
        if (tar != null) {
            try {
                tar.close();
            } catch (IOException e) {
                getLog().error("unable to finish tar", e);
            }
        }
    }
}