Search in sources :

Example 16 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 17 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 18 with TarArchiveOutputStream

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

the class TestFSDownload method createTarFile.

static LocalResource createTarFile(FileContext files, Path p, int len, Random r, LocalResourceVisibility vis) throws IOException, URISyntaxException {
    byte[] bytes = new byte[len];
    r.nextBytes(bytes);
    File archiveFile = new File(p.toUri().getPath() + ".tar");
    archiveFile.createNewFile();
    TarArchiveOutputStream out = new TarArchiveOutputStream(new FileOutputStream(archiveFile));
    TarArchiveEntry entry = new TarArchiveEntry(p.getName());
    entry.setSize(bytes.length);
    out.putArchiveEntry(entry);
    out.write(bytes);
    out.closeArchiveEntry();
    out.close();
    LocalResource ret = recordFactory.newRecordInstance(LocalResource.class);
    ret.setResource(URL.fromPath(new Path(p.toString() + ".tar")));
    ret.setSize(len);
    ret.setType(LocalResourceType.ARCHIVE);
    ret.setVisibility(vis);
    ret.setTimestamp(files.getFileStatus(new Path(p.toString() + ".tar")).getModificationTime());
    return ret;
}
Also used : Path(org.apache.hadoop.fs.Path) FileOutputStream(java.io.FileOutputStream) TarArchiveOutputStream(org.apache.commons.compress.archivers.tar.TarArchiveOutputStream) File(java.io.File) TarArchiveEntry(org.apache.commons.compress.archivers.tar.TarArchiveEntry) LocalResource(org.apache.hadoop.yarn.api.records.LocalResource)

Example 19 with TarArchiveOutputStream

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

the class TestFSDownload method createTgzFile.

static LocalResource createTgzFile(FileContext files, Path p, int len, Random r, LocalResourceVisibility vis) throws IOException, URISyntaxException {
    byte[] bytes = new byte[len];
    r.nextBytes(bytes);
    File gzipFile = new File(p.toUri().getPath() + ".tar.gz");
    gzipFile.createNewFile();
    TarArchiveOutputStream out = new TarArchiveOutputStream(new GZIPOutputStream(new FileOutputStream(gzipFile)));
    TarArchiveEntry entry = new TarArchiveEntry(p.getName());
    entry.setSize(bytes.length);
    out.putArchiveEntry(entry);
    out.write(bytes);
    out.closeArchiveEntry();
    out.close();
    LocalResource ret = recordFactory.newRecordInstance(LocalResource.class);
    ret.setResource(URL.fromPath(new Path(p.toString() + ".tar.gz")));
    ret.setSize(len);
    ret.setType(LocalResourceType.ARCHIVE);
    ret.setVisibility(vis);
    ret.setTimestamp(files.getFileStatus(new Path(p.toString() + ".tar.gz")).getModificationTime());
    return ret;
}
Also used : Path(org.apache.hadoop.fs.Path) GZIPOutputStream(java.util.zip.GZIPOutputStream) FileOutputStream(java.io.FileOutputStream) TarArchiveOutputStream(org.apache.commons.compress.archivers.tar.TarArchiveOutputStream) File(java.io.File) TarArchiveEntry(org.apache.commons.compress.archivers.tar.TarArchiveEntry) LocalResource(org.apache.hadoop.yarn.api.records.LocalResource)

Example 20 with TarArchiveOutputStream

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

the class Dumper method openArchiveOut.

private static ArchiveOutputStream openArchiveOut(Path archive) 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.
    TarArchiveOutputStream tarball = new TarArchiveOutputStream(new GzipCompressorOutputStream(Files.newOutputStream(archive, StandardOpenOption.CREATE_NEW)));
    tarball.setLongFileMode(TarArchiveOutputStream.LONGFILE_POSIX);
    tarball.setBigNumberMode(TarArchiveOutputStream.BIGNUMBER_POSIX);
    return tarball;
}
Also used : GzipCompressorOutputStream(org.apache.commons.compress.compressors.gzip.GzipCompressorOutputStream) TarArchiveOutputStream(org.apache.commons.compress.archivers.tar.TarArchiveOutputStream)

Aggregations

TarArchiveOutputStream (org.apache.commons.compress.archivers.tar.TarArchiveOutputStream)22 FileOutputStream (java.io.FileOutputStream)13 File (java.io.File)12 TarArchiveEntry (org.apache.commons.compress.archivers.tar.TarArchiveEntry)12 FileInputStream (java.io.FileInputStream)7 IOException (java.io.IOException)7 BufferedOutputStream (java.io.BufferedOutputStream)5 ByteArrayOutputStream (java.io.ByteArrayOutputStream)4 ZipArchiveOutputStream (org.apache.commons.compress.archivers.zip.ZipArchiveOutputStream)4 GzipCompressorOutputStream (org.apache.commons.compress.compressors.gzip.GzipCompressorOutputStream)4 OutputStream (java.io.OutputStream)3 GZIPOutputStream (java.util.zip.GZIPOutputStream)3 TarArchiveInputStream (org.apache.commons.compress.archivers.tar.TarArchiveInputStream)3 InputStream (java.io.InputStream)2 Map (java.util.Map)2 WrappedFile (org.apache.camel.WrappedFile)2 GenericFile (org.apache.camel.component.file.GenericFile)2 ArchiveEntry (org.apache.commons.compress.archivers.ArchiveEntry)2 ArchiveStreamFactory (org.apache.commons.compress.archivers.ArchiveStreamFactory)2 CompressorStreamFactory (org.apache.commons.compress.compressors.CompressorStreamFactory)2