Search in sources :

Example 76 with TarArchiveOutputStream

use of org.apache.commons.compress.archivers.tar.TarArchiveOutputStream in project neo4j by neo4j.

the class Dumper method openArchiveOut.

private ArchiveOutputStream openArchiveOut(Path archive, CompressionFormat format) throws IOException {
    // StandardOpenOption.CREATE_NEW is important here because it atomically asserts that the file doesn't
    // exist as it is opened, avoiding a TOCTOU race condition which results in a security vulnerability. I
    // can't see a way to write a test to verify that we are using this option rather than just implementing
    // the check ourselves non-atomically.
    OutputStream out = Files.newOutputStream(archive, StandardOpenOption.CREATE_NEW);
    OutputStream compress = format.compress(out);
    // Add enough archive meta-data that the load command can print a meaningful progress indicator.
    if (format == CompressionFormat.ZSTD) {
        writeArchiveMetadata(compress);
    }
    TarArchiveOutputStream tarball = new TarArchiveOutputStream(compress);
    tarball.setLongFileMode(TarArchiveOutputStream.LONGFILE_POSIX);
    tarball.setBigNumberMode(TarArchiveOutputStream.BIGNUMBER_POSIX);
    return tarball;
}
Also used : OutputStream(java.io.OutputStream) TarArchiveOutputStream(org.apache.commons.compress.archivers.tar.TarArchiveOutputStream) DataOutputStream(java.io.DataOutputStream) ArchiveOutputStream(org.apache.commons.compress.archivers.ArchiveOutputStream) TarArchiveOutputStream(org.apache.commons.compress.archivers.tar.TarArchiveOutputStream)

Example 77 with TarArchiveOutputStream

use of org.apache.commons.compress.archivers.tar.TarArchiveOutputStream in project gitblit by gitblit.

the class PtServlet method doGet.

@Override
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
    try {
        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();
    }
}
Also used : OutputStream(java.io.OutputStream) ByteArrayOutputStream(java.io.ByteArrayOutputStream) CompressorOutputStream(org.apache.commons.compress.compressors.CompressorOutputStream) TarArchiveOutputStream(org.apache.commons.compress.archivers.tar.TarArchiveOutputStream) ZipArchiveOutputStream(org.apache.commons.compress.archivers.zip.ZipArchiveOutputStream) CompressorOutputStream(org.apache.commons.compress.compressors.CompressorOutputStream) ZipArchiveOutputStream(org.apache.commons.compress.archivers.zip.ZipArchiveOutputStream) CompressorStreamFactory(org.apache.commons.compress.compressors.CompressorStreamFactory) ServletException(javax.servlet.ServletException) IOException(java.io.IOException) FileInputStream(java.io.FileInputStream) TarArchiveEntry(org.apache.commons.compress.archivers.tar.TarArchiveEntry) ZipArchiveEntry(org.apache.commons.compress.archivers.zip.ZipArchiveEntry) TarArchiveOutputStream(org.apache.commons.compress.archivers.tar.TarArchiveOutputStream) File(java.io.File)

Example 78 with TarArchiveOutputStream

use of org.apache.commons.compress.archivers.tar.TarArchiveOutputStream in project disconf by knightliao.

the class TarUtils method tarFiles.

/**
 * @param fileList
 *
 * @return
 *
 * @throws IOException
 * @throws CompressorException
 */
