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