use of org.apache.commons.compress.compressors.CompressorOutputStream 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.compressors.CompressorOutputStream in project DataX by alibaba.
the class UnstructuredStorageWriterUtil method writeToStream.
public static void writeToStream(RecordReceiver lineReceiver, OutputStream outputStream, Configuration config, String context, TaskPluginCollector taskPluginCollector) {
String encoding = config.getString(Key.ENCODING, Constant.DEFAULT_ENCODING);
// handle blank encoding
if (StringUtils.isBlank(encoding)) {
LOG.warn(String.format("您配置的encoding为[%s], 使用默认值[%s]", encoding, Constant.DEFAULT_ENCODING));
encoding = Constant.DEFAULT_ENCODING;
}
String compress = config.getString(Key.COMPRESS);
BufferedWriter writer = null;
// compress logic
try {
if (null == compress) {
writer = new BufferedWriter(new OutputStreamWriter(outputStream, encoding));
} else {
// TODO more compress
if ("gzip".equalsIgnoreCase(compress)) {
CompressorOutputStream compressorOutputStream = new GzipCompressorOutputStream(outputStream);
writer = new BufferedWriter(new OutputStreamWriter(compressorOutputStream, encoding));
} else if ("bzip2".equalsIgnoreCase(compress)) {
CompressorOutputStream compressorOutputStream = new BZip2CompressorOutputStream(outputStream);
writer = new BufferedWriter(new OutputStreamWriter(compressorOutputStream, encoding));
} else {
throw DataXException.asDataXException(UnstructuredStorageWriterErrorCode.ILLEGAL_VALUE, String.format("仅支持 gzip, bzip2 文件压缩格式 , 不支持您配置的文件压缩格式: [%s]", compress));
}
}
UnstructuredStorageWriterUtil.doWriteToStream(lineReceiver, writer, context, config, taskPluginCollector);
} catch (UnsupportedEncodingException uee) {
throw DataXException.asDataXException(UnstructuredStorageWriterErrorCode.Write_FILE_WITH_CHARSET_ERROR, String.format("不支持的编码格式 : [%s]", encoding), uee);
} catch (NullPointerException e) {
throw DataXException.asDataXException(UnstructuredStorageWriterErrorCode.RUNTIME_EXCEPTION, "运行时错误, 请联系我们", e);
} catch (IOException e) {
throw DataXException.asDataXException(UnstructuredStorageWriterErrorCode.Write_FILE_IO_ERROR, String.format("流写入错误 : [%s]", context), e);
} finally {
IOUtils.closeQuietly(writer);
}
}
Aggregations