public static String tarFiles(String dir, String fileNamePrefix, List<File> fileList) throws IOException, CompressorException {
    // 
    OsUtil.makeDirs(dir);
    // 时间
    String curTime = DateUtils.format(new Date(), DataFormatConstants.COMMON_TIME_FORMAT);
    // 文件名
    String outputFilePath = fileNamePrefix + "_" + curTime + ".tar.gz";
    File outputFile = new File(dir, outputFilePath);
    FileOutputStream out = null;
    out = new FileOutputStream(outputFile);
    // 
    // 进行打包
    // 
    TarArchiveOutputStream os = new TarArchiveOutputStream(out);
    for (File file : fileList) {
        os.putArchiveEntry(new TarArchiveEntry(file, file.getName()));
        IOUtils.copy(new FileInputStream(file), os);
        os.closeArchiveEntry();
    }
    if (os != null) {
        try {
            os.flush();
            os.close();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
    return outputFile.getAbsolutePath();
}
Also used : FileOutputStream(java.io.FileOutputStream) TarArchiveOutputStream(org.apache.commons.compress.archivers.tar.TarArchiveOutputStream) IOException(java.io.IOException) File(java.io.File) Date(java.util.Date) TarArchiveEntry(org.apache.commons.compress.archivers.tar.TarArchiveEntry) FileInputStream(java.io.FileInputStream)

Example 79 with TarArchiveOutputStream

use of org.apache.commons.compress.archivers.tar.TarArchiveOutputStream in project vespa by vespa-engine.

the class CompressedApplicationInputStreamTest method createTarFile.

public static File createTarFile() throws IOException {
    File outFile = File.createTempFile("testapp", ".tar.gz");
    ArchiveOutputStream archiveOutputStream = new TarArchiveOutputStream(new GZIPOutputStream(new FileOutputStream(outFile)));
    return createArchiveFile(archiveOutputStream, outFile);
}
Also used : GZIPOutputStream(java.util.zip.GZIPOutputStream) FileOutputStream(java.io.FileOutputStream) TarArchiveOutputStream(org.apache.commons.compress.archivers.tar.TarArchiveOutputStream) File(java.io.File) TarArchiveOutputStream(org.apache.commons.compress.archivers.tar.TarArchiveOutputStream) ArchiveOutputStream(org.apache.commons.compress.archivers.ArchiveOutputStream) ZipArchiveOutputStream(org.apache.commons.compress.archivers.zip.ZipArchiveOutputStream)

Example 80 with TarArchiveOutputStream

use of org.apache.commons.compress.archivers.tar.TarArchiveOutputStream in project twister2 by DSC-SPIDAL.

the class TarGzipPacker method createTarGzipPacker.

/**
 * create TarGzipPacker object
 */
public static TarGzipPacker createTarGzipPacker(String targetDir, Config config) {
    // this should be received from config
    String archiveFilename = SchedulerContext.jobPackageFileName(config);
    Path archiveFile = Paths.get(targetDir + "/" + archiveFilename);
    try {
        // construct output stream
        OutputStream outStream = Files.newOutputStream(archiveFile);
        GzipCompressorOutputStream gzipOutputStream = new GzipCompressorOutputStream(outStream);
        TarArchiveOutputStream tarOutputStream = new TarArchiveOutputStream(gzipOutputStream);
        return new TarGzipPacker(archiveFile, tarOutputStream);
    } catch (IOException ioe) {
        LOG.log(Level.SEVERE, "Archive file can not be created: " + archiveFile, ioe);
        return null;
    }
}
Also used : Path(java.nio.file.Path) GzipCompressorOutputStream(org.apache.commons.compress.compressors.gzip.GzipCompressorOutputStream) TarArchiveOutputStream(org.apache.commons.compress.archivers.tar.TarArchiveOutputStream) GzipCompressorOutputStream(org.apache.commons.compress.compressors.gzip.GzipCompressorOutputStream) OutputStream(java.io.OutputStream) FileOutputStream(java.io.FileOutputStream) TarArchiveOutputStream(org.apache.commons.compress.archivers.tar.TarArchiveOutputStream) IOException(java.io.IOException)

Aggregations

TarArchiveOutputStream (org.apache.commons.compress.archivers.tar.TarArchiveOutputStream)101 File (java.io.File)42 TarArchiveEntry (org.apache.commons.compress.archivers.tar.TarArchiveEntry)42 FileOutputStream (java.io.FileOutputStream)37 GzipCompressorOutputStream (org.apache.commons.compress.compressors.gzip.GzipCompressorOutputStream)30 BufferedOutputStream (java.io.BufferedOutputStream)22 IOException (java.io.IOException)21 ByteArrayOutputStream (java.io.ByteArrayOutputStream)20 FileInputStream (java.io.FileInputStream)19 Path (java.nio.file.Path)17 GZIPOutputStream (java.util.zip.GZIPOutputStream)17 ByteArrayInputStream (java.io.ByteArrayInputStream)15 OutputStream (java.io.OutputStream)13 Test (org.junit.Test)12 ArchiveEntry (org.apache.commons.compress.archivers.ArchiveEntry)11 ArchiveOutputStream (org.apache.commons.compress.archivers.ArchiveOutputStream)11 TarArchiveInputStream (org.apache.commons.compress.archivers.tar.TarArchiveInputStream)9 ZipArchiveOutputStream (org.apache.commons.compress.archivers.zip.ZipArchiveOutputStream)9 ImageArchiveManifest (io.fabric8.maven.docker.model.ImageArchiveManifest)8 InputStream (java.io.InputStream)7