Search in sources :

Example 96 with ZipOutputStream

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);
}
Also used : ZipOutputStream(java.util.zip.ZipOutputStream) ZipInputStream(java.util.zip.ZipInputStream) InputStream(java.io.InputStream) ZipEntry(java.util.zip.ZipEntry)

Example 97 with ZipOutputStream

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();
    }
}
Also used : ZipOutputStream(java.util.zip.ZipOutputStream) FileWriter(java.io.FileWriter) FileOutputStream(java.io.FileOutputStream) ZipEntry(java.util.zip.ZipEntry) File(java.io.File) FileInputStream(java.io.FileInputStream)

Example 98 with ZipOutputStream

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);
    }
}
Also used : FileInputStream(java.io.FileInputStream) InputStream(java.io.InputStream) ZipEntry(java.util.zip.ZipEntry) IOException(java.io.IOException) FileInputStream(java.io.FileInputStream) ZipFile(java.util.zip.ZipFile) ZipOutputStream(java.util.zip.ZipOutputStream) FileOutputStream(java.io.FileOutputStream) BufferedOutputStream(java.io.BufferedOutputStream) ZipFile(java.util.zip.ZipFile) File(java.io.File)

Example 99 with ZipOutputStream

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();
    }
}
Also used : ZipFile(java.util.zip.ZipFile) ZipOutputStream(java.util.zip.ZipOutputStream) FileOutputStream(java.io.FileOutputStream) ZipEntry(java.util.zip.ZipEntry) Test(org.junit.Test)

Example 100 with ZipOutputStream

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();
}
Also used : ZipOutputStream(java.util.zip.ZipOutputStream) ZipEntry(java.util.zip.ZipEntry)

Aggregations

ZipOutputStream (java.util.zip.ZipOutputStream)1168 ZipEntry (java.util.zip.ZipEntry)745 FileOutputStream (java.io.FileOutputStream)561 File (java.io.File)486 IOException (java.io.IOException)393 FileInputStream (java.io.FileInputStream)193 ByteArrayOutputStream (java.io.ByteArrayOutputStream)186 BufferedOutputStream (java.io.BufferedOutputStream)177 ZipFile (java.util.zip.ZipFile)163 InputStream (java.io.InputStream)144 Test (org.junit.Test)128 OutputStream (java.io.OutputStream)109 ByteArrayInputStream (java.io.ByteArrayInputStream)94 ZipInputStream (java.util.zip.ZipInputStream)88 Path (java.nio.file.Path)82 BufferedInputStream (java.io.BufferedInputStream)61 ArrayList (java.util.ArrayList)55 FileNotFoundException (java.io.FileNotFoundException)51 Date (java.util.Date)44 HashMap (java.util.HashMap)44