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;
}
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();
}
}
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();
}
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);
}
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;
}
}
Aggregations