use of java.util.zip.ZipOutputStream in project bazel by bazelbuild.
the class ZipReaderTest method testRawFileData.
@Test
public void testRawFileData() throws IOException {
CRC32 crc = new CRC32();
Deflater deflator = new Deflater(Deflater.DEFAULT_COMPRESSION, true);
try (ZipOutputStream zout = new ZipOutputStream(new FileOutputStream(test))) {
ZipEntry foo = new ZipEntry("foo");
foo.setComment("foo comment.");
foo.setMethod(ZipEntry.DEFLATED);
zout.putNextEntry(foo);
zout.write("foo".getBytes(UTF_8));
zout.closeEntry();
ZipEntry bar = new ZipEntry("bar");
bar.setComment("bar comment.");
bar.setMethod(ZipEntry.STORED);
bar.setSize("bar".length());
bar.setCompressedSize("bar".length());
crc.reset();
crc.update("bar".getBytes(UTF_8));
bar.setCrc(crc.getValue());
zout.putNextEntry(bar);
zout.write("bar".getBytes(UTF_8));
zout.closeEntry();
}
try (ZipReader reader = new ZipReader(test, UTF_8)) {
ZipFileEntry fooEntry = reader.getEntry("foo");
InputStream fooIn = reader.getRawInputStream(fooEntry);
byte[] fooData = new byte[10];
fooIn.read(fooData);
byte[] expectedFooData = new byte[10];
deflator.reset();
deflator.setInput("foo".getBytes(UTF_8));
deflator.finish();
deflator.deflate(expectedFooData);
assertThat(fooData).isEqualTo(expectedFooData);
ZipFileEntry barEntry = reader.getEntry("bar");
InputStream barIn = reader.getRawInputStream(barEntry);
byte[] barData = new byte[3];
barIn.read(barData);
byte[] expectedBarData = "bar".getBytes(UTF_8);
assertThat(barData).isEqualTo(expectedBarData);
assertThat(barIn.read()).isEqualTo(-1);
assertThat(barIn.read(barData)).isEqualTo(-1);
assertThat(barIn.read(barData, 0, 3)).isEqualTo(-1);
thrown.expect(IOException.class);
thrown.expectMessage("Reset is not supported on this type of stream.");
barIn.reset();
}
}
use of java.util.zip.ZipOutputStream in project bazel by bazelbuild.
the class ZipReaderTest method testFileData.
@Test
public void testFileData() throws IOException {
CRC32 crc = new CRC32();
try (ZipOutputStream zout = new ZipOutputStream(new FileOutputStream(test))) {
ZipEntry foo = new ZipEntry("foo");
foo.setComment("foo comment.");
foo.setMethod(ZipEntry.DEFLATED);
zout.putNextEntry(foo);
zout.write("foo".getBytes(UTF_8));
zout.closeEntry();
ZipEntry bar = new ZipEntry("bar");
bar.setComment("bar comment.");
bar.setMethod(ZipEntry.STORED);
bar.setSize("bar".length());
bar.setCompressedSize("bar".length());
crc.reset();
crc.update("bar".getBytes(UTF_8));
bar.setCrc(crc.getValue());
zout.putNextEntry(bar);
zout.write("bar".getBytes(UTF_8));
zout.closeEntry();
}
try (ZipReader reader = new ZipReader(test, UTF_8)) {
ZipFileEntry fooEntry = reader.getEntry("foo");
InputStream fooIn = reader.getInputStream(fooEntry);
byte[] fooData = new byte[3];
fooIn.read(fooData);
byte[] expectedFooData = "foo".getBytes(UTF_8);
assertThat(fooData).isEqualTo(expectedFooData);
assertThat(fooIn.read()).isEqualTo(-1);
assertThat(fooIn.read(fooData)).isEqualTo(-1);
assertThat(fooIn.read(fooData, 0, 3)).isEqualTo(-1);
ZipFileEntry barEntry = reader.getEntry("bar");
InputStream barIn = reader.getInputStream(barEntry);
byte[] barData = new byte[3];
barIn.read(barData);
byte[] expectedBarData = "bar".getBytes(UTF_8);
assertThat(barData).isEqualTo(expectedBarData);
assertThat(barIn.read()).isEqualTo(-1);
assertThat(barIn.read(barData)).isEqualTo(-1);
assertThat(barIn.read(barData, 0, 3)).isEqualTo(-1);
thrown.expect(IOException.class);
thrown.expectMessage("Reset is not supported on this type of stream.");
barIn.reset();
}
}
use of java.util.zip.ZipOutputStream in project bazel by bazelbuild.
the class ZipReaderTest method testSimultaneousReads.
@Test
public void testSimultaneousReads() throws IOException {
byte[] expectedFooData = "This is file foo. It contains a foo.".getBytes(UTF_8);
byte[] expectedBarData = "This is a different file bar. It contains only a bar.".getBytes(UTF_8);
try (ZipOutputStream zout = new ZipOutputStream(new FileOutputStream(test))) {
ZipEntry foo = new ZipEntry("foo");
foo.setComment("foo comment.");
foo.setMethod(ZipEntry.DEFLATED);
zout.putNextEntry(foo);
zout.write(expectedFooData);
zout.closeEntry();
ZipEntry bar = new ZipEntry("bar");
bar.setComment("bar comment.");
bar.setMethod(ZipEntry.DEFLATED);
zout.putNextEntry(bar);
zout.write(expectedBarData);
zout.closeEntry();
}
try (ZipReader reader = new ZipReader(test, UTF_8)) {
ZipFileEntry fooEntry = reader.getEntry("foo");
ZipFileEntry barEntry = reader.getEntry("bar");
InputStream fooIn = reader.getInputStream(fooEntry);
InputStream barIn = reader.getInputStream(barEntry);
byte[] fooData = new byte[expectedFooData.length];
byte[] barData = new byte[expectedBarData.length];
fooIn.read(fooData, 0, 10);
barIn.read(barData, 0, 10);
fooIn.read(fooData, 10, 10);
barIn.read(barData, 10, 10);
fooIn.read(fooData, 20, fooData.length - 20);
barIn.read(barData, 20, barData.length - 20);
assertThat(fooData).isEqualTo(expectedFooData);
assertThat(barData).isEqualTo(expectedBarData);
}
}
use of java.util.zip.ZipOutputStream in project bazel by bazelbuild.
the class ZipReaderTest method testZipEntryInvalidTime.
@Test
public void testZipEntryInvalidTime() throws IOException {
// 11/30/1979 00:00:00, which is also 0 in DOS format
long date = 312796800000L;
byte[] extra = new ExtraData((short) 0xaa, new byte[] { (byte) 0xbb, (byte) 0xcd }).getBytes();
try (ZipOutputStream zout = new ZipOutputStream(new FileOutputStream(test))) {
ZipEntry foo = new ZipEntry("foo");
foo.setComment("foo comment.");
foo.setMethod(ZipEntry.DEFLATED);
foo.setTime(date);
foo.setExtra(extra);
zout.putNextEntry(foo);
zout.write("foo".getBytes(UTF_8));
zout.closeEntry();
}
try (ZipReader reader = new ZipReader(test, UTF_8)) {
ZipFileEntry fooEntry = reader.getEntry("foo");
assertThat(fooEntry.getTime()).isEqualTo(ZipUtil.DOS_EPOCH);
}
}
use of java.util.zip.ZipOutputStream in project eweb4j-framework by laiweiwei.
the class ActionExecution method handleResult.
private void handleResult() throws Exception {
this.exeActionLog();
if (retn == null)
return;
String baseUrl = (String) this.context.getServletContext().getAttribute(MVCConfigConstant.BASE_URL_KEY);
if (File.class.isAssignableFrom(retn.getClass())) {
File file = (File) retn;
this.handleDownload(file);
return;
} else if (File[].class.isAssignableFrom(retn.getClass())) {
File[] files = (File[]) retn;
String fileName = CommonUtil.getNowTime("yyyyMMddHHmmss") + "_" + "download.zip";
HttpServletResponse resp = this.context.getResponse();
resp.reset();
resp.setContentType("application/zip");
resp.addHeader("Content-Disposition", "attachment; filename=\"" + fileName + "\"");
ServletOutputStream outputStream = resp.getOutputStream();
ZipOutputStream zip = new ZipOutputStream(outputStream);
for (File file : files) {
byte[] b = new byte[1024];
int len;
zip.putNextEntry(new ZipEntry(file.getName()));
FileInputStream fis = new FileInputStream(file);
while ((len = fis.read(b)) != -1) {
zip.write(b, 0, len);
}
fis.close();
}
zip.flush();
zip.close();
outputStream.flush();
return;
}
if (!String.class.isAssignableFrom(retn.getClass())) {
String mimeType = null;
boolean hasProduces = JAXWSUtil.hasProduces(method);
if (hasProduces) {
String[] mimeTypes = ProducesUtil.getProducesValue(method);
if (mimeTypes != null && mimeTypes.length > 0)
mimeType = mimeTypes[0];
}
if (mimeType == null || mimeType.trim().length() == 0)
mimeType = this.context.getRequest().getParameter(MVCConfigConstant.HTTP_HEADER_ACCEPT_PARAM);
if (mimeType == null || mimeType.trim().length() == 0) {
String contentType = this.context.getRequest().getContentType();
if (contentType != null) {
this.context.getResponse().setContentType(contentType);
mimeType = contentType.split(";")[0];
}
}
if (this.context.getWriter() == null)
this.context.setWriter(this.context.getResponse().getWriter());
if (MIMEType.JSON.equals(mimeType) || "json".equalsIgnoreCase(mimeType)) {
this.context.getResponse().setContentType(MIMEType.JSON);
this.context.getWriter().print(CommonUtil.toJson(retn));
} else if (MIMEType.XML.equals(mimeType) || "xml".equalsIgnoreCase(mimeType)) {
Class<?> cls = retn.getClass();
if (Collection.class.isAssignableFrom(cls)) {
Class<?> _cls = ClassUtil.getPojoClass(this.method);
if (_cls != null)
cls = _cls;
}
XMLWriter writer = BeanXMLUtil.getBeanXMLWriter(retn);
writer.setCheckStatck(true);
writer.setSubNameAuto(true);
writer.setClass(cls);
writer.setRootElementName(null);
this.context.getResponse().setContentType(MIMEType.XML);
this.context.getWriter().print(writer.toXml());
} else {
//默认都用json
this.context.getResponse().setContentType(MIMEType.JSON);
this.context.getWriter().print(CommonUtil.toJson(retn));
}
return;
}
List<String> produces = this.context.getActionConfigBean().getProduces();
if (produces != null && produces.size() > 0)
for (String produce : produces) {
this.context.getResponse().setContentType(produce);
break;
}
String re = String.valueOf(retn);
//model driven
for (Field f : fields) {
Method getter = ru.getGetter(f.getName());
if (getter == null)
continue;
String name = f.getName();
if (this.context.getModel().containsKey(name))
continue;
this.context.getModel().put(name, getter.invoke(actionObject));
}
this.context.getModel().put(MVCConfigConstant.BASE_URL_KEY, this.context.getServletContext().getAttribute(MVCConfigConstant.BASE_URL_KEY));
this.context.getModel().put(MVCConfigConstant.APPLICATION_SCOPE_KEY, new ServletContextProxy(this.context.getServletContext()).attrs());
this.context.getModel().put(MVCConfigConstant.SESSION_SCOPE_KEY, new HttpSessionProxy(this.context.getSession()).attrs());
this.context.getModel().put(MVCConfigConstant.COOKIE_SCOPE_KEY, new CookieProxy(this.context.getRequest().getCookies()).attrs());
this.context.getModel().put(MVCConfigConstant.REQ_PARAM_SCOPE_KEY, this.context.getQueryParamMap());
// 客户端重定向
if (re.startsWith(RenderType.REDIRECT + ":")) {
String url = re.substring((RenderType.REDIRECT + ":").length());
String location = url;
this.context.getResponse().sendRedirect(CommonUtil.replaceChinese2Utf8(location));
return;
} else if (re.startsWith(RenderType.ACTION + ":")) {
String path = re.substring((RenderType.ACTION + ":").length());
// ACTION 重定向
handleActionRedirect(context, path, baseUrl);
return;
} else if (re.startsWith(RenderType.OUT + ":")) {
String location = re.substring((RenderType.OUT + ":").length());
this.context.getWriter().print(location);
return;
} else if (re.startsWith(RenderType.FORWARD + ":") || re.startsWith(RenderType.JSP + ":") || re.endsWith("." + RenderType.JSP)) {
String[] str = re.split("@");
re = str[0];
String location = re;
if (re.startsWith(RenderType.FORWARD + ":"))
location = re.substring((RenderType.FORWARD + ":").length());
else if (re.startsWith(RenderType.JSP + ":"))
location = re.substring((RenderType.JSP + ":").length());
//渲染JSP
JSPRendererImpl render = new JSPRendererImpl();
render.setContext(context);
if (str.length > 1)
render.layout(str[1]);
render.target(location).render(context.getWriter(), context.getModel());
return;
} else if (re.startsWith(RenderType.FREEMARKER + ":") || re.startsWith(RenderType.FREEMARKER2 + ":") || re.endsWith("." + RenderType.FREEMARKER2)) {
String[] str = re.split("@");
re = str[0];
String location = re;
if (re.startsWith(RenderType.FREEMARKER + ":"))
location = re.substring((RenderType.FREEMARKER + ":").length());
else if (re.startsWith(RenderType.FREEMARKER2 + ":"))
location = re.substring((RenderType.FREEMARKER2 + ":").length());
//渲染Freemarker
Renderer render = RenderFactory.create(RenderType.FREEMARKER).target(location);
if (str.length > 1)
render.layout(str[1]);
render.render(context.getWriter(), context.getModel());
// this.context.getWriter().flush();
return;
} else if (re.startsWith(RenderType.VELOCITY + ":") || re.startsWith(RenderType.VELOCITY2 + ":") || re.endsWith("." + RenderType.VELOCITY2)) {
String[] str = re.split("@");
re = str[0];
String location = re;
if (re.startsWith(RenderType.VELOCITY + ":"))
location = re.substring((RenderType.VELOCITY + ":").length());
else if (re.startsWith(RenderType.VELOCITY2 + ":"))
location = re.substring((RenderType.VELOCITY2 + ":").length());
//渲染Velocity
Renderer render = RenderFactory.create(RenderType.VELOCITY).target(location);
if (str.length > 1)
render.layout(str[1]);
render.render(context.getWriter(), context.getModel());
// this.context.getWriter().flush();
return;
} else {
List<ResultConfigBean> results = this.context.getActionConfigBean().getResult();
if (results == null || results.size() == 0) {
this.context.getWriter().print(retn);
// this.context.getWriter().flush();
return;
}
boolean isOut = true;
for (ResultConfigBean r : results) {
if (!"_props_".equals(r.getName()) && !r.getName().equals(re) && !"".equals(re)) {
continue;
}
isOut = false;
String type = r.getType();
String location = r.getLocation();
if (RenderType.REDIRECT.equalsIgnoreCase(type)) {
this.context.getResponse().sendRedirect(CommonUtil.replaceChinese2Utf8(location));
return;
} else if (RenderType.FORWARD.equalsIgnoreCase(type) || RenderType.JSP.equalsIgnoreCase(type)) {
//渲染JSP
String[] str = location.split("@");
JSPRendererImpl render = new JSPRendererImpl();
render.setContext(context);
if (str.length > 1)
render.layout(str[1]);
render.target(str[0]).render(context.getWriter(), context.getModel());
return;
} else if (RenderType.FREEMARKER.equalsIgnoreCase(type) || RenderType.FREEMARKER2.equalsIgnoreCase(type)) {
//渲染Freemarker
String[] str = location.split("@");
Renderer render = RenderFactory.create(RenderType.FREEMARKER).target(str[0]);
if (str.length > 1)
render.layout(str[1]);
render.render(context.getWriter(), context.getModel());
return;
} else if (RenderType.VELOCITY.equalsIgnoreCase(type) || RenderType.VELOCITY2.equalsIgnoreCase(type)) {
//渲染Velocity
String[] str = location.split("@");
Renderer render = RenderFactory.create(RenderType.VELOCITY).target(str[0]);
if (str.length > 1)
render.layout(str[1]);
render.render(context.getWriter(), context.getModel());
return;
} else if (RenderType.ACTION.equalsIgnoreCase(type)) {
// ACTION 重定向
handleActionRedirect(context, location, baseUrl);
return;
} else if (RenderType.OUT.equalsIgnoreCase(type) || location.trim().length() == 0) {
this.context.getWriter().print(location);
return;
}
}
if (isOut) {
this.context.getWriter().print(retn);
// this.context.getWriter().flush();
}
}
}
Aggregations