use of java.util.zip.ZipOutputStream in project camel by apache.
the class ZipFileDataFormat method marshal.
@Override
public void marshal(final Exchange exchange, final Object graph, final OutputStream stream) throws Exception {
String filename = exchange.getIn().getHeader(FILE_NAME, String.class);
if (filename == null) {
// generate the file name as the camel file component would do
filename = StringHelper.sanitize(exchange.getIn().getMessageId());
} else {
// remove any path elements
filename = Paths.get(filename).getFileName().toString();
}
ZipOutputStream zos = new ZipOutputStream(stream);
zos.putNextEntry(new ZipEntry(filename));
InputStream is = exchange.getContext().getTypeConverter().mandatoryConvertTo(InputStream.class, exchange, graph);
try {
IOHelper.copy(is, zos);
} finally {
IOHelper.close(is, zos);
}
String newFilename = filename + ".zip";
exchange.getOut().setHeader(FILE_NAME, newFilename);
}
use of java.util.zip.ZipOutputStream in project camel by apache.
the class ZipFileSplitOneFileTest method createZipFile.
private void createZipFile(String content) throws IOException {
String basePath = "target" + File.separator + "zip-unmarshal" + File.separator;
File file = new File(basePath + "test.txt");
file.getParentFile().mkdirs();
try (FileWriter fw = new FileWriter(file);
FileOutputStream fos = new FileOutputStream(basePath + "test.zip");
ZipOutputStream zos = new ZipOutputStream(fos);
FileInputStream fis = new FileInputStream(basePath + "test.txt")) {
fw.write(content);
fw.close();
ZipEntry entry = new ZipEntry("test.txt");
zos.putNextEntry(entry);
int len;
byte[] buffer = new byte[1024];
while ((len = fis.read(buffer)) > 0) {
zos.write(buffer, 0, len);
}
zos.closeEntry();
}
}
use of java.util.zip.ZipOutputStream in project robovm by robovm.
the class AbstractTarget method stripArchive.
protected void stripArchive(Path path, File output) throws IOException {
if (!config.isClean() && output.exists() && !path.hasChangedSince(output.lastModified())) {
config.getLogger().info("Not creating stripped archive file %s for unchanged path %s", output, path.getFile());
return;
}
config.getLogger().info("Creating stripped archive file %s", output);
ZipOutputStream out = null;
try {
out = new ZipOutputStream(new BufferedOutputStream(new FileOutputStream(output)));
if (path.getFile().isFile()) {
ZipFile archive = null;
try {
archive = new ZipFile(path.getFile());
Enumeration<? extends ZipEntry> entries = archive.entries();
while (entries.hasMoreElements()) {
ZipEntry entry = entries.nextElement();
if (entry.getName().toLowerCase().endsWith(".class")) {
continue;
}
if (entry.getName().startsWith("META-INF/robovm/")) {
// Don't include anything under META-INF/robovm/
continue;
}
ZipEntry newEntry = new ZipEntry(entry.getName());
newEntry.setTime(entry.getTime());
out.putNextEntry(newEntry);
InputStream in = null;
try {
in = archive.getInputStream(entry);
IOUtils.copy(in, out);
out.closeEntry();
} finally {
IOUtils.closeQuietly(in);
}
}
} finally {
try {
archive.close();
} catch (Throwable t) {
}
}
} else {
String basePath = path.getFile().getAbsolutePath();
@SuppressWarnings("unchecked") Collection<File> files = FileUtils.listFiles(path.getFile(), null, true);
for (File f : files) {
if (f.getName().toLowerCase().endsWith(".class")) {
continue;
}
String entryName = f.getAbsolutePath().substring(basePath.length() + 1);
if (entryName.startsWith("META-INF/robovm/")) {
// Don't include anything under META-INF/robovm/
continue;
}
ZipEntry newEntry = new ZipEntry(entryName);
newEntry.setTime(f.lastModified());
out.putNextEntry(newEntry);
InputStream in = null;
try {
in = new FileInputStream(f);
IOUtils.copy(in, out);
out.closeEntry();
} finally {
IOUtils.closeQuietly(in);
}
}
}
} catch (IOException e) {
IOUtils.closeQuietly(out);
output.delete();
} finally {
IOUtils.closeQuietly(out);
}
}
use of java.util.zip.ZipOutputStream in project byte-buddy by raphw.
the class ClassFileLocatorForModuleFileTest method testNonSuccessfulLocation.
@Test
public void testNonSuccessfulLocation() throws Exception {
ZipOutputStream zipOutputStream = new ZipOutputStream(new FileOutputStream(file));
try {
ZipEntry zipEntry = new ZipEntry("noop.class");
zipOutputStream.putNextEntry(zipEntry);
zipOutputStream.write(VALUE);
zipOutputStream.closeEntry();
} finally {
zipOutputStream.close();
}
ZipFile zipFile = new ZipFile(file);
try {
ClassFileLocator classFileLocator = new ClassFileLocator.ForModuleFile(zipFile);
ClassFileLocator.Resolution resolution = classFileLocator.locate(FOO + "." + BAR);
assertThat(resolution.isResolved(), is(false));
} finally {
zipFile.close();
}
}
use of java.util.zip.ZipOutputStream in project processing by processing.
the class Table method saveODS.
protected void saveODS(OutputStream os) throws IOException {
ZipOutputStream zos = new ZipOutputStream(os);
final String xmlHeader = "<?xml version=\"1.0\" encoding=\"UTF-8\"?>";
ZipEntry entry = new ZipEntry("META-INF/manifest.xml");
String[] lines = new String[] { xmlHeader, "<manifest:manifest xmlns:manifest=\"urn:oasis:names:tc:opendocument:xmlns:manifest:1.0\">", " <manifest:file-entry manifest:media-type=\"application/vnd.oasis.opendocument.spreadsheet\" manifest:version=\"1.2\" manifest:full-path=\"/\"/>", " <manifest:file-entry manifest:media-type=\"text/xml\" manifest:full-path=\"content.xml\"/>", " <manifest:file-entry manifest:media-type=\"text/xml\" manifest:full-path=\"styles.xml\"/>", " <manifest:file-entry manifest:media-type=\"text/xml\" manifest:full-path=\"meta.xml\"/>", " <manifest:file-entry manifest:media-type=\"text/xml\" manifest:full-path=\"settings.xml\"/>", "</manifest:manifest>" };
zos.putNextEntry(entry);
zos.write(PApplet.join(lines, "\n").getBytes());
zos.closeEntry();
/*
entry = new ZipEntry("meta.xml");
lines = new String[] {
xmlHeader,
"<office:document-meta office:version=\"1.0\"" +
" xmlns:office=\"urn:oasis:names:tc:opendocument:xmlns:office:1.0\" />"
};
zos.putNextEntry(entry);
zos.write(PApplet.join(lines, "\n").getBytes());
zos.closeEntry();
entry = new ZipEntry("meta.xml");
lines = new String[] {
xmlHeader,
"<office:document-settings office:version=\"1.0\"" +
" xmlns:config=\"urn:oasis:names:tc:opendocument:xmlns:config:1.0\"" +
" xmlns:office=\"urn:oasis:names:tc:opendocument:xmlns:office:1.0\"" +
" xmlns:ooo=\"http://openoffice.org/2004/office\"" +
" xmlns:xlink=\"http://www.w3.org/1999/xlink\" />"
};
zos.putNextEntry(entry);
zos.write(PApplet.join(lines, "\n").getBytes());
zos.closeEntry();
entry = new ZipEntry("settings.xml");
lines = new String[] {
xmlHeader,
"<office:document-settings office:version=\"1.0\"" +
" xmlns:config=\"urn:oasis:names:tc:opendocument:xmlns:config:1.0\"" +
" xmlns:office=\"urn:oasis:names:tc:opendocument:xmlns:office:1.0\"" +
" xmlns:ooo=\"http://openoffice.org/2004/office\"" +
" xmlns:xlink=\"http://www.w3.org/1999/xlink\" />"
};
zos.putNextEntry(entry);
zos.write(PApplet.join(lines, "\n").getBytes());
zos.closeEntry();
entry = new ZipEntry("styles.xml");
lines = new String[] {
xmlHeader,
"<office:document-styles office:version=\"1.0\"" +
" xmlns:office=\"urn:oasis:names:tc:opendocument:xmlns:office:1.0\" />"
};
zos.putNextEntry(entry);
zos.write(PApplet.join(lines, "\n").getBytes());
zos.closeEntry();
*/
final String[] dummyFiles = new String[] { "meta.xml", "settings.xml", "styles.xml" };
lines = new String[] { xmlHeader, "<office:document-meta office:version=\"1.0\"" + " xmlns:office=\"urn:oasis:names:tc:opendocument:xmlns:office:1.0\" />" };
byte[] dummyBytes = PApplet.join(lines, "\n").getBytes();
for (String filename : dummyFiles) {
entry = new ZipEntry(filename);
zos.putNextEntry(entry);
zos.write(dummyBytes);
zos.closeEntry();
}
//
entry = new ZipEntry("mimetype");
zos.putNextEntry(entry);
zos.write("application/vnd.oasis.opendocument.spreadsheet".getBytes());
zos.closeEntry();
//
entry = new ZipEntry("content.xml");
zos.putNextEntry(entry);
//lines = new String[] {
writeUTF(zos, new String[] { xmlHeader, "<office:document-content" + " xmlns:office=\"urn:oasis:names:tc:opendocument:xmlns:office:1.0\"" + " xmlns:text=\"urn:oasis:names:tc:opendocument:xmlns:text:1.0\"" + " xmlns:table=\"urn:oasis:names:tc:opendocument:xmlns:table:1.0\"" + " office:version=\"1.2\">", " <office:body>", " <office:spreadsheet>", " <table:table table:name=\"Sheet1\" table:print=\"false\">" });
//zos.write(PApplet.join(lines, "\n").getBytes());
byte[] rowStart = " <table:table-row>\n".getBytes();
byte[] rowStop = " </table:table-row>\n".getBytes();
if (hasColumnTitles()) {
zos.write(rowStart);
for (int i = 0; i < getColumnCount(); i++) {
saveStringODS(zos, columnTitles[i]);
}
zos.write(rowStop);
}
for (TableRow row : rows()) {
zos.write(rowStart);
for (int i = 0; i < getColumnCount(); i++) {
if (columnTypes[i] == STRING || columnTypes[i] == CATEGORY) {
saveStringODS(zos, row.getString(i));
} else {
saveNumberODS(zos, row.getString(i));
}
}
zos.write(rowStop);
}
//lines = new String[] {
writeUTF(zos, new String[] { " </table:table>", " </office:spreadsheet>", " </office:body>", "</office:document-content>" });
//zos.write(PApplet.join(lines, "\n").getBytes());
zos.closeEntry();
zos.flush();
zos.close();
}
Aggregations